summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-03-16 02:20:57 +0500
committerilotterytea <iltsu@alright.party>2025-03-16 02:20:57 +0500
commit7ec95a660dfc40868dcda204f728d8961bc3e7c9 (patch)
tree73fc94069e04b25d4dfe1cf840f04915ff6f31fa /src
parentfaf359ab3863b5f35547906fa28304c0ab33a93a (diff)
reinitial commit
Diffstat (limited to 'src')
-rw-r--r--src/CLI.ts30
-rw-r--r--src/Main.ts56
-rw-r--r--src/clients/Multer.ts68
-rw-r--r--src/clients/Prisma.ts19
-rw-r--r--src/routers/Auth.ts100
-rw-r--r--src/routers/Image.ts107
-rw-r--r--src/routers/Profile.ts58
7 files changed, 0 insertions, 438 deletions
diff --git a/src/CLI.ts b/src/CLI.ts
deleted file mode 100644
index 4259004..0000000
--- a/src/CLI.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-// 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 { Command } from "commander";
-
-/**
- * Silly Command-Line Interface.
- * @returns options.
- */
-function CLI(): Record<string, string | boolean> {
- const Program = new Command();
-
- Program.option("--init", "Generate the neccessary files.", false);
-
- Program.parse(process.argv);
- return Program.opts();
-}
-
-export default CLI; \ No newline at end of file
diff --git a/src/Main.ts b/src/Main.ts
deleted file mode 100644
index 5471a17..0000000
--- a/src/Main.ts
+++ /dev/null
@@ -1,56 +0,0 @@
-// 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 express from "express";
-
-import cookieParser from "cookie-parser";
-import bodyParser from "body-parser";
-
-import { Logger } from "tslog";
-
-import ProfileRouter from "./routers/Profile";
-import AuthRouter from "./routers/Auth";
-import ImageRouter from "./routers/Image";
-import { PrismaClient } from "@prisma/client";
-import { readFileSync } from "fs";
-
-const log: Logger = new Logger({name: "main"});
-
-function Main(dirPath: string, cfg: {[key: string]: any}, cli_options?: {[key: string]: any}): void {
- const App: express.Express = express();
- const prisma: PrismaClient = new PrismaClient();
-
- App.set("view engine", "ejs");
- App.set("views", `${dirPath}/static/ejs`);
-
- App.use(cookieParser());
- App.use(bodyParser.urlencoded({extended: false}));
-
- App.use("/me", ProfileRouter(dirPath, prisma));
- App.use("/auth", AuthRouter(dirPath, cfg, prisma));
- App.use("/", ImageRouter(dirPath, cfg, prisma));
-
- App.use(express.static(`${dirPath}/static`));
-
- if (cli_options) {
- App.listen(parseInt(cfg.Server.Port), () => {
- log.info("Image hoster is running on port", cfg.Server.Port);
- });
- } else {
- log.error("NO CLI OPTIONS PROVIDED!!!")
- process.exit(1);
- }
-}
-
-export default Main; \ No newline at end of file
diff --git a/src/clients/Multer.ts b/src/clients/Multer.ts
deleted file mode 100644
index 81f2b0f..0000000
--- a/src/clients/Multer.ts
+++ /dev/null
@@ -1,68 +0,0 @@
-// 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
deleted file mode 100644
index 1e17fc9..0000000
--- a/src/clients/Prisma.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-// 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
diff --git a/src/routers/Auth.ts b/src/routers/Auth.ts
deleted file mode 100644
index 146bafe..0000000
--- a/src/routers/Auth.ts
+++ /dev/null
@@ -1,100 +0,0 @@
-// 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 { Router, CookieOptions } from "express";
-import { PrismaClient, User } from "@prisma/client";
-import { randomBytes } from "crypto";
-import axios from "axios";
-import { Logger } from "tslog";
-
-const log: Logger = new Logger({name: "authlog"});
-
-function AuthRouter(dirPath: string, cfg: {[key: string]: any}, prisma: PrismaClient): Router {
- const router: Router = Router();
-
- router.get("/twitch", async (req, res) => {
- if (!("code" in req.query)) {
- return res.json({
- status: 400,
- reason: "\"code\" query not found."
- }).status(400);
- }
-
- try {
- const req_token = await axios.post("https://id.twitch.tv/oauth2/token", `client_id=${cfg.Auth.ClientID}&client_secret=${cfg.Auth.ClientSecret}&code=${req.query.code}&grant_type=authorization_code&redirect_uri=${cfg.Auth.RedirectURI}`, {headers: {"Content-Type": "application/x-www-form-urlencoded"}});
-
- const ttv_user = await axios.get("https://api.twitch.tv/helix/users", {
- responseType: "json",
- headers: {
- "Authorization": `Bearer ${req_token.data.access_token}`,
- "Client-Id": cfg.Auth.ClientID
- }
- });
-
- const user_data = ttv_user.data.data[0];
-
- const user: User | null = await prisma.user.findFirst({
- where: {
- alias_id: parseInt(user_data.id)
- }
- });
-
- const key: string = randomBytes(16).toString("hex");
- const cookie_opts: CookieOptions = {
- httpOnly: false,
- secure: true,
- sameSite: "lax"
- };
-
- if (!user) {
- await prisma.user.create({
- data: {
- alias_id: parseInt(user_data.id),
- name: user_data.login,
- desc: user_data.description,
- pic: user_data.profile_image_url,
- key: key
- }
- });
-
- res.cookie("key", key, cookie_opts);
- res.cookie("id", user_data.id, cookie_opts);
- } else {
- await prisma.user.update({
- where: {id: user.id},
- data: {
- name: user_data.login,
- desc: user_data.description,
- pic: user_data.profile_image_url,
- key: key
- }
- });
-
- res.cookie("key", key, cookie_opts);
- res.cookie("id", user_data.id, cookie_opts);
- }
-
- res.redirect("/me");
- } catch (err: any) {
- res.json({
- status: (err.response.data.status) ? err.response.data.status : 400,
- message: (err.response.data.message) ? err.response.data.message : "Bad request."
- }).status((err.response.data.status) ? err.response.data.status : 400);
- }
- });
-
- return router;
-}
-
-export default AuthRouter; \ No newline at end of file
diff --git a/src/routers/Image.ts b/src/routers/Image.ts
deleted file mode 100644
index dfd6a16..0000000
--- a/src/routers/Image.ts
+++ /dev/null
@@ -1,107 +0,0 @@
-// 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, User, Image } from "@prisma/client";
-import express from "express";
-import mime from "mime";
-import multer from "../clients/Multer";
-
-function ImageRouter(dirPath: string, cfg: {[key: string]: any}, prisma: PrismaClient): express.Router {
- const router: express.Router = express.Router();
-
- router.get("/", async (req, res) => {
- const user: User | null = await prisma.user.findFirst({
- where: {
- alias_id: (req.cookies.id) ? parseInt(req.cookies.id) : null,
- key: (req.cookies.key) ? req.cookies.key : ""
- }
- });
-
- return res.render("pages/home", {
- user: user,
- cid: cfg.Auth.ClientID,
- uri: cfg.Auth.RedirectURI
- });
- });
-
- router.post("/upload", multer.single("file"), async (req, res) => {
- const auth: string[] | undefined = (req.headers["authorization"]) ? new Buffer(req.headers["authorization"], "base64").toString("utf-8").split(':') : undefined;
-
- var user: User | null = await prisma.user.findFirst({
- where: {
- alias_id: (auth) ? parseInt(auth[1]) : (req.cookies.id) ? parseInt(req.cookies["id"]) : null,
- key: (auth) ? auth[2] : (req.cookies.key) ? req.cookies["key"] : ""
- }
- });
-
- const image: Image = await prisma.image.create({
- data: {
- storage_id: req.file!.filename!,
- ext: mime.getExtension(req.file!.mimetype)!,
- userId: (user) ? user.id : null
- }
- });
-
- return res.send(`https://i.hmmtodayiwill.ru/${image.id}`).status(200);
- });
-
-
- router.get("/:imageId", async (req, res) => {
- const auth: string[] | undefined = (req.headers["authorization"]) ? new Buffer(req.headers["authorization"], "base64").toString("utf-8").split(':') : undefined;
-
- var user: User | null = await prisma.user.findFirst({
- where: {
- alias_id: (auth) ? parseInt(auth[0]) : (req.cookies.id) ? parseInt(req.cookies["id"]) : null,
- key: (auth) ? auth[1] : (req.cookies.key) ? req.cookies["key"] : ""
- }
- });
-
- const image: Image | null = await prisma.image.findFirst({
- where: {
- id: req.params.imageId
- }
- });
-
- if (!image) {
- return res.json({
- status: 404,
- reason: "Image ID " + req.params.imageId + " not found in database!"
- }).status(404);
- }
-
- if (image.is_hidden) {
- if (user) {
- if (image.userId !== user.id) {
- return res.json({
- status: 401,
- reason: "Image ID " + req.params.imageId + " have a hide flag."
- }).status(401);
- } else {
- return res.sendFile(`${__dirname}/static/images/${image.storage_id}`);
- }
- } else {
- return res.json({
- status: 401,
- reason: "Image ID " + req.params.imageId + " have a hide flag."
- }).status(401);
- }
- }
-
- return res.sendFile(`${dirPath}/static/images/${image.storage_id}`);
- });
-
- return router;
-}
-
-export default ImageRouter; \ No newline at end of file
diff --git a/src/routers/Profile.ts b/src/routers/Profile.ts
deleted file mode 100644
index afc5e5d..0000000
--- a/src/routers/Profile.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-// 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, User, Image } from "@prisma/client";
-import { Router } from "express";
-
-function ProfileRouter(dirPath: string, prisma: PrismaClient): Router {
- const router: Router = Router();
-
- router.get("/", async (req, res) => {
- if (!req.cookies.id || !req.cookies.key) {
- return res.redirect("/");
- }
-
- const user: User | null = await prisma.user.findFirst({
- where: {
- alias_id: parseInt(req.cookies.id),
- key: req.cookies.key
- }
- });
-
- if (!user) {
- return res.redirect("/");
- }
-
- var images: Image[] = await prisma.image.findMany({
- where: {
- userId: user.id
- },
- orderBy: {
- timestamp: "desc"
- }
- });
-
- const keyNotEncoded: string = `${user.id}:${user.alias_id}:${user.key}`;
-
- return res.render("pages/me", {
- user: user,
- images: images,
- authKey: Buffer.alloc(keyNotEncoded.length, keyNotEncoded).toString("base64")
- });
- });
-
- return router;
-}
-
-export default ProfileRouter; \ No newline at end of file