use express static to server /storage

This commit is contained in:
srgooglo 2022-06-06 23:24:24 +02:00
parent 9fd287bc48
commit d4812d44cc
2 changed files with 20 additions and 18 deletions

View File

@ -1,7 +1,7 @@
import { Controller } from "linebridge/dist/server"
import path from "path"
import fs from "fs"
import stream from "stream"
//import stream from "stream"
import pmap from "../../utils/pMap"
const formidable = require("formidable")
@ -9,7 +9,7 @@ const formidable = require("formidable")
function resolveToUrl(filepath, req) {
const host = req ? (req.protocol + "://" + req.get("host")) : global.globalPublicURI
return `${host}/upload/${filepath}`
return `${host}/storage/${filepath}`
}
// TODO: Get maximunFileSize by type of user subscription (free, premium, etc) when `PermissionsAPI` is ready
@ -64,31 +64,31 @@ function videoTranscode(originalFilePath, outputPath, options = {}) {
}
export default class FilesController extends Controller {
get = {
"/upload/:id": {
fn: (req, res) => {
const filePath = path.join(global.uploadPath, req.params?.id)
// get = {
// "/upload/:id": {
// fn: (req, res) => {
// const filePath = path.join(global.uploadPath, req.params?.id)
const readStream = fs.createReadStream(filePath)
const passTrough = new stream.PassThrough()
// const readStream = fs.createReadStream(filePath)
// const passTrough = new stream.PassThrough()
stream.pipeline(readStream, passTrough, (err) => {
if (err) {
return res.status(400)
}
})
// stream.pipeline(readStream, passTrough, (err) => {
// if (err) {
// return res.status(400)
// }
// })
return passTrough.pipe(res)
},
}
}
// return passTrough.pipe(res)
// },
// }
// }
post = {
"/upload": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
let failed = []
// check directories exist
if (!fs.existsSync(global.uploadCachePath)) {
await fs.promises.mkdir(global.uploadCachePath, { recursive: true })

View File

@ -70,6 +70,8 @@ class Server {
this.httpInstance.httpInterface.use(express.json())
this.httpInstance.httpInterface.use(express.urlencoded({ extended: true }))
this.httpInstance.httpInterface.use("/storage", express.static(path.join(__dirname, "../uploads")))
this.httpInstance.wsInterface["clients"] = []
this.httpInstance.wsInterface["findUserIdFromClientID"] = (searchClientId) => {
return this.httpInstance.wsInterface.clients.find(client => client.id === searchClientId)?.userId ?? false