feat: Add support for uploading media in admin (#7564)

This commit is contained in:
Stevche Radevski
2024-05-31 13:11:34 +02:00
committed by GitHub
parent 11528526fa
commit cec9af1b80
11 changed files with 106 additions and 20 deletions
+61
View File
@@ -314,4 +314,65 @@ export class Admin {
)
},
}
public uploads = {
// Note: The creation/upload flow be made more advanced, with support for streaming and progress, but for now we keep it simple
create: async (
body: HttpTypes.AdminUploadFile,
query?: SelectParams,
headers?: ClientHeaders
) => {
const form = new FormData()
if (body instanceof FileList) {
Array.from(body).forEach((file) => {
form.append("files", file)
})
} else {
body.files.forEach((file) => {
form.append(
"files",
"content" in file
? new Blob([file.content], {
type: "text/plain",
})
: file,
file.name
)
})
}
return this.client.fetch<{ files: HttpTypes.AdminFile[] }>(
`/admin/uploads`,
{
method: "POST",
headers: {
...headers,
// Let the browser determine the content type.
"content-type": null,
},
body: form,
query,
}
)
},
retrieve: async (
id: string,
query?: SelectParams,
headers?: ClientHeaders
) => {
return this.client.fetch<{ file: HttpTypes.AdminFile }>(
`/admin/uploads/${id}`,
{
query,
headers,
}
)
},
delete: async (id: string, headers?: ClientHeaders) => {
return this.client.fetch<DeleteResponse<"file">>(`/admin/uploads/${id}`, {
method: "DELETE",
headers,
})
},
}
}