2021-11-16 10:20:33 +00:00
|
|
|
import { Config, StorageType } from '../config'
|
|
|
|
import { Local } from './local'
|
|
|
|
|
2021-11-17 15:20:50 +00:00
|
|
|
export abstract class Storage {
|
|
|
|
abstract read(path: string): Promise<Buffer>
|
|
|
|
abstract write(path: string, data: Buffer): Promise<void>
|
|
|
|
abstract exists(path: string): Promise<boolean>
|
|
|
|
abstract delete(path: string): Promise<void>
|
2021-11-16 10:20:33 +00:00
|
|
|
|
2021-11-17 15:20:50 +00:00
|
|
|
abstract readStream(path: string): Promise<NodeJS.ReadableStream>
|
|
|
|
abstract writeStream(path: string): Promise<NodeJS.WritableStream>
|
2021-11-16 10:20:33 +00:00
|
|
|
// list(path: string): Promise<string[]>
|
2021-11-17 15:20:50 +00:00
|
|
|
|
|
|
|
abstract init(): Promise<void>
|
2021-11-16 10:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export let storage: Storage
|
|
|
|
|
2021-11-17 15:20:50 +00:00
|
|
|
export async function init() {
|
2021-11-16 10:20:33 +00:00
|
|
|
if (!storage) {
|
|
|
|
switch (Config.storage) {
|
|
|
|
case StorageType.Local:
|
2021-11-17 16:25:54 +00:00
|
|
|
storage = new Local(Config.localAssets)
|
2021-11-16 10:20:33 +00:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown storage type: ${Config.storage}`)
|
|
|
|
}
|
2021-11-17 15:20:50 +00:00
|
|
|
await storage.init()
|
2021-11-16 10:20:33 +00:00
|
|
|
}
|
|
|
|
}
|