From a75b79065452ad006383f618508293f67a66f97e Mon Sep 17 00:00:00 2001 From: TacticalCoderJay Date: Sun, 27 Nov 2022 18:20:29 -0800 Subject: [PATCH] fix: allow root route & remove swift refs (#235) (#214) * fix: - readd root route for uploads only - catch 1 edge case for root route (/dashboard) * fix: spelt dashboard right * fix: include the dot for the extension * fix: remove any possible references of swift * fix: missed a spot * Update .env.local.example Co-authored-by: Jonathan * Update .env.local.example * Update validateConfig.ts * format Co-authored-by: dicedtomato <35403473+diced@users.noreply.github.com> Co-authored-by: Jonathan --- .env.local.example | 16 ++++++-------- src/lib/config/Config.ts | 10 --------- src/lib/config/validateConfig.ts | 32 +++++++-------------------- src/pages/api/upload.ts | 4 ++-- src/server/index.ts | 37 ++++++++++++++++++-------------- 5 files changed, 38 insertions(+), 61 deletions(-) diff --git a/.env.local.example b/.env.local.example index c12d8f9..011dec7 100644 --- a/.env.local.example +++ b/.env.local.example @@ -1,7 +1,7 @@ # every field in here is optional except, CORE_SECRET and CORE_DATABASE_URL. # if CORE_SECRET is still "changethis" then zipline will exit and tell you to change it. -# if using s3/swift make sure to comment out the other datasources +# if using s3/supabase make sure to comment out the other datasources CORE_HTTPS=true CORE_SECRET="changethis" @@ -25,14 +25,12 @@ DATASOURCE_S3_REGION=us-west-2 DATASOURCE_S3_FORCE_S3_PATH=false DATASOURCE_S3_USE_SSL=false -# or you can use swift -DATASOURCE_TYPE=swift -DATASOURCE_SWIFT_CONTAINER=container -DATASOURCE_SWIFT_AUTH_ENDPOINT="https://something/v3" -DATASOURCE_SWIFT_USERNAME=username -DATASOURCE_SWIFT_PASSWORD=password -DATASOURCE_SWIFT_PROJECT_ID=project_id -DATASOURCE_SWIFT_DOMAIN_ID=domain_id +# or supabase +DATASOURCE_TYPE=supabase +DATASOURCE_SUPABASE_KEY=xxx +# remember: no leading slash +DATASOURCE_SUPABASE_URL=https://something.supabase.co +DATASOURCE_SUPABASE_BUCKET=zipline UPLOADER_DEFAULT_FORMAT=RANDOM UPLOADER_ROUTE=/u diff --git a/src/lib/config/Config.ts b/src/lib/config/Config.ts index a761bcf..b4cfbce 100644 --- a/src/lib/config/Config.ts +++ b/src/lib/config/Config.ts @@ -32,16 +32,6 @@ export interface ConfigS3Datasource { region?: string; } -export interface ConfigSwiftDatasource { - container: string; - auth_endpoint: string; - username: string; - password: string; - project_id: string; - domain_id?: string; - region_id?: string; -} - export interface ConfigSupabaseDatasource { url: string; key: string; diff --git a/src/lib/config/validateConfig.ts b/src/lib/config/validateConfig.ts index 791e758..729eb1e 100644 --- a/src/lib/config/validateConfig.ts +++ b/src/lib/config/validateConfig.ts @@ -34,7 +34,7 @@ const validator = s.object({ }), datasource: s .object({ - type: s.enum('local', 's3', 'swift', 'supabase').default('local'), + type: s.enum('local', 's3', 'supabase').default('local'), local: s .object({ directory: s.string.default('./uploads'), @@ -52,15 +52,6 @@ const validator = s.object({ region: s.string.default('us-east-1'), use_ssl: s.boolean.default(false), }).optional, - swift: s.object({ - username: s.string, - password: s.string, - auth_endpoint: s.string, - container: s.string, - project_id: s.string, - domain_id: s.string.default('default'), - region_id: s.string.nullable, - }).optional, supabase: s.object({ url: s.string, key: s.string, @@ -76,9 +67,6 @@ const validator = s.object({ region: 'us-east-1', force_s3_path: false, }, - swift: { - domain_id: 'default', - }, }), uploader: s .object({ @@ -233,19 +221,15 @@ export default function validate(config): Config { if (errors.length) throw { errors }; break; } - case 'swift': { + case 'supabase': { const errors = []; - if (!validated.datasource.swift.container) - errors.push('datasource.swift.container is a required field'); - if (!validated.datasource.swift.project_id) - errors.push('datasource.swift.project_id is a required field'); - if (!validated.datasource.swift.auth_endpoint) - errors.push('datasource.swift.auth_endpoint is a required field'); - if (!validated.datasource.swift.password) - errors.push('datasource.swift.password is a required field'); - if (!validated.datasource.swift.username) - errors.push('datasource.swift.username is a required field'); + + if (!validated.datasource.supabase.key) errors.push('datasource.supabase.key is a required field'); + if (!validated.datasource.supabase.url) errors.push('datasource.supabase.url is a required field'); + if (!validated.datasource.supabase.bucket) + errors.push('datasource.supabase.bucket is a required field'); if (errors.length) throw { errors }; + break; } } diff --git a/src/pages/api/upload.ts b/src/pages/api/upload.ts index 31f3ad8..3b6a6fe 100644 --- a/src/pages/api/upload.ts +++ b/src/pages/api/upload.ts @@ -149,7 +149,7 @@ async function handler(req: NextApiReq, res: NextApiRes) { const file = await prisma.image.create({ data: { - file: `${fileName}${compressionUsed ? '.jpg' : ext ?? ''}`, + file: `${fileName}${compressionUsed ? '.jpg' : `${ext ? '.' : ''}${ext}`}`, mimetype, userId: user.id, embed: !!req.headers.embed, @@ -273,7 +273,7 @@ async function handler(req: NextApiReq, res: NextApiRes) { let invis: InvisibleImage; const image = await prisma.image.create({ data: { - file: `${fileName}${compressionUsed ? '.jpg' : ext ?? ''}`, + file: `${fileName}${compressionUsed ? '.jpg' : `${ext ? '.' : ''}${ext}`}`, mimetype: req.headers.uploadtext ? 'text/plain' : compressionUsed ? 'image/jpeg' : file.mimetype, userId: user.id, embed: !!req.headers.embed, diff --git a/src/server/index.ts b/src/server/index.ts index 3f6b1c2..5c267e1 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -118,27 +118,32 @@ async function start() { return redirect(res, url.destination); }); - router.on('GET', `${config.uploader.route}/:id`, async (req, res, params) => { - if (params.id === '') return nextServer.render404(req, res as ServerResponse); + router.on( + 'GET', + config.uploader.route === '/' ? '/:id' : `${config.uploader.route}/:id`, + async (req, res, params) => { + if (params.id === '') return nextServer.render404(req, res as ServerResponse); + else if (params.id === 'dashboard') return nextServer.render(req, res as ServerResponse, '/dashboard'); - const image = await prisma.image.findFirst({ - where: { - OR: [{ file: params.id }, { invisible: { invis: decodeURI(params.id) } }], - }, - }); + const image = await prisma.image.findFirst({ + where: { + OR: [{ file: params.id }, { invisible: { invis: decodeURI(params.id) } }], + }, + }); - if (!image) return rawFile(req, res, nextServer, params.id); - else { - const failed = await preFile(image, prisma); - if (failed) return nextServer.render404(req, res as ServerResponse); + if (!image) return rawFile(req, res, nextServer, params.id); + else { + const failed = await preFile(image, prisma); + if (failed) return nextServer.render404(req, res as ServerResponse); - if (image.password || image.embed || image.mimetype.startsWith('text/')) - redirect(res, `/view/${image.file}`); - else fileDb(req, res, nextServer, handle, image); + if (image.password || image.embed || image.mimetype.startsWith('text/')) + redirect(res, `/view/${image.file}`); + else fileDb(req, res, nextServer, handle, image); - postFile(image, prisma); + postFile(image, prisma); + } } - }); + ); router.on('GET', '/r/:id', async (req, res, params) => { if (params.id === '') return nextServer.render404(req, res as ServerResponse);