🚚 move file handle to utility
This commit is contained in:
parent
e6839719a6
commit
b862780990
BIN
bundle/package-lock.json
generated
BIN
bundle/package-lock.json
generated
Binary file not shown.
BIN
cdn/package-lock.json
generated
BIN
cdn/package-lock.json
generated
Binary file not shown.
BIN
gateway/package-lock.json
generated
BIN
gateway/package-lock.json
generated
Binary file not shown.
BIN
util/package-lock.json
generated
BIN
util/package-lock.json
generated
Binary file not shown.
@ -31,6 +31,7 @@
|
||||
"@types/amqplib": "^0.8.1",
|
||||
"@types/jsonwebtoken": "^8.5.0",
|
||||
"@types/mongoose-autopopulate": "^0.10.1",
|
||||
"@types/multer": "^1.4.7",
|
||||
"@types/node": "^14.17.9",
|
||||
"@types/node-fetch": "^2.5.12",
|
||||
"jest": "^27.0.6"
|
||||
@ -44,6 +45,7 @@
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"lambert-server": "^1.2.10",
|
||||
"missing-native-js-functions": "^1.2.15",
|
||||
"multer": "^1.4.3",
|
||||
"node-fetch": "^2.6.1",
|
||||
"patch-package": "^6.4.7",
|
||||
"pg": "^8.7.1",
|
||||
|
||||
54
util/src/util/cdn.ts
Normal file
54
util/src/util/cdn.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import FormData from "form-data";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import fetch from "node-fetch";
|
||||
import { Config } from "./Config";
|
||||
import multer from "multer";
|
||||
|
||||
export async function uploadFile(path: string, file: Express.Multer.File) {
|
||||
const form = new FormData();
|
||||
form.append("file", file.buffer, {
|
||||
contentType: file.mimetype,
|
||||
filename: file.originalname,
|
||||
});
|
||||
|
||||
const response = await fetch(`${Config.get().cdn.endpoint || "http://localhost:3003"}${path}`, {
|
||||
headers: {
|
||||
signature: Config.get().security.requestSignature,
|
||||
...form.getHeaders(),
|
||||
},
|
||||
method: "POST",
|
||||
body: form,
|
||||
});
|
||||
const result = await response.json();
|
||||
|
||||
if (response.status !== 200) throw result;
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function handleFile(path: string, body?: string): Promise<string | undefined> {
|
||||
if (!body || !body.startsWith("data:")) return body;
|
||||
try {
|
||||
const mimetype = body.split(":")[1].split(";")[0];
|
||||
const buffer = Buffer.from(body.split(",")[1], "base64");
|
||||
|
||||
// @ts-ignore
|
||||
const { id } = await uploadFile(path, { buffer, mimetype, originalname: "banner" });
|
||||
return id;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw new HTTPError("Invalid " + path);
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteFile(path: string) {
|
||||
const response = await fetch(`${Config.get().cdn.endpoint || "http://localhost:3003"}${path}`, {
|
||||
headers: {
|
||||
signature: Config.get().security.requestSignature,
|
||||
},
|
||||
method: "DELETE",
|
||||
});
|
||||
const result = await response.json();
|
||||
|
||||
if (response.status !== 200) throw result;
|
||||
return result;
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
export * from "./ApiError";
|
||||
export * from "./BitField";
|
||||
export * from "./checkToken";
|
||||
export * from "./cdn";
|
||||
export * from "./Config";
|
||||
export * from "./Constants";
|
||||
export * from "./Database";
|
||||
|
||||
Reference in New Issue
Block a user