def save_database_as_zip(): app = flask.current_app if not app.config.get("DEBUG", False): flask.abort(404) database_dump = dump_database(auth=False).data user_photos_dump = dump_user_photos(auth=False) # create temporary files temp = ppath(tempfile.mkdtemp()) database_path = flask.safe_join(temp, "database") os.makedirs(database_path) with open(flask.safe_join(database_path, "dump.json"), "w+") as f: f.write(database_dump) with open(flask.safe_join(database_path, "user_photos.zip"), "w+") as f: f.write(user_photos_dump.data) archive_name = flask.safe_join(temp, "database") try: shutil.make_archive(archive_name, "zip", database_path) f = open(archive_name + ".zip") finally: temp.rmtree() return flask.Response(sugar.read_file(f), mimetype="application/x-zip-compressed")
def dump_user_photos(auth=True): if auth: _auth() app = flask.current_app user_photos_path = app.config['UPLOADED_PHOTOS_DEST'] temp = ppath(tempfile.mkdtemp()) archive_name = flask.safe_join(temp, "photos") try: shutil.make_archive(archive_name, "zip", user_photos_path) f = open(archive_name + ".zip") finally: temp.rmtree() return flask.Response(sugar.read_file(f), mimetype="application/x-zip-compressed")