diff --git a/api/upload.py b/api/upload.py index 1fabadee..30c9a264 100644 --- a/api/upload.py +++ b/api/upload.py @@ -58,19 +58,22 @@ def upload_storj(filecontent, filename, bucket_name): @app.route('/api/upload', methods=['post']) def upload(): - print('upload serverless route is fine') - print(request.path) - print(request.form) - print(request.files) - img = request.files['userpic'] - if img: - # Perform the file upload - filename = secure_filename(img.filename) + # check if the post request has the file part + if 'file' not in request.files: + return json({'error': 'No file part'}, status=400) + file = request.files.get('file') + # if user does not select file, browser also + # submit a empty part without filename + if file.name == '': + return json({'error': 'No selected file'}, status=400) + if file: + # save the file + filename = secure_filename(file.name) # Save the file to a temporary location with tempfile.TemporaryDirectory() as temp_dir: temp_path = os.path.join(temp_dir, filename) - img.save(temp_path) + file.save(temp_path) # Open the file in binary mode with open(temp_path, 'rb') as filecontent: - return jsonify(upload_storj(filecontent, filename, 'discoursio')) - return + result = upload_storj(filecontent, filename, 'discoursio') + return json({'message': 'File uploaded'}, status=200) diff --git a/src/components/Pages/profile/ProfileSettingsPage.tsx b/src/components/Pages/profile/ProfileSettingsPage.tsx index fcfcb54c..e98e684f 100644 --- a/src/components/Pages/profile/ProfileSettingsPage.tsx +++ b/src/components/Pages/profile/ProfileSettingsPage.tsx @@ -36,13 +36,13 @@ export const ProfileSettingsPage = (props: PageProps) => { const reader = new FileReader() reader.onloadend = () => { f.fileData = reader.result - const data = new FormData() - data.append('file', f) + const body = new FormData() + body.append('file', f) fetch('/api/upload', { method: 'POST', - body: data + body }).then((resp) => { - resp.json().then((url) => updateFormField('userpic', url)) + resp.json().then((url) => updateFormField('file', url)) }) } reader.readAsDataURL(f) @@ -81,8 +81,7 @@ export const ProfileSettingsPage = (props: PageProps) => { diff --git a/vercel.json b/vercel.json index c761bf8c..18211c71 100644 --- a/vercel.json +++ b/vercel.json @@ -1,12 +1,9 @@ { - "functions": { - "api/upload.py": { - "memory": 3008, - "maxDuration": 30 - }, - "api/feedback.js": { - "memory": 3008, - "maxDuration": 30 + "routes": [ + { + "src": "/api/upload", + "headers": { "Content-Type": "application/json" }, + "dest": "api/upload.py" } - } + ] }