import { FastifyInstance } from 'fastify' import { Config, StorageType } from '../config' import { Local } from './local' import { Minio } from './minio' export abstract class Storage { abstract init(): Promise abstract read(path: string): Promise abstract write(path: string, data: Buffer): Promise abstract readStream(path: string): Promise abstract writeStream(path: string): Promise // list(path: string): Promise abstract exists(path: string): Promise abstract delete(path: string): Promise } export let storage: Storage export async function init(App: FastifyInstance) { if (!storage) { switch (Config.storage) { case StorageType.Local: storage = new Local(Config.localAssets) break case StorageType.S3: // storage = new S3({ // accessKeyId: Config.s3.accessKey, // secretAccessKey: Config.s3.secretKey, // bucket: Config.s3.bucket, // region: Config.s3.region, // }) storage = new Minio({ accessKey: Config.s3.accessKey, secretKey: Config.s3.secretKey, bucket: Config.s3.bucket, region: Config.s3.region, endpoint: 'https://s3.amazonaws.com', }) break case StorageType.Minio: storage = new Minio({ accessKey: Config.minio.accessKey, secretKey: Config.minio.secretKey, endpoint: Config.minio.endpoint, region: Config.minio.region, bucket: Config.minio.bucket, }) break default: throw new Error(`Unknown storage type: ${Config.storage}`) } try { await storage.init() App.log.debug(`Storage initialized: ${Config.storage}`) } catch (e) { App.log.error(`Storage initialization failed: ${Config.storage}`) process.exit(1) } } }