refactor: clean up datasource stuff
This commit is contained in:
parent
1e2b8efb13
commit
8fea0cbe77
19 changed files with 40 additions and 29 deletions
|
@ -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',
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -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');
|
|
@ -1,4 +0,0 @@
|
|||
export { Datasource } from './datasource';
|
||||
export { Local } from './Local';
|
||||
export { S3 } from './S3';
|
||||
|
|
@ -2,7 +2,7 @@ import { Readable } from 'stream';
|
|||
|
||||
export abstract class Datasource {
|
||||
public name: string;
|
||||
|
||||
|
||||
public abstract save(file: string, data: Buffer): Promise<void>;
|
||||
public abstract delete(file: string): Promise<void>;
|
||||
public abstract get(file: string): Readable;
|
|
@ -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';
|
|
@ -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';
|
3
src/lib/datasources/index.ts
Normal file
3
src/lib/datasources/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export { Datasource } from './Datasource';
|
||||
export { Local } from './Local';
|
||||
export { S3 } from './S3';
|
|
@ -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}`;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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() {
|
||||
|
|
2
zip-env.d.ts
vendored
2
zip-env.d.ts
vendored
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue