File upload on PUT requests

This commit is contained in:
2014-10-30 16:47:03 +01:00
parent 57b40a7dcb
commit b1ac405ef5
3 changed files with 56 additions and 8 deletions

View File

@@ -17,18 +17,43 @@ def request_url(path):
#r = requests.get(request_url('/files'), params=payload, auth=('bam', 'bam'))
#print (r.json())
payload = {
'filepath': 'shots',
'command' : 'info',
}
# payload = {
# 'filepath': 'shots',
# 'command' : 'info',
# }
r = requests.get(request_url('/file'), params=payload, auth=('bam', 'bam'), stream=True)
local_filename = payload['filepath'].split('/')[-1]
# r = requests.get(request_url('/file'), params=payload, auth=('bam', 'bam'), stream=True)
# local_filename = payload['filepath'].split('/')[-1]
print (r.json())
# print (r.json())
# 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)
# 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)

View File

@@ -20,6 +20,7 @@
import os
import svn.local
import pprint
import werkzeug
from flask import Flask, jsonify, abort, request, make_response, url_for, Response
from flask.views import MethodView
@@ -114,10 +115,12 @@ class FileAPI(Resource):
def __init__(self):
parser = reqparse.RequestParser()
parser.add_argument('filepath', type=str, required=True,
parser.add_argument('filepath', type=str,
help="Filepath cannot be blank!")
parser.add_argument('command', type=str, required=True,
help="Command cannot be blank!")
parser.add_argument('files', type=werkzeug.datastructures.FileStorage,
location='files')
args = parser.parse_args()
super(FileAPI, self).__init__()
@@ -152,6 +155,19 @@ class FileAPI(Resource):
else:
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
def pack_fn(filepath):
@@ -188,6 +204,11 @@ class FileAPI(Resource):
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(FileAPI, '/file', endpoint='file')

View File

@@ -3,3 +3,5 @@ class Config(object):
class Development(Config):
STORAGE_PATH='/Volumes/PROJECTS/storage'
UPLOAD_FOLDER = '/Volumes/PROJECTS/storage_staging'
ALLOWED_EXTENSIONS = set(['txt', 'mp4', 'png', 'jpg', 'jpeg', 'gif', 'blend', 'zip'])