def post(self, fileid): image = yield motor.Op( self.db.images.find_one, {'fileid': fileid} ) if not image: raise tornado.web.HTTPError(404, "File not found") if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: raise NotImplementedError original = find_original( image['fileid'], self.application.settings['static_path'], extension, ) size = os.stat(original)[stat.ST_SIZE] yield motor.Op( self.db.images.update, {'_id': image['_id']}, {'$set': {'size': size}} ) metadata_key = 'metadata:%s' % fileid self.redis.delete(metadata_key) url = self.reverse_url('admin_image', fileid) self.redirect(url)
def post(self, fileid): image = yield motor.Op(self.db.images.find_one, {'fileid': fileid}) if not image: raise tornado.web.HTTPError(404, "File not found") if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: raise NotImplementedError original = find_original( image['fileid'], self.application.settings['static_path'], extension, ) size = os.stat(original)[stat.ST_SIZE] yield motor.Op(self.db.images.update, {'_id': image['_id']}, {'$set': { 'size': size }}) metadata_key = 'metadata:%s' % fileid self.redis.delete(metadata_key) url = self.reverse_url('admin_image', fileid) self.redirect(url)
def post(self, fileid): image = yield motor.Op( self.db.images.find_one, {'fileid': fileid} ) if not image: raise tornado.web.HTTPError(404, "File not found") if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: raise NotImplementedError original = find_original( image['fileid'], self.application.settings['static_path'], extension, ) original_dir = os.path.dirname(original) originals = [] for filename in os.listdir(original_dir): filepath = os.path.join(original_dir, filename) os.remove(filepath) url = self.reverse_url('admin_image', fileid) self.redirect(url)
def attach_original_info(self, image): if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: return False original_path = find_original( image['fileid'], self.application.settings['static_path'], extension ) image['has_original'] = bool(original_path)
def upload_original(fileid, extension, static_path, bucket_id): conn = S3Connection(settings.AWS_ACCESS_KEY, settings.AWS_SECRET_KEY) bucket = conn.lookup(bucket_id) or conn.create_bucket(bucket_id, location=Location.EU) db_connection = motor.MotorConnection().open_sync() db = db_connection[settings.DATABASE_NAME] original = find_original(fileid, static_path, extension) if original: relative_path = original.replace(static_path, '') k = Key(bucket) k.key = relative_path print "Uploading original", original s = os.stat(original)[stat.ST_SIZE] print "%.1fKb" % (s / 1024.) # reduced because I'm a cheapskate k.set_contents_from_filename(original, reduced_redundancy=True) print "Original uploaded" else: print "Original can't be found", repr(original)
def get(self): data = {} page = int(self.get_argument('page', 1)) page_size = 20 skip = page_size * (page - 1) search = {'width': {'$exists': True}} total_count = yield motor.Op(self.db.images.find(search).count) cursor = ( self.db.images.find(search) .sort([('date', -1)]) .limit(page_size) .skip(skip) ) images = [] count = 0 total_bytes_served = 0 total_hits = 0 _shown_image_ids = [] while (yield cursor.fetch_next): image = cursor.next_object() if not image.get('width'): if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: raise NotImplementedError original = find_original( image['fileid'], self.application.settings['static_path'], extension, ) if not original: continue size = Image.open(original).size data = { 'width': size[0], 'height': size[1] } yield motor.Op( self.db.images.update, {'_id': image['_id']}, {'$set': data} ) image['width'] = data['width'] image['height'] = data['height'] self.attach_tiles_info(image) if not image.get('cdn_domain'): lock_key = 'uploading:%s' % image['fileid'] image['uploading_locked'] = self.redis.get(lock_key) count += 1 served = self.redis.hget('bytes_served', image['fileid']) if served is not None: total_bytes_served += int(served) image['bytes_served'] = int(served) hits = self.redis.get('hits:%s' % image['fileid']) if hits is not None: total_hits += int(hits) image['hits'] = int(hits) _shown_image_ids.append(image['_id']) comments = self.redis.hget('comments', image['fileid']) if comments is not None: comments = int(comments) image['comments'] = comments images.append(image) pagination = None if total_count > count: # pagination in order! pagination = { 'current_page': page, 'range': range(1, total_count / page_size + 2) } if (page - 1) * page_size > 0: pagination['prev'] = page - 1 if page * page_size < total_count: pagination['next'] = page + 1 data['pagination'] = pagination data['images'] = images data['total_count'] = total_count data['bytes_downloaded'] = self.redis.get('bytes_downloaded') cursor = self.db.images.find({}, ('fileid',)) while (yield cursor.fetch_next): image = cursor.next_object() if image['_id'] not in _shown_image_ids: served = self.redis.hget('bytes_served', image['fileid']) if served is not None: total_bytes_served += int(served) hits = self.redis.get('hits:%s' % image['fileid']) if hits is not None: total_hits += int(hits) data['total_bytes_served'] = total_bytes_served data['total_hits'] = total_hits total_comments = yield motor.Op(self.db.comments.find().count) data['total_comments'] = total_comments self.render('admin/home.html', **data)
def get(self): data = {} page = int(self.get_argument('page', 1)) page_size = 20 skip = page_size * (page - 1) search = {'width': {'$exists': True}} total_count = yield motor.Op(self.db.images.find(search).count) cursor = ( self.db.images.find(search) .sort([('date', -1)]) .limit(page_size) .skip(skip) ) image = yield motor.Op(cursor.next_object) images = [] count = 0 while image: if not image.get('width'): if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: raise NotImplementedError original = find_original( image['fileid'], self.application.settings['static_path'], extension, ) if not original: image = yield motor.Op(cursor.next_object) continue size = Image.open(original).size data = { 'width': size[0], 'height': size[1] } yield motor.Op( self.db.images.update, {'_id': image['_id']}, {'$set': data} ) image['width'] = data['width'] image['height'] = data['height'] self.attach_tiles_info(image) if not image.get('cdn_domain'): lock_key = 'uploading:%s' % image['fileid'] image['uploading_locked'] = self.redis.get(lock_key) count += 1 images.append(image) image = yield motor.Op(cursor.next_object) pagination = None if total_count > count: # pagination in order! pagination = { 'current_page': page, 'range': range(1, total_count / page_size + 2) } if (page - 1) * page_size > 0: pagination['prev'] = page - 1 if page * page_size < total_count: pagination['next'] = page + 1 data['pagination'] = pagination data['images'] = images data['total_count'] = total_count self.render('admin/home.html', **data)
def get(self, fileid): image = yield motor.Op( self.db.images.find_one, {'fileid': fileid} ) if not image: raise tornado.web.HTTPError(404, "File not found") self.attach_tiles_info(image) self.attach_hits_info(image) self.attach_comments_info(image) self.attach_tweet_info(image) served = self.redis.hget('bytes_served', image['fileid']) if served is not None: image['bytes_served'] = int(served) lock_key = 'uploading:%s' % fileid uploading_locked = self.redis.get(lock_key) if uploading_locked: try: uploading_locked = int(float(uploading_locked)) if uploading_locked != 1: diff = int(time.time()) - uploading_locked left = 60 * 60 - diff if left > 60: uploading_locked = "%s minutes left" % (left / 60) else: uploading_locked = "%s seconds left" % left except ValueError: pass awsupdating_key = 'awsupdated:%s' % fileid awsupdating_locked = self.redis.get(awsupdating_key) is not None unsubscribed = self.redis.sismember('unsubscribed', image['user']) data = { 'image': image, 'uploading_locked': uploading_locked, 'awsupdating_locked': awsupdating_locked, 'unsubscribed': unsubscribed, } if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: raise NotImplementedError original = find_original( image['fileid'], self.application.settings['static_path'], extension, ) if original: original_dir = os.path.dirname(original) originals = [] for filename in os.listdir(original_dir): filepath = os.path.join(original_dir, filename) originals.append( (filename, os.stat(filepath)[stat.ST_SIZE]) ) data['originals'] = originals data['original_total_size'] = sum(x[1] for x in originals) else: data['original_total_size'] = 0 data['originals'] = [] self.render('admin/image.html', **data)
def get(self): data = {} page = int(self.get_argument('page', 1)) page_size = 20 skip = page_size * (page - 1) search = {'width': {'$exists': True}} total_count = yield motor.Op(self.db.images.find(search).count) cursor = (self.db.images.find(search).sort([ ('date', -1) ]).limit(page_size).skip(skip)) images = [] count = 0 total_bytes_served = 0 total_hits = 0 _shown_image_ids = [] while (yield cursor.fetch_next): image = cursor.next_object() if not image.get('width'): if image['contenttype'] == 'image/jpeg': extension = 'jpg' elif image['contenttype'] == 'image/png': extension = 'png' else: raise NotImplementedError original = find_original( image['fileid'], self.application.settings['static_path'], extension, ) if not original: continue size = Image.open(original).size data = {'width': size[0], 'height': size[1]} yield motor.Op(self.db.images.update, {'_id': image['_id']}, {'$set': data}) image['width'] = data['width'] image['height'] = data['height'] self.attach_tiles_info(image) if not image.get('cdn_domain'): lock_key = 'uploading:%s' % image['fileid'] image['uploading_locked'] = self.redis.get(lock_key) count += 1 served = self.redis.hget('bytes_served', image['fileid']) if served is not None: total_bytes_served += int(served) image['bytes_served'] = int(served) hits = self.redis.get('hits:%s' % image['fileid']) if hits is not None: total_hits += int(hits) image['hits'] = int(hits) _shown_image_ids.append(image['_id']) comments = self.redis.hget('comments', image['fileid']) if comments is not None: comments = int(comments) image['comments'] = comments images.append(image) pagination = None if total_count > count: # pagination in order! pagination = { 'current_page': page, 'range': range(1, total_count / page_size + 2) } if (page - 1) * page_size > 0: pagination['prev'] = page - 1 if page * page_size < total_count: pagination['next'] = page + 1 data['pagination'] = pagination data['images'] = images data['total_count'] = total_count data['bytes_downloaded'] = self.redis.get('bytes_downloaded') cursor = self.db.images.find({}, ('fileid', )) while (yield cursor.fetch_next): image = cursor.next_object() if image['_id'] not in _shown_image_ids: served = self.redis.hget('bytes_served', image['fileid']) if served is not None: total_bytes_served += int(served) hits = self.redis.get('hits:%s' % image['fileid']) if hits is not None: total_hits += int(hits) data['total_bytes_served'] = total_bytes_served data['total_hits'] = total_hits total_comments = yield motor.Op(self.db.comments.find().count) data['total_comments'] = total_comments self.render('admin/home.html', **data)