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,20 +60,23 @@ 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)
# Save the file to a temporary location # if user does not select file, browser also
with tempfile.TemporaryDirectory() as temp_dir: # submit a empty part without filename
temp_path = os.path.join(temp_dir, filename) if file.name == '':
file.save(temp_path) return {'error': 'No selected file'}, 400
# Open the file in binary mode else:
with open(temp_path, 'rb') as filecontent: # Save the file to a temporary location
result = upload_storj(filecontent, filename, 'discoursio') with tempfile.TemporaryDirectory() as temp_dir:
return json({'message': 'File uploaded'}, status=200) temp_path = os.path.join(temp_dir, filename)
file.save(temp_path)
# Open the file in binary mode
with open(temp_path, 'rb') as filecontent:
result = upload_storj(filecontent, filename, 'discoursio')
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() method: 'POST',
reader.onloadend = () => { body: formData,
f.fileData = reader.result headers: {
const body = new FormData() 'Content-Type': 'multipart/form-data; boundary=discoursiofile'
body.append('file', f)
fetch('/api/upload', {
method: 'POST',
body
}).then((resp) => {
resp.json().then((url) => updateFormField('file', url))
})
} }
reader.readAsDataURL(f) })
} console.debug(response)
} 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))