Esempio n. 1
0
def save_sr_image(sr, data, resource = None):
    """
    uploades image data to s3 as a PNG and returns its new url.  Urls
    will be of the form:
      http://${g.s3_thumb_bucket}/${sr._fullname}[_${num}].png?v=${md5hash}
    [Note: g.s3_thumb_bucket begins with a "/" so the above url is valid.]
    """
    hash = md5(data).hexdigest()

    f = tempfile.NamedTemporaryFile(suffix = '.png',delete=False)
    try:
        f.write(data)
        f.close()

        optimize_png(f.name, g.png_optimizer)
        contents = open(f.name).read()

        if resource is not None:
            resource = "_%s" % resource
        else:
            resource = ""
        fname = resource = sr._fullname + resource + ".png"

        s3cp.send_file(g.s3_thumb_bucket, fname, contents, 'image/png')

    finally:
        os.unlink(f.name)

    return 'http://%s/%s?v=%s' % (g.s3_thumb_bucket, 
                                  resource.split('/')[-1], hash)
Esempio n. 2
0
def upload_media(image, never_expire=True, file_type='.jpg'):
    """Given a link and an image, uploads the image to s3 into an image
    based on the link's fullname"""
    url = str()
    mime_type = mimetypes.guess_type("file" + file_type)[0] # Requires a filename with the extension
    f = tempfile.NamedTemporaryFile(suffix=file_type, delete=False)
    try:
        img = image
        if isinstance(img, basestring):
            img = str_to_image(img)
        
        if not img.mode == 'RGBA': # Indexed images will not convert properly
            img = img.convert('RGBA')

        if file_type == ".jpg":
            # PIL does not play nice when converting alpha channels to jpg
            background = Image.new('RGBA', img.size, (255, 255, 255))
            background.paste(img, img)
            img = background.convert('RGB')
        img.save(f, quality=85, optimize=True)
        
        if file_type == ".png":
            optimize_png(f.name, g.png_optimizer)
        contents = open(f.name).read()
        file_name = get_filename_from_content(contents)
        if g.media_store == "s3":
            url = s3_upload_media(contents, file_name=file_name, mime_type=mime_type, file_type=file_type, never_expire=True)
    finally:
        os.unlink(f.name)
    return url
Esempio n. 3
0
def upload_thumb(link, image):
    """Given a link and an image, uploads the image to s3 into an image
    based on the link's fullname"""
    f = tempfile.NamedTemporaryFile(suffix = '.png', delete=False)
    try:
        image.save(f)
        f.close()
        g.log.debug("optimizing %s in %s" % (link._fullname,f.name))
        optimize_png(f.name, g.png_optimizer)
        contents = open(f.name).read()

        s3fname = link._fullname + '.png'

        log.debug('uploading to s3: %s' % link._fullname)
        s3cp.send_file(g.s3_thumb_bucket, s3fname, contents, 'image/png', never_expire=True)
        log.debug('thumbnail %s: %s' % (link._fullname, thumbnail_url(link)))
    finally:
        os.unlink(f.name)
Esempio n. 4
0
def upload_thumb(link, image):
    """Given a link and an image, uploads the image to s3 into an image
    based on the link's fullname"""
    f = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
    try:
        image.save(f)
        f.close()
        g.log.debug("optimizing %s in %s" % (link._fullname, f.name))
        optimize_png(f.name, g.png_optimizer)
        contents = open(f.name).read()

        s3fname = link._fullname + '.png'

        log.debug('uploading to s3: %s' % link._fullname)
        s3cp.send_file(g.s3_thumb_bucket,
                       s3fname,
                       contents,
                       'image/png',
                       never_expire=True)
        log.debug('thumbnail %s: %s' % (link._fullname, thumbnail_url(link)))
    finally:
        os.unlink(f.name)