From f7cde0cf1eaad5a03553470a3259d64aec94d70b Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Thu, 18 Aug 2022 01:54:36 +0600 Subject: i forgor to commit --- src/clients/Multer.ts | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/clients/Prisma.ts | 19 ++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/clients/Multer.ts create mode 100644 src/clients/Prisma.ts (limited to 'src/clients') diff --git a/src/clients/Multer.ts b/src/clients/Multer.ts new file mode 100644 index 0000000..81f2b0f --- /dev/null +++ b/src/clients/Multer.ts @@ -0,0 +1,68 @@ +// Copyright 2022 NotDankEnough (ilotterytea) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import multer from "multer"; +import mime from "mime"; +import crypto from "crypto"; +import { readdirSync } from "fs"; + +const fileFilter = (req: Express.Request, res: Express.Response, cb: multer.FileFilterCallback) => { + cb(null, true); +} + +const storage = multer.diskStorage({ + destination: "static/images", + filename(req, file, callback) { + if (!req.headers["pattern"]) req.headers["pattern"] = "random"; + + switch (req.headers["pattern"]) { + case "numeration": { + const img_id: number = readdirSync("images").length + 1; + const max_zero: number = 5; + var blank_string: string = ""; + + for (var i = 0; i < max_zero; i++) { blank_string = blank_string + "0"; } + + blank_string = blank_string.slice(img_id.toString().length, blank_string.length); + + callback(null, `${blank_string}${img_id.toString()}.${mime.getExtension(file.mimetype)}`); + break; + } + case "random": { + crypto.randomBytes(5, (err, raw) => { + if (err) return callback(err, ""); + callback(null, `${raw.toString("hex")}.${mime.getExtension(file.mimetype)}`) + }); + break; + } + default: { + crypto.randomBytes(5, (err, raw) => { + if (err) return callback(err, ""); + callback(null, `${raw.toString("hex")}.${mime.getExtension(file.mimetype)}`) + }); + break; + } + } + } +}); + +const Multer = multer({ + fileFilter: fileFilter, + storage: storage, + limits: { + fieldSize: 1.6e+7 + } +}); + +export default Multer; \ No newline at end of file diff --git a/src/clients/Prisma.ts b/src/clients/Prisma.ts new file mode 100644 index 0000000..1e17fc9 --- /dev/null +++ b/src/clients/Prisma.ts @@ -0,0 +1,19 @@ +// Copyright 2022 NotDankEnough (ilotterytea) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { PrismaClient } from "@prisma/client"; + +const prisma = new PrismaClient(); + +export default prisma; \ No newline at end of file -- cgit v1.2.3