diff --git a/.gitignore b/.gitignore index 82130b9..5af9a44 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,9 @@ webservice/venv/ docs/venv/ docs/build/ +client/venv/ + + +client/client/config.json + +webservice/bam/config.py diff --git a/client/client/__init__.py b/client/client/__init__.py new file mode 100644 index 0000000..2770ebb --- /dev/null +++ b/client/client/__init__.py @@ -0,0 +1,24 @@ +import requests + +with open('config.json', 'r') as config: + import json + config = json.load(config) + +def request_url(path): + return ('%s%s' % (config['BAM_SERVER'], path)) + + +#payload = {'path': ''} +#r = requests.get(request_url('/files'), params=payload, auth=('bam', 'bam')) +#print (r.json()) + +payload = {'filepath': 'video.mp4'} +r = requests.get(request_url('/file'), params=payload, auth=('bam', 'bam'), stream=True) +local_filename = payload['filepath'].split('/')[-1] + +with open(local_filename, 'wb') as f: + for chunk in r.iter_content(chunk_size=1024): + if chunk: # filter out keep-alive new chunks + f.write(chunk) + f.flush() +print(local_filename) diff --git a/client/client/config.json.example.json b/client/client/config.json.example.json new file mode 100644 index 0000000..a12e2d4 --- /dev/null +++ b/client/client/config.json.example.json @@ -0,0 +1,3 @@ +{ + "BAM_SERVER" : "http://localhost:5000" +} diff --git a/webservice/bam/application/__init__.py b/webservice/bam/application/__init__.py index be0f2b9..6ce9c4b 100644 --- a/webservice/bam/application/__init__.py +++ b/webservice/bam/application/__init__.py @@ -16,6 +16,8 @@ # # ***** END GPL LICENCE BLOCK ***** +import os + from flask import Flask, jsonify, abort, request, make_response, url_for from flask.views import MethodView from flask.ext.restful import Api, Resource, reqparse, fields, marshal @@ -24,7 +26,8 @@ from flask.ext.httpauth import HTTPBasicAuth app = Flask(__name__) api = Api(app) auth = HTTPBasicAuth() - +import config +app.config.from_object(config.Development) @api.representation('application/octet-stream') def output_file(data, code, headers=None): @@ -53,10 +56,44 @@ class FilesListAPI(Resource): decorators = [auth.login_required] def __init__(self): + parser = reqparse.RequestParser() + #parser.add_argument('rate', type=int, help='Rate cannot be converted') + parser.add_argument('path', type=str) + args = parser.parse_args() super(FilesListAPI, self).__init__() def get(self): - return {'message': 'Display files list'} + + path = request.args['path'] + if not path: + path = '' + + absolute_path_root = app.config['STORAGE_PATH'] + parent_path = '' + + if path != '': + absolute_path_root = os.path.join(absolute_path_root, path) + parent_path = os.pardir + + items_list = [] + + for f in os.listdir(absolute_path_root): + relative_path = os.path.join(path, f) + absolute_path = os.path.join(absolute_path_root, f) + + # we are going to pick up only blend files and folders + if absolute_path.endswith('blend'): + # items[f] = relative_path + items_list.append((f, relative_path, 'blendfile')) + elif os.path.isdir(absolute_path): + items_list.append((f, relative_path, 'folder')) + + project_files = dict( + parent_path=parent_path, + items_list=items_list) + + return jsonify(project_files) + #return {'message': 'Display files list'} class FileAPI(Resource): @@ -65,18 +102,28 @@ class FileAPI(Resource): decorators = [auth.login_required] def __init__(self): - # self.reqparse = reqparse.RequestParser() - # self.reqparse.add_argument('path', - # type = str, - # location = 'json') + parser = reqparse.RequestParser() + #parser.add_argument('rate', type=int, help='Rate cannot be converted') + parser.add_argument('filepath', type=str) + args = parser.parse_args() + # parser = reqparse.RequestParser() + # parser.add_argument('filepath', type=str, required=True, + # help="Filepath cannot be blank!") + # args = parser.parse_args() super(FileAPI, self).__init__() - def get(self, path): - with open(path, 'r') as f: + def get(self): + filepath = os.path.join(app.config['STORAGE_PATH'], request.args['filepath']) + print (filepath) + with open(filepath, 'r') as f: body = f.read() + response = flask.make_response(body) + response.headers['content-type'] = 'application/octet-stream' + return response + return output_file(body, 200) # return {'path': path} api.add_resource(FilesListAPI, '/files', endpoint='files') -api.add_resource(FileAPI, '/file/', endpoint='file') +api.add_resource(FileAPI, '/file', endpoint='file') diff --git a/webservice/bam/config.py.example.py b/webservice/bam/config.py.example.py new file mode 100644 index 0000000..b68a893 --- /dev/null +++ b/webservice/bam/config.py.example.py @@ -0,0 +1,5 @@ +class Config(object): + DEBUG=True + +class Development(Config): + STORAGE_PATH='/Volumes/PROJECTS/storage'