def uploadFile(filename): exif = photoutils.getExifData(filename) i = Image.open(filename) doc = {} doc['width'], doc['height'] = i.size doc['keywords'] = ['unprocessed'] doc['descr'] = 'Uploaded image.' doc['size'] = os.stat(filename).st_size doc['extension'] = FORMATS.get(i.format, i.format).lower() doc['tnwidth'], doc['tnheight'] = photoutils.scaleDims(i.size, TNSIZE) doc['cat'] = 'Private' doc['addedby'] = getpass.getuser() doc['type'] = 'photo' doc['ts'] = time.strftime("%Y-%m-%dT%H:%M:%S") doc['taken'] = takenDate(exif) doc['_id'] = md5File(filename) doc['exif'] = exif ext = doc['extension'] mimeType = 'image/' + i.format.lower() # Save it to S3 before it hits the DB. Mildly annoying when # there's a failure, but I'd rather not think I've got photos I've # got than vice versa. saveS3(doc['_id'], filename, ext, mimeType) docid, rev = db.save(doc) saveScaled(docid, i, (800, 600), '800x600.' + ext, mimeType) saveScaled(docid, i, TNSIZE, 'thumb.' + ext, mimeType)
docs = [] for d in db.view('app/missing-exif', limit=PICS_PER_BATCH): doc = couchdb.Document(_id=d.id) doc.update(d.value) if doc['extension'] == 'jpg' and doc['_id'] not in failed: # print "Doing", doc['_id'] filename = doc['_id'] + '.jpg' outfile = open(filename, "w") try: att = db.get_attachment(doc['_id'], 'original.jpg') shutil.copyfileobj(att, outfile) att.close() outfile.close() doc['exif'] = photoutils.getExifData(filename) # print doc docs.append(doc) finally: os.unlink(filename) try: db.update(docs) except: import traceback traceback.print_exc() print "Switching to one at a time." for d in docs: try: db.update([d])