def _RenderImage(self, req): """Get a URL that satisfies req. Returns a string that starts with either file:// or http://, depending on whether we have a configured landing area for local cache files. """ # If we already have this exact request locally, return it. cachefile = req.GetCacheFile(self.cache_dir) if os.path.exists(cachefile): VARS['fecache_hits'] += 1 return 'file://%s' % cachefile origfile = req.GetOrigFile() if not os.path.exists(origfile): VARS['missing_files'] += 1 return 'OH NOES: can\'t find original image file' if req.size == 0: return 'file://%s' % origfile # Render image. try: imgops.render(origfile, cachefile, req.size, req.inst) except imgops.Error, e: log('Failed to render %s: %s' % (cachefile, e)) return None
def MigrateImage(db, bucket, imgid, lazy=False): imgid=int(imgid) c = db.cursor() c.execute('SELECT p.firstname, p.lastname, p.username, ' 'i.restriction, i.imagefile, i.rotation, ' 'date_part(\'year\', i.ts), f.mimetype, w.description, ' 'i.caption ' 'FROM image i INNER JOIN person p ON i.author = p.personid ' 'INNER JOIN imageformat f ON i.format = f.formatid ' 'INNER JOIN watermark w ON i.watermark = w.watermarkid ' 'WHERE i.imageid = %s;' % imgid) rs = c.fetchall() if len(rs) == 0: print '%s: No row in database' % imgid return data = rs[0] FN, LN, LOGIN, REST, FILE, ROT, YEAR, MIME, WM, CAP = range(10) fake_req = pkeep.RequestSpec('', 0, 0, imgid, 0) if not os.path.exists(data[FILE]): print '%s: No such file %s' % (imgid, data[FILE]) return if data[WM] not in ('Copyright', 'Username', 'Caption', 'None'): print '%s: Can\'t migrate watermark %s' % (imgid, data[WM]) return key = fake_req.orig_key() headers = { 'Content-Type': data[MIME] } print '--> %s' % key k = bucket.new_key(key) k.set_contents_from_filename(data[FILE], headers=headers, cb=ShowProgress, num_cb=50) # print '<-- %s %s' % (res.status, res.reason) metadata = '' if data[REST] == 1: metadata += 'PUBLIC\n' if data[ROT] != 0: metadata += 'ROTATE %s\n' % data[ROT] if data[WM] == 'Copyright': metadata += 'TEXT_RIGHT (C) %s %s %s\n' % \ (int(data[YEAR]), data[FN], data[LN]) elif data[WM] == 'Username': metadata += 'TEXT_RIGHT (C) %s %s\n' % \ (int(data[YEAR]), data[LOGIN]) elif data[WM] == 'Caption': metadata += 'TEXT_LEFT %s\n' % data[CAP] key = fake_req.meta_key() headers['Content-Type'] = 'text/plain' print '--> %s' % key k = bucket.new_key(key) k.set_contents_from_string(metadata, headers=headers) if not lazy: for s in (140, 250, 350, 640, 1024): fake_req.size = s cachefile = pkeep.GetLocalCacheFile('/srv/pkeep_cache', fake_req) print '--> %s' % cachefile imgops.render(data[FILE], cachefile, s, metadata) sql = 'UPDATE image SET imagefile=\'s3\' WHERE imageid=%s;' % imgid print sql c.execute(sql) db.commit() return
k = self.bucket.get_key(req.meta_key()) k.get_contents_to_filename(mdfile) except (S3ResponseError, AttributeError), e: log('can\'t fetch %s: %s' % (req.meta_key(), e), ERR) VARS['s3_errors'] += 1 return 'OH NOES: can\'t retrieve metadata' metadata = open(mdfile, 'r').read() # Render image. If saving a frontend cache file, render directly # to that path. if self.cache_dir: newfile = GetLocalCacheFile(self.cache_dir, req) else: newfile = os.path.join(tmp_dir, 'new-%d-%d.jpg' % (req.id, req.size)) imgops.render(origfile, newfile, req.size, metadata) # If saving a local cache file, just return its path. if self.cache_dir: return 'file://%s' % GetLocalCacheFile(self.cache_dir, req) # No local cache; stuff the scaled image back in S3 and return its URL. headers = { 'Content-Type': 'image/jpeg' } try: k = self.bucket.new_key(req.cache_key()) k.set_contents_from_filename(newfile, headers=headers) except S3ResponseError, e: log('can\'t store %s: %s' % (k.name, e), ERR) VARS['s3_errors'] += 1 return 'OH NOES: can\'t store cache file'