File tree Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Expand file tree Collapse file tree 2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -19,12 +19,12 @@ async function addFile () {
19
19
loading .value = true
20
20
21
21
try {
22
+ const formData = new FormData ()
23
+ formData .append (' data' , new Blob ([JSON .stringify ({ key })], {type: ' application/json' }))
24
+ formData .append (' file' , newFileValue .value )
22
25
const file = await $fetch (' /api/storage' , {
23
26
method: ' PUT' ,
24
- body: {
25
- key,
26
- value
27
- }
27
+ body: formData
28
28
})
29
29
const fileIndex = storage .value .findIndex (e => e .key === file .key )
30
30
if (fileIndex !== - 1 ) {
Original file line number Diff line number Diff line change
1
+ import type { MultiPartData } from 'h3'
2
+
3
+ export default eventHandler ( async ( event ) => {
4
+ await requireUserSession ( event )
5
+
6
+ const form : MultiPartData [ ] = await readMultipartFormData ( event )
7
+ const dataPart = form . find ( ( part ) => part . name === 'data' )
8
+ const filePart = form . find ( ( part ) => part . name === 'file' )
9
+ if ( ! dataPart || ! filePart ) {
10
+ throw createError ( `Missing ${ ! dataPart ? 'data' : 'file' } body param.` )
11
+ }
12
+
13
+ try {
14
+ const data = JSON . parse ( dataPart . data )
15
+ const file = filePart . data . toString ( )
16
+
17
+ // Set entry for the current user
18
+
19
+ const res = await useBucket ( ) . put ( data . key , file , {
20
+ customMetadata : {
21
+ filename : filePart . filename ! ,
22
+ type : filePart . type !
23
+ }
24
+ } )
25
+ return res
26
+ } catch ( e : any ) {
27
+ throw createError ( {
28
+ statusCode : 500 ,
29
+ message : `Storage error: ${ e . message } `
30
+ } )
31
+ }
32
+ } )
You can’t perform that action at this time.
0 commit comments