コード例 #1
0
ファイル: cdn.py プロジェクト: diazinc/angular-python
def resizedimageifneeded(filename, path, backupbucketlocation, backuppath=None):
    try:
        if path is not None:
            body = load("/%s/%s" % (path, filename), immediate_call=True)
            return body
    except:
        pass
    #got here because image didn't exist               
    pathbits = filename.split(".")
    if len(pathbits) < 4 or pathbits[-1] != pathbits[-4]:
        raise Exception("not long enough")
    filename = ".".join(pathbits[:-3])
    (new_width, new_height) = (int(pathbits[-3]),int(pathbits[-2]))
    fullpath = ("/%s" % (filename)) if backuppath is None else ("/%s/%s" % (backuppath, filename))
    body = load(fullpath, bucket="/"+backupbucketlocation, immediate_call=True)
    buffer = StringIO.StringIO(body)
    img = PIL.Image.open(buffer)
    (width, height) = img.size
    if img.mode not in ('L', 'RGB', 'RGBA'):
        img = img.convert('RGB')
        (width, height) = img.size
    
    if height < 50 or width < 50:
        raise Exception("Image is not big enough!")
    
    new_ratio= float(new_height)/float(new_width)
    orig_ratio= float(height) / float(width)
                
    if orig_ratio < new_ratio:
        if orig_ratio * (1+LEEWAY) < new_ratio: 
            ratio=orig_ratio*(1+LEEWAY)
        else:
            ratio=new_ratio
        new_size=(int(new_height/ratio),new_height)
    else:
        if orig_ratio * (1-LEEWAY) > new_ratio: 
            ratio=orig_ratio*(1-LEEWAY)
        else:
            ratio=new_ratio
        new_size=(new_width,int(new_width*ratio))
           
    (actual_new_width,actual_new_height)=new_size

    sizedimg = img.resize(new_size, PIL.Image.ANTIALIAS)

    up=int((actual_new_height-new_height)*OFFSET)
    across=int((actual_new_width-new_width)*OFFSET)
    
    cropbox=(across,up,across+new_width,up+new_height)
    #print cropbox
    newimg=sizedimg.crop(cropbox) 
    
    output= StringIO.StringIO()
    newimg.save(output, "JPEG", quality=90)
    output.seek(0)
    binary = output.read()
    output.close()
    return binary
コード例 #2
0
 def any(self):
     #updating cdn.positiondial.com articles/45370.html exists
     resource_state = self.request.headers['X-Goog-Resource-State']
     if resource_state == 'sync':
         logging.info('Sync message received.')
     else:
         an_object = json.loads(self.request.body)
         bucket = an_object['bucket']
         resource = str(an_object['name'])
         logging.info('updating %s %s %s', bucket, resource, resource_state)
         try:
             if resource.startswith("build"):
                 listfiles("/" + "/".join(str(resource).split("/")[:-1]) +
                           "/")
                 if resource.endswith("js") and not ".min." in resource:
                     (needs, newfile,
                      originalbody) = needsminifyjs(resource)
                     if needs:
                         save(newfile, originalbody)
                         taskqueue.add(url='/securenotify/' + resource,
                                       queue_name='reminify')
                     load("/" + resource)
                 if resource.endswith(".min.js"):
                     load("/" + resource)
                     filepreload(True)
                     filepreload(False)
                 if resource.endswith("html"):
                     load("/" + resource)
                     filepreload(True)
                     filepreload(False)
             if resource.startswith("optimized"):
                 load("/" + resource)
                 filepreload(True)
                 filepreload(False)
             elif "img" in resource.split("/")[0]:
                 load("/" + resource)
         except Exception as e:
             logging.exception(e)
コード例 #3
0
ファイル: cdn.py プロジェクト: diazinc/angular-python
 def any(self,path):
     body = load("/%s" % (path))
     if not isadmin():
         self.response.headers['cache-control'] = 'no-transform,public,max-age=1800'
     self.response.headers['content-type'] = 'image/jpg'
     self.response.write(body)
コード例 #4
0
ファイル: cdn.py プロジェクト: diazinc/angular-python
 def any(self,path):
     body = load("/build/%s/%s" % (currentversionid,path))
     self.response.headers['content-type'] = 'text/javascript'
     self.response.write(body)