From d4812d44ccf728145fce3d4274c5baf497f28dfc Mon Sep 17 00:00:00 2001 From: srgooglo Date: Mon, 6 Jun 2022 23:24:24 +0200 Subject: [PATCH] use express static to server `/storage` --- .../src/controllers/FilesController/index.js | 36 +++++++++---------- packages/server/src/index.js | 2 ++ 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/server/src/controllers/FilesController/index.js b/packages/server/src/controllers/FilesController/index.js index 1f3e1413..4a9801a1 100644 --- a/packages/server/src/controllers/FilesController/index.js +++ b/packages/server/src/controllers/FilesController/index.js @@ -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 }) diff --git a/packages/server/src/index.js b/packages/server/src/index.js index d5512215..31e43d21 100644 --- a/packages/server/src/index.js +++ b/packages/server/src/index.js @@ -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