diff --git a/esbuild.config.js b/esbuild.config.js index 2c741f3..e31faca 100644 --- a/esbuild.config.js +++ b/esbuild.config.js @@ -22,11 +22,11 @@ const { rm } = require('fs/promises'); 'src/server/validateConfig.ts', 'src/lib/logger.ts', 'src/lib/readConfig.ts', - 'src/lib/datasource/datasource.ts', - 'src/lib/datasource/index.ts', - 'src/lib/datasource/Local.ts', - 'src/lib/datasource/S3.ts', - 'src/lib/ds.ts', + 'src/lib/datasources/Datasource.ts', + 'src/lib/datasources/index.ts', + 'src/lib/datasources/Local.ts', + 'src/lib/datasources/S3.ts', + 'src/lib/datasource.ts', 'src/lib/config.ts', ], format: 'cjs', diff --git a/package.json b/package.json index d206c7a..6cdcc67 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "3.4.5", "license": "MIT", "scripts": { - "dev": "node esbuild.config.js && REACT_EDITOR=code-insiders NODE_ENV=development node dist/server", + "dev": "node esbuild.config.js && REACT_EDITOR=code NODE_ENV=development node dist/server", "build": "npm-run-all build:server build:schema build:next", "build:server": "node esbuild.config.js", "build:next": "next build", @@ -66,4 +66,4 @@ "url": "https://github.com/diced/zipline.git" }, "packageManager": "yarn@3.2.1" -} +} \ No newline at end of file diff --git a/src/lib/ds.ts b/src/lib/datasource.ts similarity index 93% rename from src/lib/ds.ts rename to src/lib/datasource.ts index 1bdde9c..bda46a4 100644 --- a/src/lib/ds.ts +++ b/src/lib/datasource.ts @@ -1,16 +1,16 @@ import config from './config'; -import { S3, Local } from './datasource'; +import { S3, Local } from './datasources'; import Logger from './logger'; if (!global.datasource) { switch (config.datasource.type) { case 's3': - Logger.get('datasource').info(`Using S3(${config.datasource.s3.bucket}) datasource`); global.datasource = new S3(config.datasource.s3); + Logger.get('datasource').info(`Using S3(${config.datasource.s3.bucket}) datasource`); break; case 'local': - Logger.get('datasource').info(`Using local(${config.datasource.local.directory}) datasource`); global.datasource = new Local(config.datasource.local.directory); + Logger.get('datasource').info(`Using local(${config.datasource.local.directory}) datasource`); break; default: throw new Error('Invalid datasource type'); diff --git a/src/lib/datasource/index.ts b/src/lib/datasource/index.ts deleted file mode 100644 index 6290a44..0000000 --- a/src/lib/datasource/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export { Datasource } from './datasource'; -export { Local } from './Local'; -export { S3 } from './S3'; - diff --git a/src/lib/datasource/datasource.ts b/src/lib/datasources/Datasource.ts similarity index 99% rename from src/lib/datasource/datasource.ts rename to src/lib/datasources/Datasource.ts index 266ad64..7cb183d 100644 --- a/src/lib/datasource/datasource.ts +++ b/src/lib/datasources/Datasource.ts @@ -2,7 +2,7 @@ import { Readable } from 'stream'; export abstract class Datasource { public name: string; - + public abstract save(file: string, data: Buffer): Promise; public abstract delete(file: string): Promise; public abstract get(file: string): Readable; diff --git a/src/lib/datasource/Local.ts b/src/lib/datasources/Local.ts similarity index 96% rename from src/lib/datasource/Local.ts rename to src/lib/datasources/Local.ts index 3d6b81f..06f50fa 100644 --- a/src/lib/datasource/Local.ts +++ b/src/lib/datasources/Local.ts @@ -1,7 +1,7 @@ import { createReadStream, existsSync, ReadStream } from 'fs'; import { readdir, rm, stat, writeFile } from 'fs/promises'; import { join } from 'path'; -import { Datasource } from './datasource'; +import { Datasource } from './'; export class Local extends Datasource { public name: string = 'local'; diff --git a/src/lib/datasource/S3.ts b/src/lib/datasources/S3.ts similarity index 97% rename from src/lib/datasource/S3.ts rename to src/lib/datasources/S3.ts index b4d305f..d2f031b 100644 --- a/src/lib/datasource/S3.ts +++ b/src/lib/datasources/S3.ts @@ -1,4 +1,4 @@ -import { Datasource } from './datasource'; +import { Datasource } from './'; import AWS from 'aws-sdk'; import { Readable } from 'stream'; import { ConfigS3Datasource } from 'lib/types'; diff --git a/src/lib/datasources/index.ts b/src/lib/datasources/index.ts new file mode 100644 index 0000000..0ca118e --- /dev/null +++ b/src/lib/datasources/index.ts @@ -0,0 +1,3 @@ +export { Datasource } from './Datasource'; +export { Local } from './Local'; +export { S3 } from './S3'; \ No newline at end of file diff --git a/src/lib/logger.ts b/src/lib/logger.ts index 934133a..7d22d24 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -21,7 +21,7 @@ export default class Logger { this.name = name; } - info(...args) { + info(...args: any[]) { console.log(this.formatMessage(LoggerLevel.INFO, this.name, args.join(' '))); } @@ -29,7 +29,7 @@ export default class Logger { console.log(this.formatMessage(LoggerLevel.ERROR, this.name, args.map(error => error.stack ?? error).join(' '))); } - formatMessage(level: LoggerLevel, name, message) { + formatMessage(level: LoggerLevel, name: string, message: string) { const time = format(new Date(), 'YYYY-MM-DD hh:mm:ss,SSS A'); return `${time} ${this.formatLevel(level)} [${blueBright(name)}] ${message}`; } diff --git a/src/lib/types.ts b/src/lib/types.ts index 864ab8a..f692774 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -25,8 +25,10 @@ export interface ConfigDatasource { // The type of datasource type: 'local' | 's3'; - // The local datasource + // The local datasource, the default local: ConfigLocalDatasource; + + // The s3 datasource s3?: ConfigS3Datasource; } @@ -36,10 +38,20 @@ export interface ConfigLocalDatasource { } export interface ConfigS3Datasource { + // The access key id for the s3 bucket access_key_id: string; + + // The secret access key for the s3 bucket secret_access_key: string; + + // Not required, but if using a non-aws S3 service you can specify the endpoint endpoint?: string; + + // The S3 bucket to store files in bucket: string; + + // If true Zipline will attempt to connect to the bucket via the url "https://s3.amazonaws.com/{bucket}/stuff" + // If false Zipline will attempt to connect to the bucket via the url "http://{bucket}.s3.amazonaws.com/stuff" force_s3_path: boolean; } diff --git a/src/pages/[...id].tsx b/src/pages/[...id].tsx index 63dd679..52ec74d 100644 --- a/src/pages/[...id].tsx +++ b/src/pages/[...id].tsx @@ -169,7 +169,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => { }; if (!image.mimetype.startsWith('image')) { - const { default: datasource } = await import('lib/ds'); + const { default: datasource } = await import('lib/datasource'); const data = datasource.get(image.file); if (!data) return { notFound: true }; diff --git a/src/pages/api/auth/image.ts b/src/pages/api/auth/image.ts index 552e4d9..0afd4c3 100644 --- a/src/pages/api/auth/image.ts +++ b/src/pages/api/auth/image.ts @@ -1,7 +1,7 @@ import prisma from 'lib/prisma'; import { NextApiReq, NextApiRes, withZipline } from 'middleware/withZipline'; import { checkPassword } from 'lib/util'; -import datasource from 'lib/ds'; +import datasource from 'lib/datasource'; import mimes from '../../../../scripts/mimes'; import { extname } from 'path'; diff --git a/src/pages/api/upload.ts b/src/pages/api/upload.ts index a2cfc59..8bc2c6a 100644 --- a/src/pages/api/upload.ts +++ b/src/pages/api/upload.ts @@ -7,7 +7,7 @@ import Logger from 'lib/logger'; import { ImageFormat, InvisibleImage } from '@prisma/client'; import { format as formatDate } from 'fecha'; import { v4 } from 'uuid'; -import datasource from 'lib/ds'; +import datasource from 'lib/datasource'; const uploader = multer(); diff --git a/src/pages/api/user/export.ts b/src/pages/api/user/export.ts index 10650af..53ed7b3 100644 --- a/src/pages/api/user/export.ts +++ b/src/pages/api/user/export.ts @@ -2,7 +2,7 @@ import { NextApiReq, NextApiRes, withZipline } from 'middleware/withZipline'; import prisma from 'lib/prisma'; import Logger from 'lib/logger'; import { Zip, ZipPassThrough } from 'fflate'; -import datasource from 'lib/ds'; +import datasource from 'lib/datasource'; import { readdir } from 'fs/promises'; import { createReadStream, createWriteStream } from 'fs'; diff --git a/src/pages/api/user/files.ts b/src/pages/api/user/files.ts index 548b367..5a6bec1 100644 --- a/src/pages/api/user/files.ts +++ b/src/pages/api/user/files.ts @@ -2,7 +2,7 @@ import { NextApiReq, NextApiRes, withZipline } from 'middleware/withZipline'; import prisma from 'lib/prisma'; import { chunk } from 'lib/util'; import Logger from 'lib/logger'; -import datasource from 'lib/ds'; +import datasource from 'lib/datasource'; async function handler(req: NextApiReq, res: NextApiRes) { const user = await req.user(); diff --git a/src/pages/api/users.ts b/src/pages/api/users.ts index 7982091..d146ee8 100644 --- a/src/pages/api/users.ts +++ b/src/pages/api/users.ts @@ -1,7 +1,7 @@ import { NextApiReq, NextApiRes, withZipline } from 'middleware/withZipline'; import prisma from 'lib/prisma'; import Logger from 'lib/logger'; -import datasource from 'lib/ds'; +import datasource from 'lib/datasource'; async function handler(req: NextApiReq, res: NextApiRes) { const user = await req.user(); diff --git a/src/server/server.ts b/src/server/server.ts index 3f39e5c..230fad1 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -4,7 +4,7 @@ import { Image, PrismaClient } from '@prisma/client'; import { createServer, IncomingMessage, OutgoingMessage, Server as HttpServer, ServerResponse } from 'http'; import next from 'next'; import config from '../lib/config'; -import datasource from '../lib/ds'; +import datasource from '../lib/datasource'; import { getStats, log, migrations } from './util'; import { mkdir } from 'fs/promises'; import Logger from '../lib/logger'; diff --git a/src/server/util.ts b/src/server/util.ts index 6721d64..3d74a57 100644 --- a/src/server/util.ts +++ b/src/server/util.ts @@ -1,7 +1,7 @@ import { Migrate } from '@prisma/migrate/dist/Migrate'; import { ensureDatabaseExists } from '@prisma/migrate/dist/utils/ensureDatabaseExists'; import Logger from '../lib/logger'; -import { Datasource } from 'lib/datasource'; +import { Datasource } from 'lib/datasources'; import { PrismaClient } from '@prisma/client'; export async function migrations() { diff --git a/zip-env.d.ts b/zip-env.d.ts index 1c7a6da..adbdf22 100644 --- a/zip-env.d.ts +++ b/zip-env.d.ts @@ -1,5 +1,5 @@ import type { PrismaClient } from '@prisma/client'; -import type { Datasource } from 'lib/datasource'; +import type { Datasource } from 'lib/datasources'; import type { Config } from '.lib/types'; declare global {