mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
added stream
controller to fileserver
This commit is contained in:
parent
ac1cfb42d6
commit
9dedf59994
20
packages/file_server/src/controllers/stream/index.js
Normal file
20
packages/file_server/src/controllers/stream/index.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import path from "path"
|
||||||
|
import createRoutesFromDirectory from "@utils/createRoutesFromDirectory"
|
||||||
|
import getMiddlewares from "@utils/getMiddlewares"
|
||||||
|
|
||||||
|
export default async (router) => {
|
||||||
|
const routesPath = path.resolve(__dirname, "routes")
|
||||||
|
|
||||||
|
const middlewares = await getMiddlewares(["withOptionalAuth"])
|
||||||
|
|
||||||
|
for (const middleware of middlewares) {
|
||||||
|
router.use(middleware)
|
||||||
|
}
|
||||||
|
|
||||||
|
router = createRoutesFromDirectory("routes", routesPath, router)
|
||||||
|
|
||||||
|
return {
|
||||||
|
path: "/stream",
|
||||||
|
router,
|
||||||
|
}
|
||||||
|
}
|
24
packages/file_server/src/controllers/stream/routes/get/*.js
Normal file
24
packages/file_server/src/controllers/stream/routes/get/*.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { NotFoundError, InternalServerError } from "@shared-classes/Errors"
|
||||||
|
import mimetypes from "mime-types"
|
||||||
|
|
||||||
|
export default async (req, res) => {
|
||||||
|
const streamPath = req.params[0]
|
||||||
|
|
||||||
|
global.storage.getObject(process.env.S3_BUCKET, streamPath, (err, dataStream) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(err)
|
||||||
|
return new InternalServerError(req, res, "Error while getting file from storage")
|
||||||
|
}
|
||||||
|
|
||||||
|
const extname = mimetypes.lookup(streamPath)
|
||||||
|
|
||||||
|
// send chunked response
|
||||||
|
res.status(200)
|
||||||
|
|
||||||
|
// set headers
|
||||||
|
res.setHeader("Content-Type", extname)
|
||||||
|
res.setHeader("Accept-Ranges", "bytes")
|
||||||
|
|
||||||
|
return dataStream.pipe(res)
|
||||||
|
})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user