This commit is contained in:
tonyrewin 2023-01-28 03:31:46 +03:00
parent e280709697
commit d2100cc865
2 changed files with 36 additions and 31 deletions

View File

@ -60,15 +60,16 @@ def upload_storj(filecontent, filename, bucket_name):
def upload(): def upload():
# check if the post request has the file part # check if the post request has the file part
if 'file' not in request.files: if 'file' not in request.files:
return json({'error': 'No file part'}, status=400) return {'error': 'No file part'}, 400
file = request.files.get('file') 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: if file:
# save the file # save the file
filename = secure_filename(file.name) filename = secure_filename(file.name)
# if user does not select file, browser also
# submit a empty part without filename
if file.name == '':
return {'error': 'No selected file'}, 400
else:
# Save the file to a temporary location # Save the file to a temporary location
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
temp_path = os.path.join(temp_dir, filename) temp_path = os.path.join(temp_dir, filename)
@ -76,4 +77,6 @@ def upload():
# Open the file in binary mode # Open the file in binary mode
with open(temp_path, 'rb') as filecontent: with open(temp_path, 'rb') as filecontent:
result = upload_storj(filecontent, filename, 'discoursio') result = upload_storj(filecontent, filename, 'discoursio')
return json({'message': 'File uploaded'}, status=200) else:
return {'error': 'No selected file'}, 400
return {'message': 'File uploaded', 'result': jsonify(result)}, 200

View File

@ -27,30 +27,32 @@ export const ProfileSettingsPage = (props: PageProps) => {
submit(form) submit(form)
} }
let userpicFile: HTMLInputElement let userpicFile: HTMLInputElement
const handleUserpicUpload = async (ev) => { const handleFileUpload = async (file: File) => {
// TODO: show progress
try { try {
console.debug('handleUserpicUpload trigger') const formData = new FormData()
const f = ev.target.files[0] formData.append('file', file)
if (f) { const response = await fetch('/api/upload', {
const reader = new FileReader()
reader.onloadend = () => {
f.fileData = reader.result
const body = new FormData()
body.append('file', f)
fetch('/api/upload', {
method: 'POST', method: 'POST',
body body: formData,
}).then((resp) => { headers: {
resp.json().then((url) => updateFormField('file', url)) 'Content-Type': 'multipart/form-data; boundary=discoursiofile'
}
}) })
} console.debug(response)
reader.readAsDataURL(f)
}
} catch (error) { } catch (error) {
console.error('[upload] error', error) console.error('[upload] error', error)
} }
} }
const handleUserpicUpload = async (ev) => {
// TODO: show progress
try {
const f = ev.target.files[0]
if (f) handleFileUpload(f)
} catch (error) {
console.error('[upload] error', error)
}
}
const [hostname, setHostname] = createSignal('new.discours.io') const [hostname, setHostname] = createSignal('new.discours.io')
onMount(() => setHostname(window?.location.host)) onMount(() => setHostname(window?.location.host))