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 { Controller } from "linebridge/dist/server"
import path from "path" import path from "path"
import fs from "fs" import fs from "fs"
import stream from "stream" //import stream from "stream"
import pmap from "../../utils/pMap" import pmap from "../../utils/pMap"
const formidable = require("formidable") const formidable = require("formidable")
@ -9,7 +9,7 @@ const formidable = require("formidable")
function resolveToUrl(filepath, req) { function resolveToUrl(filepath, req) {
const host = req ? (req.protocol + "://" + req.get("host")) : global.globalPublicURI 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 // 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 { export default class FilesController extends Controller {
get = { // get = {
"/upload/:id": { // "/upload/:id": {
fn: (req, res) => { // fn: (req, res) => {
const filePath = path.join(global.uploadPath, req.params?.id) // const filePath = path.join(global.uploadPath, req.params?.id)
const readStream = fs.createReadStream(filePath) // const readStream = fs.createReadStream(filePath)
const passTrough = new stream.PassThrough() // const passTrough = new stream.PassThrough()
stream.pipeline(readStream, passTrough, (err) => { // stream.pipeline(readStream, passTrough, (err) => {
if (err) { // if (err) {
return res.status(400) // return res.status(400)
} // }
}) // })
return passTrough.pipe(res) // return passTrough.pipe(res)
}, // },
} // }
} // }
post = { post = {
"/upload": { "/upload": {
middlewares: ["withAuthentication"], middlewares: ["withAuthentication"],
fn: async (req, res) => { fn: async (req, res) => {
let failed = [] let failed = []
// check directories exist // check directories exist
if (!fs.existsSync(global.uploadCachePath)) { if (!fs.existsSync(global.uploadCachePath)) {
await fs.promises.mkdir(global.uploadCachePath, { recursive: true }) 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.json())
this.httpInstance.httpInterface.use(express.urlencoded({ extended: true })) 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["clients"] = []
this.httpInstance.wsInterface["findUserIdFromClientID"] = (searchClientId) => { this.httpInstance.wsInterface["findUserIdFromClientID"] = (searchClientId) => {
return this.httpInstance.wsInterface.clients.find(client => client.id === searchClientId)?.userId ?? false return this.httpInstance.wsInterface.clients.find(client => client.id === searchClientId)?.userId ?? false