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)
def save_sr_image(sr, data, num = 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.] """ import tempfile from r2.lib import s3cp from md5 import md5 hash = md5(data).hexdigest() try: f = tempfile.NamedTemporaryFile(suffix = '.png') f.write(data) f.flush() resource = g.s3_thumb_bucket + sr._fullname if num is not None: resource += '_' + str(num) resource += '.png' s3cp.send_file(f.name, resource, 'image/png', 'public-read', None, False) finally: f.close() return 'http:/%s%s?v=%s' % (g.s3_thumb_bucket, resource.split('/')[-1], hash)
def save_sr_image(sr, data, num=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.] """ import tempfile from r2.lib import s3cp from md5 import md5 hash = md5(data).hexdigest() try: f = tempfile.NamedTemporaryFile(suffix='.png') f.write(data) f.flush() resource = g.s3_thumb_bucket + sr._fullname if num is not None: resource += '_' + str(num) resource += '.png' s3cp.send_file(f.name, resource, 'image/png', 'public-read', None, False) finally: f.close() return 'http:/%s%s?v=%s' % (g.s3_thumb_bucket, resource.split('/')[-1], hash)
def s3_upload_media(data, file_name, file_type, mime_type, never_expire): bucket = filename_to_s3_bucket(file_name) s3cp.send_file(bucket, file_name+file_type, data, mime_type, never_expire=never_expire, replace = False, reduced_redundancy=True) if g.s3_media_direct: return "http://%s/%s/%s%s" % (s3_direct_url, bucket, file_name, file_type) else: return "http://%s/%s%s" % (bucket, file_name, file_type)
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') image.save(f) resource = s3_thumbnail_bucket + link._fullname + '.png' log.debug('uploading to s3: %s' % link._fullname) s3cp.send_file(f.name, resource, 'image/png', 'public-read', None, False) log.debug('thumbnail %s: %s' % (link._fullname, thumbnail_url(link)))
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') image.save(f) resource = s3_thumbnail_bucket + link._fullname + '.png' log.debug('uploading to s3: %s' % link._fullname) s3cp.send_file(f.name, resource, 'image/png', 'public-read', None, False) log.debug('thumbnail %s: %s' % (link._fullname, thumbnail_url(link)))
def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False): from r2.models import ModAction author = author if author else c.user.name if content is None: content = '' try: wiki = WikiPage.get(self, 'config/stylesheet') except tdb_cassandra.NotFound: wiki = WikiPage.create(self, 'config/stylesheet') wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force) minified = cssmin(parsed) if minified: if g.static_stylesheet_bucket: digest = hashlib.sha1(minified).digest() self.stylesheet_hash = ( base64.urlsafe_b64encode(digest).rstrip("=")) s3cp.send_file( g.static_stylesheet_bucket, self.static_stylesheet_name, minified, content_type="text/css", never_expire=True, replace=False, ) self.stylesheet_contents = "" self.stylesheet_modified = None else: self.stylesheet_hash = hashlib.md5(minified).hexdigest() self.stylesheet_contents = minified self.stylesheet_modified = datetime.datetime.now(g.tz) else: self.stylesheet_contents = "" self.stylesheet_hash = "" self.stylesheet_modified = datetime.datetime.now(g.tz) self.stylesheet_contents_user = "" # reads from wiki; ensure pg clean self._commit() ModAction.create(self, c.user, action='wikirevise', details='Updated subreddit stylesheet') return wr
def s3_upload_media(data, file_name, file_type, mime_type, never_expire, replace=False): bucket = filename_to_s3_bucket(file_name) s3cp.send_file(bucket, file_name+file_type, data, mime_type, never_expire=never_expire, replace=replace, reduced_redundancy=True) if g.s3_media_direct: return "http://%s/%s/%s%s" % (s3_direct_url, bucket, file_name, file_type) else: return "http://%s/%s%s" % (bucket, file_name, file_type)
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)
def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False): from r2.models import ModAction author = author if author else c.user.name if content is None: content = "" try: wiki = WikiPage.get(self, "config/stylesheet") except tdb_cassandra.NotFound: wiki = WikiPage.create(self, "config/stylesheet") wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force) minified = cssmin(parsed) if minified: if g.static_stylesheet_bucket: digest = hashlib.sha1(minified).digest() self.stylesheet_hash = base64.urlsafe_b64encode(digest).rstrip("=") s3cp.send_file( g.static_stylesheet_bucket, self.static_stylesheet_name, minified, content_type="text/css", never_expire=True, replace=False, ) self.stylesheet_contents = "" self.stylesheet_modified = None else: self.stylesheet_hash = hashlib.md5(minified).hexdigest() self.stylesheet_contents = minified self.stylesheet_modified = datetime.datetime.now(g.tz) else: self.stylesheet_contents = "" self.stylesheet_hash = "" self.stylesheet_modified = datetime.datetime.now(g.tz) self.stylesheet_contents_user = "" # reads from wiki; ensure pg clean self._commit() ModAction.create(self, c.user, action="wikirevise", details="Updated subreddit stylesheet") return wr
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)