Базовая система входа и система новостей/оповещений

This commit is contained in:
Artem VV 2023-05-19 21:15:25 +07:00
parent 38b3006746
commit 811856fee4
21 changed files with 1400 additions and 109 deletions

31
src/pages/uploadFile.ts Normal file
View file

@ -0,0 +1,31 @@
import type { APIContext } from "astro";
import sharp from "sharp";
import { join as joinPath } from "path";
function randomHash() {
return (Math.random() * 1e30).toString(16);
}
export async function post({ request }: APIContext) {
const response: { success: number; file?: { url: string } } = {
success: 1,
};
try {
const fd = await request.formData();
const imageFile = fd.get("image") as Blob;
const fName = `${randomHash()}_${imageFile.name}.jpg`;
await sharp(await imageFile.arrayBuffer())
.jpeg({ mozjpeg: true })
.toFile(joinPath(process.cwd(), "public", "uploads", fName));
response.file = {
url: `/uploads/${fName}`,
};
} catch (e) {
response.success = 0;
}
return {
body: JSON.stringify(response),
};
}