From 090060d5805b2f6594ad66862d79c0d76e9c7dc5 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 17 Feb 2023 18:00:16 -0500 Subject: Implement default avatars --- assets/default-avatars/1.png | Bin 0 -> 4397 bytes assets/default-avatars/2.png | Bin 0 -> 4544 bytes assets/default-avatars/3.png | Bin 0 -> 2831 bytes assets/default-avatars/4.png | Bin 0 -> 2855 bytes assets/default-avatars/5.png | Bin 0 -> 4276 bytes src/cdn/routes/embed.ts | 56 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+) create mode 100644 assets/default-avatars/1.png create mode 100644 assets/default-avatars/2.png create mode 100644 assets/default-avatars/3.png create mode 100644 assets/default-avatars/4.png create mode 100644 assets/default-avatars/5.png create mode 100644 src/cdn/routes/embed.ts diff --git a/assets/default-avatars/1.png b/assets/default-avatars/1.png new file mode 100644 index 00000000..61b3f9cf Binary files /dev/null and b/assets/default-avatars/1.png differ diff --git a/assets/default-avatars/2.png b/assets/default-avatars/2.png new file mode 100644 index 00000000..8caebf90 Binary files /dev/null and b/assets/default-avatars/2.png differ diff --git a/assets/default-avatars/3.png b/assets/default-avatars/3.png new file mode 100644 index 00000000..3fadcedf Binary files /dev/null and b/assets/default-avatars/3.png differ diff --git a/assets/default-avatars/4.png b/assets/default-avatars/4.png new file mode 100644 index 00000000..bcf0c183 Binary files /dev/null and b/assets/default-avatars/4.png differ diff --git a/assets/default-avatars/5.png b/assets/default-avatars/5.png new file mode 100644 index 00000000..3cc5589c Binary files /dev/null and b/assets/default-avatars/5.png differ diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts new file mode 100644 index 00000000..86268189 --- /dev/null +++ b/src/cdn/routes/embed.ts @@ -0,0 +1,56 @@ +/* + Fosscord: A FOSS re-implementation and extension of the Discord.com backend. + Copyright (C) 2023 Fosscord and Fosscord Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +import { Request, Response, Router } from "express"; +import FileType from "file-type"; +import fs from "fs"; +import { HTTPError } from "lambert-server"; +import { join } from "path"; + +const router = Router(); + +function getFile(path: string) { + try { + return fs.readFileSync(path); + } catch (error) { + try { + const files = fs.readdirSync(path); + if (!files.length) return null; + return fs.readFileSync(join(path, files[0])); + } catch (error) { + return null; + } + } +} + +router.get("/avatars/:id", async (req: Request, res: Response) => { + let { id } = req.params; + id = id.split(".")[0]; // remove .file extension + const path = join(process.cwd(), "assets", "default-avatars", `${id}.png`); + + const file = getFile(path); + if (!file) throw new HTTPError("not found", 404); + const type = await FileType.fromBuffer(file); + + res.set("Content-Type", type?.mime); + res.set("Cache-Control", "public, max-age=31536000"); + + return res.send(file); +}); + +export default router; -- cgit 1.4.1 From 6fa4f9649ffee1f9909bc7645ca6d408d4699d4b Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 17 Feb 2023 18:10:05 -0500 Subject: async --- src/cdn/routes/embed.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts index 86268189..c38046ed 100644 --- a/src/cdn/routes/embed.ts +++ b/src/cdn/routes/embed.ts @@ -18,20 +18,20 @@ import { Request, Response, Router } from "express"; import FileType from "file-type"; -import fs from "fs"; +import fs from "fs/promises"; import { HTTPError } from "lambert-server"; import { join } from "path"; const router = Router(); -function getFile(path: string) { +async function getFile(path: string) { try { - return fs.readFileSync(path); + return fs.readFile(path); } catch (error) { try { - const files = fs.readdirSync(path); + const files = await fs.readdir(path); if (!files.length) return null; - return fs.readFileSync(join(path, files[0])); + return fs.readFile(join(path, files[0])); } catch (error) { return null; } @@ -43,7 +43,7 @@ router.get("/avatars/:id", async (req: Request, res: Response) => { id = id.split(".")[0]; // remove .file extension const path = join(process.cwd(), "assets", "default-avatars", `${id}.png`); - const file = getFile(path); + const file = await getFile(path); if (!file) throw new HTTPError("not found", 404); const type = await FileType.fromBuffer(file); -- cgit 1.4.1 From 1189deaa7cac6b8d5860e1cbdd83b8fa9feb744b Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 17 Feb 2023 19:59:12 -0500 Subject: add support for desktop/web --- assets/default-avatars/1.png | Bin 4397 -> 0 bytes assets/default-avatars/2.png | Bin 4544 -> 0 bytes assets/default-avatars/3.png | Bin 2831 -> 0 bytes assets/default-avatars/4.png | Bin 2855 -> 0 bytes assets/default-avatars/5.png | Bin 4276 -> 0 bytes assets/public/1f0bfc0865d324c2587920a7d80c609b.png | Bin 0 -> 4397 bytes assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png | Bin 0 -> 4276 bytes assets/public/6f26ddd1bf59740c536d2274bb834a05.png | Bin 0 -> 2855 bytes assets/public/7c8f476123d28d103efe381543274c25.png | Bin 0 -> 2831 bytes assets/public/c09a43a372ba81e3018c3151d4ed4773.png | Bin 0 -> 4544 bytes package-lock.json | 1 - src/cdn/routes/embed.ts | 12 +++++++++++- 12 files changed, 11 insertions(+), 2 deletions(-) delete mode 100644 assets/default-avatars/1.png delete mode 100644 assets/default-avatars/2.png delete mode 100644 assets/default-avatars/3.png delete mode 100644 assets/default-avatars/4.png delete mode 100644 assets/default-avatars/5.png create mode 100644 assets/public/1f0bfc0865d324c2587920a7d80c609b.png create mode 100644 assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png create mode 100644 assets/public/6f26ddd1bf59740c536d2274bb834a05.png create mode 100644 assets/public/7c8f476123d28d103efe381543274c25.png create mode 100644 assets/public/c09a43a372ba81e3018c3151d4ed4773.png diff --git a/assets/default-avatars/1.png b/assets/default-avatars/1.png deleted file mode 100644 index 61b3f9cf..00000000 Binary files a/assets/default-avatars/1.png and /dev/null differ diff --git a/assets/default-avatars/2.png b/assets/default-avatars/2.png deleted file mode 100644 index 8caebf90..00000000 Binary files a/assets/default-avatars/2.png and /dev/null differ diff --git a/assets/default-avatars/3.png b/assets/default-avatars/3.png deleted file mode 100644 index 3fadcedf..00000000 Binary files a/assets/default-avatars/3.png and /dev/null differ diff --git a/assets/default-avatars/4.png b/assets/default-avatars/4.png deleted file mode 100644 index bcf0c183..00000000 Binary files a/assets/default-avatars/4.png and /dev/null differ diff --git a/assets/default-avatars/5.png b/assets/default-avatars/5.png deleted file mode 100644 index 3cc5589c..00000000 Binary files a/assets/default-avatars/5.png and /dev/null differ diff --git a/assets/public/1f0bfc0865d324c2587920a7d80c609b.png b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png new file mode 100644 index 00000000..61b3f9cf Binary files /dev/null and b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png differ diff --git a/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png new file mode 100644 index 00000000..3cc5589c Binary files /dev/null and b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png differ diff --git a/assets/public/6f26ddd1bf59740c536d2274bb834a05.png b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png new file mode 100644 index 00000000..bcf0c183 Binary files /dev/null and b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png differ diff --git a/assets/public/7c8f476123d28d103efe381543274c25.png b/assets/public/7c8f476123d28d103efe381543274c25.png new file mode 100644 index 00000000..3fadcedf Binary files /dev/null and b/assets/public/7c8f476123d28d103efe381543274c25.png differ diff --git a/assets/public/c09a43a372ba81e3018c3151d4ed4773.png b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png new file mode 100644 index 00000000..8caebf90 Binary files /dev/null and b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png differ diff --git a/package-lock.json b/package-lock.json index 165a2b23..ecd455b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45,7 +45,6 @@ "probe-image-size": "^7.2.3", "proxy-agent": "^5.0.0", "reflect-metadata": "^0.1.13", - "sqlite3": "*", "ts-node": "^10.9.1", "tslib": "^2.4.1", "typeorm": "^0.3.10", diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts index c38046ed..5e5ff76d 100644 --- a/src/cdn/routes/embed.ts +++ b/src/cdn/routes/embed.ts @@ -22,6 +22,14 @@ import fs from "fs/promises"; import { HTTPError } from "lambert-server"; import { join } from "path"; +const defaultAvatarHashMap = new Map([ + ["1", "1f0bfc0865d324c2587920a7d80c609b"], + ["2", "c09a43a372ba81e3018c3151d4ed4773"], + ["3", "7c8f476123d28d103efe381543274c25"], + ["4", "6f26ddd1bf59740c536d2274bb834a05"], + ["5", "3c6ccb83716d1e4fb91d3082f6b21d77"], +]); + const router = Router(); async function getFile(path: string) { @@ -41,7 +49,9 @@ async function getFile(path: string) { router.get("/avatars/:id", async (req: Request, res: Response) => { let { id } = req.params; id = id.split(".")[0]; // remove .file extension - const path = join(process.cwd(), "assets", "default-avatars", `${id}.png`); + const hash = defaultAvatarHashMap.get(id); + if (!hash) throw new HTTPError("not found", 404); + const path = join(process.cwd(), "assets", "public", `${hash}.png`); const file = await getFile(path); if (!file) throw new HTTPError("not found", 404); -- cgit 1.4.1 From 2b627ab4a4e8d9c4f518c1534e551a6c1610d8f0 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 18 Feb 2023 12:52:32 +1100 Subject: Update default avatar colour scheme and fix ordering --- assets/public/1f0bfc0865d324c2587920a7d80c609b.png | Bin 4397 -> 4380 bytes assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png | Bin 4276 -> 4511 bytes assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png | Bin 0 -> 4251 bytes assets/public/6f26ddd1bf59740c536d2274bb834a05.png | Bin 2855 -> 4494 bytes assets/public/7c8f476123d28d103efe381543274c25.png | Bin 2831 -> 4657 bytes assets/public/c09a43a372ba81e3018c3151d4ed4773.png | Bin 4544 -> 4158 bytes src/cdn/routes/embed.ts | 11 ++++++----- 7 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png diff --git a/assets/public/1f0bfc0865d324c2587920a7d80c609b.png b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png index 61b3f9cf..9b92bd2f 100644 Binary files a/assets/public/1f0bfc0865d324c2587920a7d80c609b.png and b/assets/public/1f0bfc0865d324c2587920a7d80c609b.png differ diff --git a/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png index 3cc5589c..62c599a7 100644 Binary files a/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png and b/assets/public/3c6ccb83716d1e4fb91d3082f6b21d77.png differ diff --git a/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png b/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png new file mode 100644 index 00000000..bd7afef2 Binary files /dev/null and b/assets/public/4c1b599b1ef5b9f1874fdb9933f3e03b.png differ diff --git a/assets/public/6f26ddd1bf59740c536d2274bb834a05.png b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png index bcf0c183..9f137906 100644 Binary files a/assets/public/6f26ddd1bf59740c536d2274bb834a05.png and b/assets/public/6f26ddd1bf59740c536d2274bb834a05.png differ diff --git a/assets/public/7c8f476123d28d103efe381543274c25.png b/assets/public/7c8f476123d28d103efe381543274c25.png index 3fadcedf..3e8eeae9 100644 Binary files a/assets/public/7c8f476123d28d103efe381543274c25.png and b/assets/public/7c8f476123d28d103efe381543274c25.png differ diff --git a/assets/public/c09a43a372ba81e3018c3151d4ed4773.png b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png index 8caebf90..67ee7bbd 100644 Binary files a/assets/public/c09a43a372ba81e3018c3151d4ed4773.png and b/assets/public/c09a43a372ba81e3018c3151d4ed4773.png differ diff --git a/src/cdn/routes/embed.ts b/src/cdn/routes/embed.ts index 5e5ff76d..fd13d215 100644 --- a/src/cdn/routes/embed.ts +++ b/src/cdn/routes/embed.ts @@ -23,11 +23,12 @@ import { HTTPError } from "lambert-server"; import { join } from "path"; const defaultAvatarHashMap = new Map([ - ["1", "1f0bfc0865d324c2587920a7d80c609b"], - ["2", "c09a43a372ba81e3018c3151d4ed4773"], - ["3", "7c8f476123d28d103efe381543274c25"], - ["4", "6f26ddd1bf59740c536d2274bb834a05"], - ["5", "3c6ccb83716d1e4fb91d3082f6b21d77"], + ["0", "1f0bfc0865d324c2587920a7d80c609b"], + ["1", "c09a43a372ba81e3018c3151d4ed4773"], + ["2", "7c8f476123d28d103efe381543274c25"], + ["3", "6f26ddd1bf59740c536d2274bb834a05"], + ["4", "3c6ccb83716d1e4fb91d3082f6b21d77"], + ["5", "4c1b599b1ef5b9f1874fdb9933f3e03b"], ]); const router = Router(); -- cgit 1.4.1