mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-02-19 02:34:20 -05:00
Use JPEG and reduced redundancy for thumbnails.
This commit is contained in:
@@ -482,7 +482,7 @@ class PromoteController(ListingController):
|
||||
errors = dict(BAD_CSS_NAME = "", IMAGE_ERROR = "")
|
||||
try:
|
||||
# thumnails for promoted links can change and therefore expire
|
||||
force_thumbnail(link, file)
|
||||
force_thumbnail(link, file, fileType=".jpg")
|
||||
except cssfilter.BadImage:
|
||||
# if the image doesn't clean up nicely, abort
|
||||
errors["IMAGE_ERROR"] = _("bad image")
|
||||
|
||||
@@ -209,7 +209,7 @@ def valid_url(prop,value,report):
|
||||
if c.site.images.has_key(name):
|
||||
num = c.site.images[name]
|
||||
value._setCssText("url(http://%s/%s_%d.png?v=%s)"
|
||||
% (g.s3_thumb_bucket, c.site._fullname, num,
|
||||
% (g.s3_thumb_bucket, c.site._fullname[::-1], num,
|
||||
randstr(36)))
|
||||
else:
|
||||
# unknown image label -> error
|
||||
@@ -434,7 +434,7 @@ def save_sr_image(sr, data, resource = None):
|
||||
resource = "_%s" % resource
|
||||
else:
|
||||
resource = ""
|
||||
fname = resource = sr._fullname + resource + ".png"
|
||||
fname = resource = sr._fullname[::-1] + resource + ".png"
|
||||
|
||||
s3cp.send_file(g.s3_thumb_bucket, fname, contents, 'image/png')
|
||||
|
||||
|
||||
@@ -35,32 +35,43 @@ import os
|
||||
import tempfile
|
||||
import traceback
|
||||
|
||||
import mimetypes
|
||||
|
||||
s3_thumbnail_bucket = g.s3_thumb_bucket
|
||||
threads = 20
|
||||
log = g.log
|
||||
|
||||
link_jpg_started = 113
|
||||
|
||||
|
||||
def thumbnail_url(link):
|
||||
"""Given a link, returns the url for its thumbnail based on its fullname"""
|
||||
res = 'http://%s/%s.png' % (s3_thumbnail_bucket, link._fullname)
|
||||
if hasattr(link, "thumbnail_version"):
|
||||
res += "?v=%s" % link.thumbnail_version
|
||||
if (link._id >= link_jpg_started or hasattr(link, "thumbnail_version") or not g.old_s3_thumb_bucket):
|
||||
res = 'http://%s.s3.amazonaws.com/%s.jpg' % (s3_thumbnail_bucket,link._fullname[::-1])
|
||||
if hasattr(link, "thumbnail_version"):
|
||||
res += "?v=%s" % link.thumbnail_version
|
||||
else:
|
||||
res = 'http://%s.s3.amazonaws.com/%s.png' % (g.old_s3_thumb_bucket,link._fullname)
|
||||
|
||||
return res
|
||||
|
||||
def upload_thumb(link, image, never_expire = True, reduced_redundancy=True):
|
||||
def upload_thumb(link, image, never_expire = True, reduced_redundancy=True, fileType = ".jpg"):
|
||||
"""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)
|
||||
mimeType = mimetypes.guess_type("file"+fileType)[0] # Requires a filename with the extension
|
||||
f = tempfile.NamedTemporaryFile(suffix = fileType, 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)
|
||||
if(fileType == ".png"):
|
||||
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'
|
||||
s3fname = link._fullname[::-1] + fileType
|
||||
|
||||
log.debug('uploading to s3: %s' % link._fullname)
|
||||
s3cp.send_file(g.s3_thumb_bucket, s3fname, contents, 'image/png',
|
||||
log.debug('uploading to s3: %s' % link._fullname[::-1])
|
||||
s3cp.send_file(g.s3_thumb_bucket, s3fname, contents, mimeType,
|
||||
never_expire=never_expire,
|
||||
reduced_redundancy=reduced_redundancy)
|
||||
log.debug('thumbnail %s: %s' % (link._fullname, thumbnail_url(link)))
|
||||
@@ -108,10 +119,10 @@ def set_media(link, force = False):
|
||||
|
||||
update_link(link, thumbnail, media_object)
|
||||
|
||||
def force_thumbnail(link, image_data, never_expire = True):
|
||||
def force_thumbnail(link, image_data, never_expire = True, fileType=".jpg"):
|
||||
image = str_to_image(image_data)
|
||||
image = prepare_image(image)
|
||||
upload_thumb(link, image, never_expire = never_expire)
|
||||
upload_thumb(link, image, never_expire = never_expire, fileType = fileType)
|
||||
update_link(link, thumbnail = True, media_object = None)
|
||||
|
||||
def run():
|
||||
|
||||
@@ -34,7 +34,7 @@ NEVER = 'Thu, 31 Dec 2037 23:59:59 GMT'
|
||||
|
||||
class S3Exception(Exception): pass
|
||||
|
||||
def send_file(bucketname, filename, content, content_type = 'text/plain', never_expire = False, reduced_redundancy=False):
|
||||
def send_file(bucketname, filename, content, content_type = 'text/plain', never_expire = False, reduced_redundancy=True):
|
||||
# this function is pretty low-traffic, but if we start using it a
|
||||
# lot more we'll want to maintain a connection pool across the app
|
||||
# rather than connecting on every invocation
|
||||
@@ -50,5 +50,5 @@ def send_file(bucketname, filename, content, content_type = 'text/plain', never_
|
||||
headers['Expires'] = NEVER
|
||||
|
||||
k.set_contents_from_string(content, policy='public-read',
|
||||
headers=headers)
|
||||
# reduced_redundancy=reduced_redundancy)
|
||||
headers=headers,
|
||||
reduced_redundancy=reduced_redundancy)
|
||||
|
||||
Reference in New Issue
Block a user