UNPKG

uyem

Version:
103 lines 3.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const interfaces_1 = require("../types/interfaces"); const constants_1 = require("../utils/constants"); const lib_1 = require("../utils/lib"); const db_1 = __importDefault(require("./db")); class Http extends db_1.default { cloudPath; constructor({ prisma, cloudPath }) { super({ prisma }); this.cloudPath = cloudPath; } async getVideoHandler({ isDefaultAuth, res, unitId, url }) { const id = url.replace(constants_1.VIDEO_REGEX, '').replace(new RegExp(`${interfaces_1.EXT_WEBM}$`), ''); const video = await this.videoFindFirst({ where: { id, }, include: { Room: { select: { authorId: true, }, }, }, }); if (!video) { res.writeHead(video === undefined ? 500 : 404); res.end(); return; } // Check author if (unitId !== video.Room.authorId && !isDefaultAuth) { res.writeHead(401); res.end(); return; } const videoPath = (0, lib_1.getVideoPath)({ cloudPath: this.cloudPath, roomId: video.roomId, name: video.name, }); const stream = fs_1.default.createReadStream(videoPath); res.writeHead(200, { 'Content-Type': 'video/webm' }); stream.pipe(res); } async getTmpHandler({ res, url, unitId, isDefaultAuth, req }) { const urlArr = url.replace(constants_1.TMP_REGEX, '').split(interfaces_1.DELIMITER); const id = urlArr[0]; const room = await this.roomFindFirst({ where: { id }, }); if (!room) { res.writeHead(room === undefined ? 500 : 404); res.end(); return; } // Check author if (unitId !== room.authorId && !isDefaultAuth) { res.writeHead(401); res.end(); return; } const isWebm = constants_1.WEBM_REGEX.test(url); if (isWebm) { const stream = fs_1.default.createReadStream(path_1.default.resolve(this.cloudPath, `./${url}`)); res.writeHead(200, { 'Content-Type': 'video/webm' }); stream.pipe(res); } else { const dir = await new Promise((resolve) => { fs_1.default.readdir(path_1.default.resolve(this.cloudPath, `./${url}`), (err, data) => { if (err) { (0, lib_1.log)('error', 'Error read tmp dir', { err, url, headers: req.headers, unitId, isDefaultAuth, }); resolve(1); } resolve(data); }); }); if (dir === 1) { res.writeHead(410); res.end(); return; } res.writeHead(200, { 'Content-Type': 'application/json' }); res.write(JSON.stringify(dir)); res.end(); } } } exports.default = Http; //# sourceMappingURL=http.js.map