mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
added filesystem driver and storage class controlelr
This commit is contained in:
parent
4e7c3feabb
commit
bae44d48d4
@ -1,24 +1,118 @@
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
|
||||
// TODO: Volatile files
|
||||
// TODO: Redis cache
|
||||
|
||||
class FilesystemDriver {
|
||||
constructor(params) {
|
||||
this.params = params
|
||||
this.params = { ...params }
|
||||
this.rootPath = this.params.root ?? path.resolve(process.cwd(), 'storage')
|
||||
|
||||
this.root = this.params.root ?? path.resolve(process.cwd(), 'storage')
|
||||
this.defaultWriteMode = "0777"
|
||||
this.defaultWriteFlags = 'w+'
|
||||
|
||||
this.matchMimetypes = String()
|
||||
if (Array.isArray(this.params.allowedMimetypes)) {
|
||||
this.params.allowedMimetypes.forEach((type, index) => {
|
||||
if (index == 0) {
|
||||
return this.matchMimetypes = `${type}.*`
|
||||
}
|
||||
return this.matchMimetypes = `${this.matchMimetypes}|${type}.*`
|
||||
})
|
||||
}
|
||||
|
||||
this.initRootPath()
|
||||
}
|
||||
|
||||
set = (key, value, options) => {
|
||||
|
||||
initRootPath() {
|
||||
// check if root exists
|
||||
if (!fs.existsSync(this.rootPath)) {
|
||||
fs.mkdirSync(this.rootPath)
|
||||
}
|
||||
}
|
||||
|
||||
get = () => {
|
||||
checkMimetype(type) {
|
||||
return type.match(this.matchMimetypes)
|
||||
}
|
||||
|
||||
stream = (dir, options) => {
|
||||
const filePath = path.resolve(this.rootPath, dir ?? "")
|
||||
return fs.createWriteStream(filePath, { ...options })
|
||||
}
|
||||
|
||||
set = (file, dir, options, callback) => {
|
||||
const fileParent = path.resolve(this.rootPath, dir ?? "")
|
||||
const filePath = path.join(fileParent, file.name)
|
||||
|
||||
if (!fs.existsSync(fileParent)) {
|
||||
fs.mkdirSync(fileParent)
|
||||
}
|
||||
|
||||
const validMIME = this.checkMimetype(file.mimetype) ? true : false
|
||||
|
||||
if (!validMIME) {
|
||||
throw new Error(`Invalid mimetype [${file.mimetype}]`)
|
||||
}
|
||||
|
||||
const stream = this.stream(filePath, { mode: options?.mode ?? this.defaultWriteMode, flags: options?.flags ?? this.defaultWriteFlags })
|
||||
|
||||
stream.on("ready", () => {
|
||||
stream.write(file.data)
|
||||
stream.close()
|
||||
})
|
||||
|
||||
stream.on("close", () => {
|
||||
if (typeof callback === "function") {
|
||||
return callback(fs.lstatSync(filePath))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
setSync = (file, dir, options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
this.set(file, dir, options, (...context) => {
|
||||
return resolve(...context)
|
||||
})
|
||||
} catch (error) {
|
||||
return reject(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// this didnt work with subdirs, only on root (instead use fetch)
|
||||
get = (key) => {
|
||||
const filePath = path.resolve(this.rootPath, key)
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
throw new Error(`File not found`)
|
||||
}
|
||||
|
||||
return {
|
||||
buffer: fs.readFileSync(filePath),
|
||||
stat: fs.lstatSync(filePath)
|
||||
}
|
||||
}
|
||||
|
||||
getSync = (key) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const file = this.get(key)
|
||||
return resolve(file)
|
||||
} catch (error) {
|
||||
return reject(error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fetch = () => {
|
||||
// *
|
||||
}
|
||||
|
||||
del = () => {
|
||||
|
||||
// *
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { FilesystemDriver }
|
||||
module.exports = FilesystemDriver
|
@ -0,0 +1,3 @@
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
|
@ -1,8 +1,16 @@
|
||||
const path = require('path')
|
||||
|
||||
const drivers = {
|
||||
"fs": require("./drivers/filesystem"),
|
||||
"memfs": require("./drivers/onMemory")
|
||||
}
|
||||
|
||||
// set default allowed file extension
|
||||
const allowedImages = ["png", "jpg", "tiff", "gif", "svg", "jpeg"]
|
||||
const allowedFiles = ["zip"]
|
||||
const allowedAudio = ["wav", "mp3", "ogg", "flac"]
|
||||
const allowedVideo = ["mp4", "mkv"]
|
||||
|
||||
class Storage {
|
||||
constructor(params) {
|
||||
this.params = { ...params }
|
||||
@ -10,26 +18,28 @@ class Storage {
|
||||
this.type = this.params.type
|
||||
this.driver = null
|
||||
|
||||
if (typeof drivers[this.params.driver] !== "undefined") {
|
||||
this.driver = drivers[this.params.driver]
|
||||
}
|
||||
//
|
||||
this.allowedExtensions = [
|
||||
...allowedImages ?? [],
|
||||
...allowedFiles ?? [],
|
||||
...allowedAudio ?? [],
|
||||
...allowedVideo ?? [],
|
||||
...global.allowedExtensions ?? []
|
||||
]
|
||||
this.allowedMimetypes = [
|
||||
"text",
|
||||
"image",
|
||||
"application",
|
||||
...global.allowedMimetypes ?? []
|
||||
]
|
||||
|
||||
if (typeof this.driver !== "undefined") {
|
||||
//
|
||||
if (typeof drivers[this.params.driver] !== "undefined") {
|
||||
return new drivers[this.params.driver](this)
|
||||
} else {
|
||||
throw new Error(`Invalid storage driver!`)
|
||||
}
|
||||
}
|
||||
|
||||
set = (key, value, options) => {
|
||||
this.driver.set(key, value, options)
|
||||
}
|
||||
|
||||
get = (key) => {
|
||||
this.driver.get(key, value, options)
|
||||
}
|
||||
|
||||
del = (key) => {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Storage }
|
@ -2,7 +2,7 @@ const express = require("express")
|
||||
const { objectToArrayMap } = require("@corenode/utils")
|
||||
const uuid = require("uuid")
|
||||
|
||||
const { Controller } = require("./classes/Controller")
|
||||
const { Controller } = require("@@classes")
|
||||
const { getLocalEndpoints, fetchController } = require("./lib/helpers")
|
||||
const SERVER_VERSION = global.SERVER_VERSION = runtime.helpers.getVersion()
|
||||
|
||||
@ -170,11 +170,4 @@ class RequestServer {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { Controller, Server: RequestServer }
|
||||
|
||||
// create default server
|
||||
const defServer = new RequestServer({ autoInit: true })
|
||||
defServer.onRequest()
|
||||
|
||||
|
||||
|
||||
module.exports = { Controller, Server: RequestServer }
|
Loading…
x
Reference in New Issue
Block a user