File upload on PUT requests
This commit is contained in:
@@ -17,18 +17,43 @@ def request_url(path):
|
|||||||
#r = requests.get(request_url('/files'), params=payload, auth=('bam', 'bam'))
|
#r = requests.get(request_url('/files'), params=payload, auth=('bam', 'bam'))
|
||||||
#print (r.json())
|
#print (r.json())
|
||||||
|
|
||||||
payload = {
|
# payload = {
|
||||||
'filepath': 'shots',
|
# 'filepath': 'shots',
|
||||||
'command' : 'info',
|
# 'command' : 'info',
|
||||||
}
|
# }
|
||||||
|
|
||||||
r = requests.get(request_url('/file'), params=payload, auth=('bam', 'bam'), stream=True)
|
# r = requests.get(request_url('/file'), params=payload, auth=('bam', 'bam'), stream=True)
|
||||||
local_filename = payload['filepath'].split('/')[-1]
|
# local_filename = payload['filepath'].split('/')[-1]
|
||||||
|
|
||||||
print (r.json())
|
# print (r.json())
|
||||||
# with open(local_filename, 'wb') as f:
|
# with open(local_filename, 'wb') as f:
|
||||||
# for chunk in r.iter_content(chunk_size=1024):
|
# for chunk in r.iter_content(chunk_size=1024):
|
||||||
# if chunk: # filter out keep-alive new chunks
|
# if chunk: # filter out keep-alive new chunks
|
||||||
# f.write(chunk)
|
# f.write(chunk)
|
||||||
# f.flush()
|
# f.flush()
|
||||||
# print(local_filename)
|
# print(local_filename)
|
||||||
|
|
||||||
|
# filepath = 'yourfilename.txt'
|
||||||
|
# with open(filepath) as fh:
|
||||||
|
# mydata = fh.read()
|
||||||
|
# response = requests.put('https://api.elasticemail.com/attachments/upload',
|
||||||
|
# data=mydata,
|
||||||
|
# auth=('omer', 'b01ad0ce'),
|
||||||
|
# headers={'content-type':'text/plain'},
|
||||||
|
# params={'file': filepath}
|
||||||
|
# )
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
'command' : 'checkin',
|
||||||
|
}
|
||||||
|
|
||||||
|
files = {'file': open('buck.mp4', 'rb')}
|
||||||
|
#files = {'name': ('filename', (open('mytest.txt', 'rb')))}
|
||||||
|
|
||||||
|
r = requests.put(request_url('/file'),
|
||||||
|
params=payload,
|
||||||
|
auth=('bam', 'bam'),
|
||||||
|
files=files)
|
||||||
|
|
||||||
|
print(r.text)
|
||||||
|
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
import os
|
import os
|
||||||
import svn.local
|
import svn.local
|
||||||
import pprint
|
import pprint
|
||||||
|
import werkzeug
|
||||||
|
|
||||||
from flask import Flask, jsonify, abort, request, make_response, url_for, Response
|
from flask import Flask, jsonify, abort, request, make_response, url_for, Response
|
||||||
from flask.views import MethodView
|
from flask.views import MethodView
|
||||||
@@ -114,10 +115,12 @@ class FileAPI(Resource):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
parser = reqparse.RequestParser()
|
parser = reqparse.RequestParser()
|
||||||
parser.add_argument('filepath', type=str, required=True,
|
parser.add_argument('filepath', type=str,
|
||||||
help="Filepath cannot be blank!")
|
help="Filepath cannot be blank!")
|
||||||
parser.add_argument('command', type=str, required=True,
|
parser.add_argument('command', type=str, required=True,
|
||||||
help="Command cannot be blank!")
|
help="Command cannot be blank!")
|
||||||
|
parser.add_argument('files', type=werkzeug.datastructures.FileStorage,
|
||||||
|
location='files')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
super(FileAPI, self).__init__()
|
super(FileAPI, self).__init__()
|
||||||
@@ -152,6 +155,19 @@ class FileAPI(Resource):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
return jsonify(message='Command unknown')
|
return jsonify(message='Command unknown')
|
||||||
|
|
||||||
|
def put(self):
|
||||||
|
command = request.args['command']
|
||||||
|
file = request.files['file']
|
||||||
|
|
||||||
|
if file and self.allowed_file(file.filename):
|
||||||
|
filename = werkzeug.secure_filename(file.filename)
|
||||||
|
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||||||
|
|
||||||
|
return jsonify(message='Done')
|
||||||
|
else:
|
||||||
|
return jsonify(message='File not allowed')
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pack_fn(filepath):
|
def pack_fn(filepath):
|
||||||
@@ -188,6 +204,11 @@ class FileAPI(Resource):
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def allowed_file(filename):
|
||||||
|
return '.' in filename and \
|
||||||
|
filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
|
||||||
|
|
||||||
|
|
||||||
api.add_resource(FilesListAPI, '/file_list', endpoint='file_list')
|
api.add_resource(FilesListAPI, '/file_list', endpoint='file_list')
|
||||||
api.add_resource(FileAPI, '/file', endpoint='file')
|
api.add_resource(FileAPI, '/file', endpoint='file')
|
||||||
|
@@ -3,3 +3,5 @@ class Config(object):
|
|||||||
|
|
||||||
class Development(Config):
|
class Development(Config):
|
||||||
STORAGE_PATH='/Volumes/PROJECTS/storage'
|
STORAGE_PATH='/Volumes/PROJECTS/storage'
|
||||||
|
UPLOAD_FOLDER = '/Volumes/PROJECTS/storage_staging'
|
||||||
|
ALLOWED_EXTENSIONS = set(['txt', 'mp4', 'png', 'jpg', 'jpeg', 'gif', 'blend', 'zip'])
|
||||||
|
Reference in New Issue
Block a user