2020-04-23 10:39:35 -07:00
|
|
|
import os
|
|
|
|
import base64
|
|
|
|
import io
|
|
|
|
import binascii
|
|
|
|
import zipfile
|
|
|
|
from uuid import uuid4
|
|
|
|
|
2020-07-13 09:52:34 +02:00
|
|
|
from flask import after_this_request, send_from_directory, Blueprint, current_app
|
|
|
|
from flask_restful import Resource, reqparse, abort, request, url_for, Api
|
2020-04-23 10:39:35 -07:00
|
|
|
from cerberus import Validator
|
|
|
|
from werkzeug.datastructures import FileStorage
|
2020-07-13 08:01:07 +02:00
|
|
|
from flasgger import swag_from
|
|
|
|
|
2020-04-23 10:39:35 -07:00
|
|
|
|
|
|
|
from matweb import file_removal_scheduler, utils
|
|
|
|
|
|
|
|
|
2020-07-13 16:01:32 +02:00
|
|
|
api_bp = Blueprint('api_bp', __name__)
|
|
|
|
api = Api(api_bp, prefix='/api')
|
2020-07-13 09:52:34 +02:00
|
|
|
|
2020-07-13 08:01:07 +02:00
|
|
|
|
2020-07-13 09:52:34 +02:00
|
|
|
class APIUpload(Resource):
|
2020-07-13 08:01:07 +02:00
|
|
|
@swag_from('./oas/upload.yml')
|
2020-04-23 10:39:35 -07:00
|
|
|
def post(self):
|
2020-07-13 09:52:34 +02:00
|
|
|
utils.check_upload_folder(current_app.config['UPLOAD_FOLDER'])
|
2020-04-23 10:39:35 -07:00
|
|
|
req_parser = reqparse.RequestParser()
|
|
|
|
req_parser.add_argument('file_name', type=str, required=True, help='Post parameter is not specified: file_name')
|
|
|
|
req_parser.add_argument('file', type=str, required=True, help='Post parameter is not specified: file')
|
|
|
|
|
|
|
|
args = req_parser.parse_args()
|
|
|
|
try:
|
|
|
|
file_data = base64.b64decode(args['file'])
|
2020-05-08 09:10:18 -07:00
|
|
|
except (binascii.Error, ValueError):
|
|
|
|
abort(400, message='Failed decoding file')
|
2020-04-23 10:39:35 -07:00
|
|
|
|
|
|
|
file = FileStorage(stream=io.BytesIO(file_data), filename=args['file_name'])
|
2020-05-08 09:10:18 -07:00
|
|
|
try:
|
2020-07-13 09:52:34 +02:00
|
|
|
filename, filepath = utils.save_file(file, current_app.config['UPLOAD_FOLDER'])
|
2020-05-08 09:10:18 -07:00
|
|
|
except ValueError:
|
|
|
|
abort(400, message='Invalid Filename')
|
|
|
|
|
2020-04-23 10:39:35 -07:00
|
|
|
parser, mime = utils.get_file_parser(filepath)
|
|
|
|
|
|
|
|
if parser is None:
|
|
|
|
abort(415, message='The type %s is not supported' % mime)
|
|
|
|
|
|
|
|
meta = parser.get_meta()
|
|
|
|
if not parser.remove_all():
|
|
|
|
abort(500, message='Unable to clean %s' % mime)
|
|
|
|
|
2020-07-13 09:52:34 +02:00
|
|
|
key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
|
2020-04-23 10:39:35 -07:00
|
|
|
return utils.return_file_created_response(
|
2020-05-15 14:38:13 +02:00
|
|
|
utils.get_file_removal_max_age_sec(),
|
2020-04-23 10:39:35 -07:00
|
|
|
output_filename,
|
|
|
|
mime,
|
|
|
|
key,
|
2020-04-26 09:50:14 -07:00
|
|
|
secret,
|
2020-04-23 10:39:35 -07:00
|
|
|
meta,
|
|
|
|
meta_after,
|
2020-06-26 16:51:53 +02:00
|
|
|
url_for(
|
2020-07-13 09:52:34 +02:00
|
|
|
'api_bp.apidownload',
|
2020-06-26 16:51:53 +02:00
|
|
|
key=key,
|
|
|
|
secret=secret,
|
|
|
|
filename=output_filename,
|
|
|
|
_external=True
|
|
|
|
)
|
2020-07-13 08:34:18 +02:00
|
|
|
), 201
|
2020-04-23 10:39:35 -07:00
|
|
|
|
|
|
|
|
|
|
|
class APIDownload(Resource):
|
2020-07-13 08:14:47 +02:00
|
|
|
@swag_from('./oas/download.yml')
|
2020-04-26 09:50:14 -07:00
|
|
|
def get(self, key: str, secret: str, filename: str):
|
2020-07-13 09:52:34 +02:00
|
|
|
complete_path, filepath = utils.is_valid_api_download_file(filename, key, secret, current_app.config['UPLOAD_FOLDER'])
|
2020-04-23 10:39:35 -07:00
|
|
|
# Make sure the file is NOT deleted on HEAD requests
|
|
|
|
if request.method == 'GET':
|
2020-07-13 09:52:34 +02:00
|
|
|
file_removal_scheduler.run_file_removal_job(current_app.config['UPLOAD_FOLDER'])
|
2020-04-23 10:39:35 -07:00
|
|
|
|
|
|
|
@after_this_request
|
|
|
|
def remove_file(response):
|
|
|
|
if os.path.exists(complete_path):
|
|
|
|
os.remove(complete_path)
|
|
|
|
return response
|
|
|
|
|
2020-07-13 09:52:34 +02:00
|
|
|
return send_from_directory(current_app.config['UPLOAD_FOLDER'], filepath, as_attachment=True)
|
2020-04-23 10:39:35 -07:00
|
|
|
|
|
|
|
|
2020-08-21 09:09:19 +02:00
|
|
|
class APIClean(Resource):
|
|
|
|
@swag_from('./oas/remove_metadata.yml')
|
|
|
|
def post(self):
|
|
|
|
if 'file' not in request.files:
|
|
|
|
abort(400, message='No file part')
|
|
|
|
|
|
|
|
uploaded_file = request.files['file']
|
|
|
|
if not uploaded_file.filename:
|
|
|
|
abort(400, message='No selected `file`')
|
|
|
|
try:
|
|
|
|
filename, filepath = utils.save_file(uploaded_file, current_app.config['UPLOAD_FOLDER'])
|
|
|
|
except ValueError:
|
|
|
|
abort(400, message='Invalid Filename')
|
|
|
|
|
|
|
|
parser, mime = utils.get_file_parser(filepath)
|
|
|
|
|
|
|
|
if parser is None:
|
|
|
|
abort(415, message='The type %s is not supported' % mime)
|
|
|
|
|
|
|
|
if parser.remove_all() is not True:
|
|
|
|
abort(500, message='Unable to clean %s' % mime)
|
|
|
|
|
|
|
|
_, _, _, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
|
|
|
|
|
|
|
|
@after_this_request
|
|
|
|
def remove_file(response):
|
|
|
|
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], output_filename))
|
|
|
|
return response
|
|
|
|
|
|
|
|
return send_from_directory(current_app.config['UPLOAD_FOLDER'], output_filename, as_attachment=True)
|
|
|
|
|
|
|
|
|
2020-04-23 10:39:35 -07:00
|
|
|
class APIBulkDownloadCreator(Resource):
|
|
|
|
schema = {
|
|
|
|
'download_list': {
|
|
|
|
'type': 'list',
|
|
|
|
'minlength': 2,
|
|
|
|
'maxlength': int(os.environ.get('MAT2_MAX_FILES_BULK_DOWNLOAD', 10)),
|
|
|
|
'schema': {
|
|
|
|
'type': 'dict',
|
|
|
|
'schema': {
|
|
|
|
'key': {'type': 'string', 'required': True},
|
2020-04-26 09:50:14 -07:00
|
|
|
'secret': {'type': 'string', 'required': True},
|
2020-04-23 10:39:35 -07:00
|
|
|
'file_name': {'type': 'string', 'required': True}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v = Validator(schema)
|
|
|
|
|
2020-07-13 08:34:18 +02:00
|
|
|
@swag_from('./oas/bulk.yml')
|
2020-04-23 10:39:35 -07:00
|
|
|
def post(self):
|
2020-07-13 09:52:34 +02:00
|
|
|
utils.check_upload_folder(current_app.config['UPLOAD_FOLDER'])
|
2020-04-23 10:39:35 -07:00
|
|
|
data = request.json
|
2020-07-14 22:49:24 +02:00
|
|
|
if not data:
|
|
|
|
abort(400, message="Post Body Required")
|
2020-04-23 10:39:35 -07:00
|
|
|
if not self.v.validate(data):
|
|
|
|
abort(400, message=self.v.errors)
|
|
|
|
# prevent the zip file from being overwritten
|
|
|
|
zip_filename = 'files.' + str(uuid4()) + '.zip'
|
2020-07-13 09:52:34 +02:00
|
|
|
zip_path = os.path.join(current_app.config['UPLOAD_FOLDER'], zip_filename)
|
2020-04-23 10:39:35 -07:00
|
|
|
cleaned_files_zip = zipfile.ZipFile(zip_path, 'w')
|
|
|
|
with cleaned_files_zip:
|
|
|
|
for file_candidate in data['download_list']:
|
|
|
|
complete_path, file_path = utils.is_valid_api_download_file(
|
|
|
|
file_candidate['file_name'],
|
|
|
|
file_candidate['key'],
|
2020-04-26 09:50:14 -07:00
|
|
|
file_candidate['secret'],
|
2020-07-13 09:52:34 +02:00
|
|
|
current_app.config['UPLOAD_FOLDER']
|
2020-04-23 10:39:35 -07:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
cleaned_files_zip.write(complete_path)
|
|
|
|
os.remove(complete_path)
|
|
|
|
except ValueError:
|
|
|
|
abort(400, message='Creating the archive failed')
|
|
|
|
|
|
|
|
try:
|
|
|
|
cleaned_files_zip.testzip()
|
|
|
|
except ValueError as e:
|
|
|
|
abort(400, message=str(e))
|
|
|
|
|
|
|
|
parser, mime = utils.get_file_parser(zip_path)
|
|
|
|
if not parser.remove_all():
|
|
|
|
abort(500, message='Unable to clean %s' % mime)
|
2020-07-13 09:52:34 +02:00
|
|
|
key, secret, meta_after, output_filename = utils.cleanup(parser, zip_path, current_app.config['UPLOAD_FOLDER'])
|
2020-04-23 10:39:35 -07:00
|
|
|
return {
|
2020-05-15 14:38:13 +02:00
|
|
|
'inactive_after_sec': utils.get_file_removal_max_age_sec(),
|
2020-04-23 10:39:35 -07:00
|
|
|
'output_filename': output_filename,
|
|
|
|
'mime': mime,
|
|
|
|
'key': key,
|
2020-04-26 09:50:14 -07:00
|
|
|
'secret': secret,
|
2020-04-23 10:39:35 -07:00
|
|
|
'meta_after': meta_after,
|
2020-06-26 16:51:53 +02:00
|
|
|
'download_link': url_for(
|
2020-07-13 09:52:34 +02:00
|
|
|
'api_bp.apidownload',
|
2020-06-26 16:51:53 +02:00
|
|
|
key=key,
|
|
|
|
secret=secret,
|
|
|
|
filename=output_filename,
|
|
|
|
_external=True
|
2020-04-26 09:50:14 -07:00
|
|
|
)
|
2020-04-23 10:39:35 -07:00
|
|
|
}, 201
|
|
|
|
|
|
|
|
|
|
|
|
class APISupportedExtensions(Resource):
|
2020-07-13 08:21:08 +02:00
|
|
|
@swag_from('./oas/extension.yml')
|
2020-04-23 10:39:35 -07:00
|
|
|
def get(self):
|
|
|
|
return utils.get_supported_extensions()
|
2020-07-13 09:52:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
api.add_resource(
|
|
|
|
APIUpload,
|
|
|
|
'/upload'
|
|
|
|
)
|
|
|
|
api.add_resource(
|
|
|
|
APIDownload,
|
|
|
|
'/download/<string:key>/<string:secret>/<string:filename>'
|
|
|
|
)
|
2020-08-21 09:09:19 +02:00
|
|
|
api.add_resource(
|
|
|
|
APIClean,
|
|
|
|
'/remove_metadata'
|
|
|
|
)
|
2020-07-13 09:52:34 +02:00
|
|
|
api.add_resource(
|
|
|
|
APIBulkDownloadCreator,
|
|
|
|
'/download/bulk'
|
|
|
|
)
|
|
|
|
api.add_resource(APISupportedExtensions, '/extension')
|