From d649dc54a3b3ed710cc140e77e75b760ed4ec927 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 23 Oct 2014 23:29:44 +0200 Subject: [PATCH] Blendfile now downloads zip --- gui/browser.py | 39 ++++++++++++---- webservice/bam/application/__init__.py | 65 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/gui/browser.py b/gui/browser.py index 00bc07e..d93b47a 100755 --- a/gui/browser.py +++ b/gui/browser.py @@ -73,11 +73,15 @@ class PathHandle: return r.json() @staticmethod - def download_from_server(path, idname): + def download_from_server(path, idname, file_type, local_filename=None): import requests payload = {"filepath": "/".join(path) + "/" + idname} - r = requests.get(PathHandle.request_url("file"), params=payload, auth=("bam", "bam"), stream=True) + r = requests.get(PathHandle.request_url(file_type), params=payload, auth=("bam", "bam"), stream=True) local_filename = payload['filepath'].split('/')[-1] + + if file_type == "file_deps": + local_filename += ".zip" + with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks @@ -101,7 +105,7 @@ class Application(tk.Frame): self.vsb.pack(side="right", fill="y") self.canvas.pack(side="left", fill="both", expand=True) - self.canvas.create_window((4, 4), window=self.frame, anchor="nw", + self.canvas.create_window((4, 4), window=self.frame, anchor="nw", tags="self.frame") self.frame.bind("", self.OnFrameConfigure) @@ -141,11 +145,12 @@ class Application(tk.Frame): self.repopulate() def exec_path_file(idname): - self.path_handle.download_from_server(self.path_handle.path, idname) - print(self.path_handle.path) - + self.path_handle.download_from_server(self.path_handle.path, idname, "file") self.repopulate() + def exec_path_blendfile(idname): + self.path_handle.download_from_server(self.path_handle.path, idname, "file_deps") + self.repopulate() js = self.path_handle.json items = js.get("items_list") @@ -177,16 +182,30 @@ class Application(tk.Frame): self.grid_members.append(but) row += 1 +# +# for name_short, name_full, item_type in items: +# print(name_short, name_full, item_type) +# if item_type == "file": +# but = tk.Label(self.frame, text="(f)", width=3, borderwidth="1", relief="solid") +# but.grid(row=row, column=0) +# def fn(idname=name_short): exec_path_file(idname) +# self.grid_members.append(but) +# +# but = tk.Button(self.frame, text=name_short, fg="blue", command=fn) +# but.grid(row=row, column=1, sticky="nw") +# del fn +# +# self.grid_members.append(but) +# row += 1 for name_short, name_full, item_type in items: print(name_short, name_full, item_type) - if item_type in {"file", "blendfile"}: - but = tk.Label(self.frame, text="(f)", width=3, borderwidth="1", relief="solid") + if item_type == "file": + but = tk.Label(self.frame, text="(b)", width=3, borderwidth="1", relief="solid") but.grid(row=row, column=0) - def fn(idname=name_short): exec_path_file(idname) + def fn(idname=name_short): exec_path_blendfile(idname) self.grid_members.append(but) - but = tk.Button(self.frame, text=name_short, fg="blue", command=fn) but.grid(row=row, column=1, sticky="nw") del fn diff --git a/webservice/bam/application/__init__.py b/webservice/bam/application/__init__.py index c0313e7..848f627 100644 --- a/webservice/bam/application/__init__.py +++ b/webservice/bam/application/__init__.py @@ -114,5 +114,70 @@ class FileAPI(Resource): return Response(f, direct_passthrough=True) +class FileDepsAPI(Resource): + """Downloads a file and its deps.""" + + decorators = [auth.login_required] + + def __init__(self): + parser = reqparse.RequestParser() + parser.add_argument('filepath', type=str, required=True, + help="Filepath cannot be blank!") + args = parser.parse_args() + + super(FileDepsAPI, self).__init__() + + def get(self): + filepath = os.path.join(app.config['STORAGE_PATH'], request.args['filepath']) + + # pack the file! + print("PACKING") + filepath_zip = self.pack_fn(filepath) + + # TODO, handle fail + if filepath_zip is None: + pass + + f = open(filepath_zip, 'rb') + return Response(f, direct_passthrough=True) + + @staticmethod + def pack_fn(filepath): + import tempfile + filepath_zip = tempfile.mkstemp(suffix=".zip") + + import os + modpath = \ + os.path.normpath( + os.path.abspath( + os.path.join( + os.path.dirname(__file__), + "..", + "..", + "..", + "packer"))) + + import sys + if modpath not in sys.path: + sys.path.append(modpath) + del modpath + + import packer + + print("AAA", filepath) + print("NNN", filepath_zip) + + try: + packer.pack(filepath.encode('utf-8'), filepath_zip[-1].encode('utf-8'), mode='ZIP') + return filepath_zip[-1] + except: + import traceback + traceback.print_exc() + + return None + + + api.add_resource(FilesListAPI, '/file_list', endpoint='file_list') api.add_resource(FileAPI, '/file', endpoint='file') +api.add_resource(FileDepsAPI, '/file_deps', endpoint='file_deps')