def zip_album(h): a = Album.from_hash(h) paths = map(lambda f: file_storage(f.original), a.items) zip_path = file_storage(h + ".zip") if os.path.exists(zip_path): return call(["zip", "-j", zip_path] + paths) a.metadata = json.dumps({"has_zip": True}) a.save()
def upload(f, filename): if not f.content_type: f.content_type = get_mimetype(filename) or "application/octet-stream" #if f.content_type.split("/")[0] not in ['video', 'image', 'audio']: # return "no", 415 ignore_limit = current_app.debug or r.sismember(_k("whitelisted_ips"), get_ip()) if not ignore_limit: rate_limit_update(file_length(f)) if rate_limit_exceeded(): return None, 420 h = get_hash(f) identifier = to_id(h) if "." not in filename: ext = mimetypes.guess_extension( f.content_type)[1:] # This not very scientific, but it works else: ext = extension(filename) filename = "%s.%s" % (identifier, ext) path = tempfile.NamedTemporaryFile( suffix="." + ext).name # Fix for imagemagick's silliness if os.path.exists(file_storage(filename)): if File.exists(identifier): return identifier, 409 else: # Delete residual files from storage by creating a dummy File dummy = File(original=filename) dummy.delete = lambda: None # nop delete_file(dummy) f.seek(0) # Otherwise it'll write a 0-byte file f.save(path) file_object = File(hash=identifier) file_object.compression = os.path.getsize(path) file_object.original = filename file_object.mimetype = f.content_type file_object.ip = secure_ip() result = process_file.delay(path, identifier, ignore_limit) file_object.taskid = result.id file_object.save() return identifier, 200
def album_zip(self): if "hash" not in request.form: return {"error": 415}, 415 h = request.form["hash"] klass = RedisObject.klass(h) if not klass or klass is not Album: return {"error": 404}, 404 if os.path.exists(file_storage(h) + ".zip"): return {"status": "done"} zip_album.delay(h) return {"status": "success"}
def album_zip(self): if "hash" not in request.form: return {"error": 415}, 415 h = request.form["hash"] klass = RedisObject.klass(h) if not klass or klass is not Album: return {"error": 404}, 404 if os.path.exists(file_storage(h) + ".zip"): return {"status": "done"} zip_album.delay(h) return {"status": "success"}
def upload(f, filename): if not f.content_type: f.content_type = get_mimetype(filename) or "application/octet-stream" #if f.content_type.split("/")[0] not in ['video', 'image', 'audio']: # return "no", 415 ignore_limit = current_app.debug or r.sismember(_k("whitelisted_ips"), get_ip()) if not ignore_limit: rate_limit_update(file_length(f)) if rate_limit_exceeded(): return None, 420 h = get_hash(f) identifier = to_id(h) if "." not in filename: ext = mimetypes.guess_extension(f.content_type)[1:] # This not very scientific, but it works else: ext = extension(filename) filename = "%s.%s" % (identifier, ext) path = tempfile.NamedTemporaryFile(suffix="." + ext).name # Fix for imagemagick's silliness if os.path.exists(file_storage(filename)): if File.exists(identifier): return identifier, 409 else: # Delete residual files from storage by creating a dummy File dummy = File(original=filename) dummy.delete = lambda: None # nop delete_file(dummy) f.seek(0) # Otherwise it'll write a 0-byte file f.save(path) file_object = File(hash=identifier) file_object.compression = os.path.getsize(path) file_object.original = filename file_object.mimetype = f.content_type file_object.ip = secure_ip() result = process_file.delay(path, identifier, ignore_limit) file_object.taskid = result.id file_object.save() return identifier, 200
if __name__ == '__main__': files = File.get_all() count = len(files) print "About to process %d files." % count done = 0 errors = [] for f in files: h = f.hash configvector = 0 try: path = file_storage(f.original) result = detect(path) if result and result['flags']: bv = BitVector(flags_per_processor.get(result['type'], [])) for flag, value in result['flags'].items(): setattr(bv, flag, value) configvector = int(bv) print h, result['type'], int(bv) done += 1 except Exception, e: errors.append(h) k = _k("file.%s" % h)