This commit is contained in:
2021-11-17 16:20:50 +01:00
parent ad13a6f0c1
commit cbc8ab0325
6 changed files with 33 additions and 21 deletions

View File

@@ -1,20 +1,22 @@
import { Config, StorageType } from '../config'
import { Local } from './local'
export interface Storage {
read(path: string): Promise<Buffer>
write(path: string, data: Buffer): Promise<void>
exists(path: string): Promise<boolean>
delete(path: string): Promise<void>
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>
readStream(path: string): Promise<NodeJS.ReadableStream>
writeStream(path: string): Promise<NodeJS.WritableStream>
abstract readStream(path: string): Promise<NodeJS.ReadableStream>
abstract writeStream(path: string): Promise<NodeJS.WritableStream>
// list(path: string): Promise<string[]>
abstract init(): Promise<void>
}
export let storage: Storage
export function init() {
export async function init() {
if (!storage) {
switch (Config.storage) {
case StorageType.Local:
@@ -23,5 +25,6 @@ export function init() {
default:
throw new Error(`Unknown storage type: ${Config.storage}`)
}
await storage.init()
}
}

View File

@@ -1,5 +1,6 @@
import { resolve, join } from 'path'
import fs from 'fs'
import { promisify } from 'util'
import { Storage } from './'
@@ -8,6 +9,10 @@ export class Local implements Storage {
this.root = resolve(root)
}
async init() {
await promisify(fs.mkdir)(this.root, { recursive: true })
}
read(path: string): Promise<Buffer> {
const file = join(this.root, path)
return new Promise((resolve, reject) => {