mirror of
https://github.com/cupcakearmy/morphus.git
synced 2025-09-06 00:00:40 +00:00
initial
This commit is contained in:
27
src/storage/index.ts
Normal file
27
src/storage/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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>
|
||||
|
||||
readStream(path: string): Promise<NodeJS.ReadableStream>
|
||||
writeStream(path: string): Promise<NodeJS.WritableStream>
|
||||
// list(path: string): Promise<string[]>
|
||||
}
|
||||
|
||||
export let storage: Storage
|
||||
|
||||
export function init() {
|
||||
if (!storage) {
|
||||
switch (Config.storage) {
|
||||
case StorageType.Local:
|
||||
storage = new Local(Config.assets)
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown storage type: ${Config.storage}`)
|
||||
}
|
||||
}
|
||||
}
|
76
src/storage/local.ts
Normal file
76
src/storage/local.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { resolve, join } from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
import { Storage } from './'
|
||||
|
||||
export class Local implements Storage {
|
||||
constructor(private readonly root: string) {
|
||||
this.root = resolve(root)
|
||||
}
|
||||
|
||||
read(path: string): Promise<Buffer> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
resolve(data)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
write(path: string, data: Buffer): Promise<void> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.writeFile(file, data, (err) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exists(path: string): Promise<boolean> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.access(file, fs.constants.F_OK, (err) => {
|
||||
if (err) {
|
||||
return resolve(false)
|
||||
}
|
||||
resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
delete(path: string): Promise<void> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.unlink(file, (err) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
readStream(path: string): Promise<NodeJS.ReadableStream> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
const stream = fs.createReadStream(file)
|
||||
stream.on('error', reject)
|
||||
resolve(stream)
|
||||
})
|
||||
}
|
||||
|
||||
writeStream(path: string): Promise<NodeJS.WritableStream> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
const stream = fs.createWriteStream(file)
|
||||
stream.on('error', reject)
|
||||
resolve(stream)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user