From 2cbce09112ef26b0589db435d8911e7c763ae1ff Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Fri, 12 Nov 2021 18:38:33 +0000 Subject: [PATCH] Change the arrangement of CDN from /users/(banner|pfp)/userID to /users/userID/(banner|pfp). --- server/.github/workflows/test-build-deploy.yml | 1 - server/scripts/single-use/convert-kt1-userimages.ts | 4 ++++ server/src/lib/cdn/cdn.ts | 4 +++- server/src/lib/cdn/url-format.ts | 4 ++-- server/src/lib/setup/config.ts | 2 +- .../router/api/v1/users/_userID/banner/router.test.ts | 6 ++++-- .../src/server/router/api/v1/users/_userID/banner/router.ts | 4 ++-- server/src/server/server.ts | 6 +++--- 8 files changed, 19 insertions(+), 12 deletions(-) diff --git a/server/.github/workflows/test-build-deploy.yml b/server/.github/workflows/test-build-deploy.yml index 0b7f41f4b..b0833530a 100644 --- a/server/.github/workflows/test-build-deploy.yml +++ b/server/.github/workflows/test-build-deploy.yml @@ -44,7 +44,6 @@ jobs: PORT: 8080 MONGO_URL: "127.0.0.1" REDIS_URL: "127.0.0.1" - CDN_FILE_ROOT: "./test-cdn" - name: Lint Code run: pnpm lint diff --git a/server/scripts/single-use/convert-kt1-userimages.ts b/server/scripts/single-use/convert-kt1-userimages.ts index 0e797535a..d77e63114 100644 --- a/server/scripts/single-use/convert-kt1-userimages.ts +++ b/server/scripts/single-use/convert-kt1-userimages.ts @@ -12,6 +12,10 @@ if (!options.dir) { throw new Error(`No dir provided, yet one is necessary.`); } +// @warn THIS SCRIPT IS OUTDATED! +// Tachi now uses /users/${userID}/banner instead of +// /users/banner/${userID} + fs.mkdirSync("users/banner", { recursive: true }); fs.mkdirSync("users/pfp", { recursive: true }); diff --git a/server/src/lib/cdn/cdn.ts b/server/src/lib/cdn/cdn.ts index a83996c59..0326b1d24 100644 --- a/server/src/lib/cdn/cdn.ts +++ b/server/src/lib/cdn/cdn.ts @@ -19,7 +19,7 @@ const logger = CreateLogCtx(__filename); * @danger - This function should **NEVER** be called with unsanitised user input! * Path directory traversal *is* possible, and *will* ruin your day. */ -function CDNFileSystemRoot(fileLoc: string) { +export function CDNFileSystemRoot(fileLoc: string) { if (ServerConfig.CDN_CONFIG.SAVE_LOCATION.TYPE !== "LOCAL_FILESYSTEM") { logger.severe( `Attempted to run CDNFileSystemRoot, but was not using LOCAL_FILESYSTEM as a CDN.`, @@ -54,6 +54,8 @@ export function CDNRedirect(res: Response, fileLoc: string) { throw new Error(`Invalid fileLoc - did not start with /.`); } + logger.debug(`CDN Redirecting to ${ServerConfig.CDN_CONFIG.WEB_LOCATION}${fileLoc}.`); + return res.redirect(`${ServerConfig.CDN_CONFIG.WEB_LOCATION}${fileLoc}`); } diff --git a/server/src/lib/cdn/url-format.ts b/server/src/lib/cdn/url-format.ts index a8177fd8e..09775ab55 100644 --- a/server/src/lib/cdn/url-format.ts +++ b/server/src/lib/cdn/url-format.ts @@ -5,9 +5,9 @@ export function GetUSCIRReplayURL(scoreID: string) { } export function GetProfilePictureURL(userID: integer) { - return `/users/pfp/${userID}`; + return `/users/${userID}/pfp`; } export function GetProfileBannerURL(userID: integer) { - return `/users/banner/${userID}`; + return `/users/${userID}/banner`; } diff --git a/server/src/lib/setup/config.ts b/server/src/lib/setup/config.ts index 9ac4f1665..cf9fbd1ac 100644 --- a/server/src/lib/setup/config.ts +++ b/server/src/lib/setup/config.ts @@ -101,7 +101,7 @@ export interface TachiServerConfig { CDN_CONFIG: { WEB_LOCATION: string; SAVE_LOCATION: - | { TYPE: "LOCAL_FILESYSTEM"; LOCATION: string; RUN_OWN_CDN?: boolean } + | { TYPE: "LOCAL_FILESYSTEM"; LOCATION: string; SERVE_OWN_CDN?: boolean } | { TYPE: "S3_BUCKET"; ENDPOINT: string; diff --git a/server/src/server/router/api/v1/users/_userID/banner/router.test.ts b/server/src/server/router/api/v1/users/_userID/banner/router.test.ts index 3c81eb69b..92cdef2c7 100644 --- a/server/src/server/router/api/v1/users/_userID/banner/router.test.ts +++ b/server/src/server/router/api/v1/users/_userID/banner/router.test.ts @@ -1,8 +1,7 @@ -import t from "tap"; import db from "external/mongo/db"; import { CDNStoreOrOverwrite } from "lib/cdn/cdn"; import { GetProfileBannerURL } from "lib/cdn/url-format"; - +import t from "tap"; import mockApi from "test-utils/mock-api"; import ResetDBState from "test-utils/resets"; import { GetKTDataBuffer } from "test-utils/test-data"; @@ -14,6 +13,8 @@ t.test("GET /api/v1/users/:userID/banner", (t) => { await CDNStoreOrOverwrite("/users/default/banner", "test"); const res = await mockApi.get("/api/v1/users/1/banner").redirects(1); + t.equal(res.statusCode, 200, "Should return 200."); + t.equal(res.body.toString(), "test"); t.end(); @@ -24,6 +25,7 @@ t.test("GET /api/v1/users/:userID/banner", (t) => { await db.users.update({ id: 1 }, { $set: { customBanner: true } }); const res = await mockApi.get("/api/v1/users/1/banner").redirects(1); + t.equal(res.statusCode, 200, "Should return 200."); t.equal(res.body.toString(), "foo"); t.end(); diff --git a/server/src/server/router/api/v1/users/_userID/banner/router.ts b/server/src/server/router/api/v1/users/_userID/banner/router.ts index ab4ae4a1f..220d84f0e 100644 --- a/server/src/server/router/api/v1/users/_userID/banner/router.ts +++ b/server/src/server/router/api/v1/users/_userID/banner/router.ts @@ -1,13 +1,13 @@ import { Router } from "express"; import db from "external/mongo/db"; -import { CDNStoreOrOverwrite, CDNRedirect, CDNDelete } from "lib/cdn/cdn"; +import { CDNDelete, CDNRedirect, CDNStoreOrOverwrite } from "lib/cdn/cdn"; import { GetProfileBannerURL } from "lib/cdn/url-format"; import { ONE_MEGABYTE } from "lib/constants/filesize"; import { SYMBOL_TachiData } from "lib/constants/tachi"; import CreateLogCtx from "lib/logger/logger"; -import { FormatUserDoc } from "utils/user"; import { RequirePermissions } from "server/middleware/auth"; import { CreateMulterSingleUploadMiddleware } from "server/middleware/multer-upload"; +import { FormatUserDoc } from "utils/user"; import { RequireAuthedAsUser } from "../middleware"; // note: this is just the ../pfp/router.ts code copied and altered. diff --git a/server/src/server/server.ts b/server/src/server/server.ts index 7a1fb4242..9b38f76bd 100644 --- a/server/src/server/server.ts +++ b/server/src/server/server.ts @@ -108,12 +108,12 @@ app.use(RequestLoggerMiddleware); app.use("/", mainRouter); -// The RUN_OWN_CDN option means that our /cdn path has to be hosted by us. In production, +// The SERVE_OWN_CDN option means that our /cdn path has to be hosted by us. In production, // this is not the case (we have a dedicated nginx box for it running in a separate process). // In dev, this is a pain to setup, so we can just run it locally. if ( ServerConfig.CDN_CONFIG.SAVE_LOCATION.TYPE === "LOCAL_FILESYSTEM" && - ServerConfig.CDN_CONFIG.SAVE_LOCATION.RUN_OWN_CDN + ServerConfig.CDN_CONFIG.SAVE_LOCATION.SERVE_OWN_CDN ) { if (Environment.nodeEnv === "production") { logger.warn( @@ -126,7 +126,7 @@ if ( }); } - app.use("/cdn", express.static(ServerConfig.CDN_CONFIG.SAVE_LOCATION.LOCATION)); + app.use("/cdn", express.static("/home/zkldi/Repos/Projects/Tachi/tachi-server/test-cdn")); app.get("/cdn/*", (req, res) => res.status(404).send("No content here.")); }