This repository has been archived on 2023-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-asset-manager/webservice/bam/application/__init__.py

119 lines
3.5 KiB
Python
Raw Normal View History

2014-10-16 16:10:25 +02:00
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
2014-10-23 14:45:21 +02:00
import os
2014-10-23 19:51:52 +02:00
from flask import Flask, jsonify, abort, request, make_response, url_for, Response
2014-10-16 16:10:25 +02:00
from flask.views import MethodView
from flask.ext.restful import Api, Resource, reqparse, fields, marshal
from flask.ext.httpauth import HTTPBasicAuth
app = Flask(__name__)
api = Api(app)
auth = HTTPBasicAuth()
2014-10-23 14:45:21 +02:00
import config
app.config.from_object(config.Development)
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
@api.representation('application/octet-stream')
def output_file(data, code, headers=None):
"""Makes a Flask response to return a file."""
resp = make_response(data, code)
resp.headers.extend(headers or {})
return resp
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
@auth.get_password
def get_password(username):
if username == 'bam':
return 'bam'
return None
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
@auth.error_handler
def unauthorized():
2014-10-17 09:33:16 +02:00
return make_response(jsonify({'message': 'Unauthorized access'}), 403)
# return 403 instead of 401 to prevent browsers from displaying
2014-10-16 16:10:25 +02:00
# the default auth dialog
class FilesListAPI(Resource):
"""Displays list of files."""
2014-10-23 19:51:52 +02:00
2014-10-16 16:10:25 +02:00
decorators = [auth.login_required]
def __init__(self):
2014-10-23 14:45:21 +02:00
parser = reqparse.RequestParser()
#parser.add_argument('rate', type=int, help='Rate cannot be converted')
parser.add_argument('path', type=str)
args = parser.parse_args()
2014-10-16 16:10:25 +02:00
super(FilesListAPI, self).__init__()
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
def get(self):
2014-10-23 14:45:21 +02:00
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)
2014-10-23 19:51:52 +02:00
if os.path.isdir(absolute_path):
2014-10-23 14:45:21 +02:00
items_list.append((f, relative_path, 'folder'))
2014-10-23 19:51:52 +02:00
else:
items_list.append((f, relative_path, 'file'))
2014-10-23 14:45:21 +02:00
project_files = dict(
parent_path=parent_path,
items_list=items_list)
return jsonify(project_files)
#return {'message': 'Display files list'}
2014-10-16 16:10:25 +02:00
class FileAPI(Resource):
"""Downloads a file."""
decorators = [auth.login_required]
2014-10-17 09:33:16 +02:00
2014-10-16 16:10:25 +02:00
def __init__(self):
2014-10-23 14:45:21 +02:00
parser = reqparse.RequestParser()
2014-10-23 19:51:52 +02:00
parser.add_argument('filepath', type=str, required=True,
help="Filepath cannot be blank!")
2014-10-23 14:45:21 +02:00
args = parser.parse_args()
2014-10-23 19:51:52 +02:00
2014-10-16 16:10:25 +02:00
super(FileAPI, self).__init__()
2014-10-23 14:45:21 +02:00
def get(self):
2014-10-23 22:28:45 +02:00
filepath = os.path.join(app.config['STORAGE_PATH'], request.args['filepath'])
2014-10-23 19:51:52 +02:00
f = open(filepath, 'rb')
return Response(f, direct_passthrough=True)
2014-10-16 16:10:25 +02:00
2014-10-23 22:28:45 +02:00
api.add_resource(FilesListAPI, '/file_list', endpoint='file_list')
2014-10-23 14:45:21 +02:00
api.add_resource(FileAPI, '/file', endpoint='file')