✨ handleFile() now returns mime_type and size
This commit is contained in:
parent
d5cdc9198c
commit
0ef82be133
@ -25,15 +25,30 @@ export async function uploadFile(path: string, file: Express.Multer.File) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handleFile(path: string, body?: string): Promise<string | undefined> {
|
export async function handleFile(
|
||||||
if (!body || !body.startsWith("data:")) return body;
|
path: string,
|
||||||
|
body?: string
|
||||||
|
): Promise<
|
||||||
|
| (string & {
|
||||||
|
id: string;
|
||||||
|
content_type: string;
|
||||||
|
size: number;
|
||||||
|
url: string;
|
||||||
|
})
|
||||||
|
| undefined
|
||||||
|
> {
|
||||||
|
if (!body || !body.startsWith("data:")) return undefined;
|
||||||
try {
|
try {
|
||||||
const mimetype = body.split(":")[1].split(";")[0];
|
const mimetype = body.split(":")[1].split(";")[0];
|
||||||
const buffer = Buffer.from(body.split(",")[1], "base64");
|
const buffer = Buffer.from(body.split(",")[1], "base64");
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const { id } = await uploadFile(path, { buffer, mimetype, originalname: "banner" });
|
const file = await uploadFile(path, { buffer, mimetype, originalname: "banner" });
|
||||||
return id;
|
const obj = file.id;
|
||||||
|
for (const key in file) {
|
||||||
|
obj[key] = file[key];
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
throw new HTTPError("Invalid " + path);
|
throw new HTTPError("Invalid " + path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user