From 0b96a7e950e6d722bc34afe816caf54b1ad1dade Mon Sep 17 00:00:00 2001 From: subnub Date: Mon, 11 May 2020 11:16:26 -0400 Subject: [PATCH] still working on video streaming --- backend/controllers/file.ts | 6 +++- .../ChunkService/FileSystemService.ts | 28 ++++++++------- backend/services/ChunkService/MongoService.ts | 6 ++-- backend/services/ChunkService/S3Service.ts | 35 ++++++++++--------- .../ChunkService/utils/awaitStreamVideo.ts | 32 ++++++++++++++++- backend/tempStorage/tempStorage.ts | 3 ++ 6 files changed, 76 insertions(+), 34 deletions(-) create mode 100644 backend/tempStorage/tempStorage.ts diff --git a/backend/controllers/file.ts b/backend/controllers/file.ts index 74244da..ae1ab14 100644 --- a/backend/controllers/file.ts +++ b/backend/controllers/file.ts @@ -4,6 +4,8 @@ import MongoService from "../services/ChunkService/MongoService"; import FileSystemService from "../services/ChunkService/FileSystemService"; import S3Service from "../services/ChunkService/S3Service"; import {UserInterface} from "../models/user"; +import uuid from "uuid"; +import tempStorage from "../tempStorage/tempStorage"; const fileService = new FileService() @@ -366,8 +368,10 @@ class FileController { const user = req.user; const fileID = req.params.id; const headers = req.headers; + + //tempStorage[req.params.uuid] = uuid.v4(); - console.log("stream request", req.params.id) + console.log("stream request", tempStorage) // req.on("close", () => { // console.log("req closed stream"); diff --git a/backend/services/ChunkService/FileSystemService.ts b/backend/services/ChunkService/FileSystemService.ts index 284fee9..a6b6a6e 100644 --- a/backend/services/ChunkService/FileSystemService.ts +++ b/backend/services/ChunkService/FileSystemService.ts @@ -256,21 +256,23 @@ class FileSystemService implements ChunkInterface { readStream.pipe(decipher); - req.on("close", () => { - // console.log("Destroying read stream"); - // readStream.destroy(); - // console.log("Read Stream Destoryed"); - }) - - req.on("error", () => { - console.log("req error"); - }) + const tempUUID = req.params.uuid; - req.on("pause", () => { - console.log("req pause") - }) + // req.on("close", () => { + // // console.log("Destroying read stream"); + // // readStream.destroy(); + // // console.log("Read Stream Destoryed"); + // }) - await awaitStreamVideo(start, end, differenceStart, decipher, res, allStreamsToErrorCatch); + // req.on("error", () => { + // console.log("req error"); + // }) + + // req.on("pause", () => { + // console.log("req pause") + // }) + + await awaitStreamVideo(start, end, differenceStart, decipher, res, tempUUID, allStreamsToErrorCatch); } getPublicDownload = async(fileID: string, tempToken: any, res: Response) => { diff --git a/backend/services/ChunkService/MongoService.ts b/backend/services/ChunkService/MongoService.ts index 7d1ccbc..0d8a2fb 100644 --- a/backend/services/ChunkService/MongoService.ts +++ b/backend/services/ChunkService/MongoService.ts @@ -213,7 +213,7 @@ class MongoService implements ChunkInterface { } } - streamVideo = async(user: UserInterface, fileID: string, headers: any, res: Response) => { + streamVideo = async(user: UserInterface, fileID: string, headers: any, res: Response, req: Request) => { const userID = user._id; const currentFile = await dbUtilsFile.getFileInfo(fileID, userID); @@ -279,7 +279,9 @@ class MongoService implements ChunkInterface { readStream.pipe(decipher); - await awaitStreamVideo(start, end, differenceStart, decipher, res, allStreamsToErrorCatch); + const tempUUID = req.params.uuid; + + await awaitStreamVideo(start, end, differenceStart, decipher, res, tempUUID, allStreamsToErrorCatch); } deleteFile = async(userID: string, fileID: string) => { diff --git a/backend/services/ChunkService/S3Service.ts b/backend/services/ChunkService/S3Service.ts index 9fafa39..1136eae 100644 --- a/backend/services/ChunkService/S3Service.ts +++ b/backend/services/ChunkService/S3Service.ts @@ -192,27 +192,28 @@ class S3Service implements ChunkInterface { s3ReadStream.pipe(decipher); - req.on("close", () => { - // console.log("Destoying read stream"); - // s3ReadStream.destroy(); - // console.log("Read Stream Destroyed"); - }) + const tempUUID = req.params.uuid; + // req.on("close", () => { + // // console.log("Destoying read stream"); + // // s3ReadStream.destroy(); + // // console.log("Read Stream Destroyed"); + // }) - req.on("end", () => { - console.log("ending stream"); - s3ReadStream.destroy(); - console.log("ended stream") - }) + // req.on("end", () => { + // console.log("ending stream"); + // s3ReadStream.destroy(); + // console.log("ended stream") + // }) - req.on("error", () => { - console.log("req error"); - }) + // req.on("error", () => { + // console.log("req error"); + // }) - req.on("pause", () => { - console.log("req pause") - }) + // req.on("pause", () => { + // console.log("req pause") + // }) - await awaitStreamVideo(start, end, differenceStart, decipher, res, allStreamsToErrorCatch); + await awaitStreamVideo(start, end, differenceStart, decipher, res, tempUUID, allStreamsToErrorCatch); } getThumbnail = async(user: UserInterface, id: string) => { diff --git a/backend/services/ChunkService/utils/awaitStreamVideo.ts b/backend/services/ChunkService/utils/awaitStreamVideo.ts index 4374f8d..300cc86 100644 --- a/backend/services/ChunkService/utils/awaitStreamVideo.ts +++ b/backend/services/ChunkService/utils/awaitStreamVideo.ts @@ -1,7 +1,12 @@ import {Response, Request} from "express" +import tempStorage from "../../../tempStorage/tempStorage"; +import uuid from "uuid"; const awaitStreamVideo = (start: number, end:number, differenceStart: number, - decipher: any, res: Response, streamsToErrorCatch: any[]) => { + decipher: any, res: Response, tempUUID: string, streamsToErrorCatch: any[]) => { + + const currentUUID = uuid.v4(); + tempStorage[tempUUID] = currentUUID; return new Promise((resolve, reject) => { @@ -10,6 +15,31 @@ const awaitStreamVideo = (start: number, end:number, differenceStart: number, decipher.on("data", (data: Buffer | string) => { + //console.log("check", tempStorage[tempUUID]); + //console.log("check b", tempStorage[tempUUID] !== currentUUID && tempStorage[tempUUID] !== undefined); + + //console.log(tempStorage[tempUUID] !== currentUUID, tempStorage[tempUUID]) + if (tempStorage[tempUUID] !== currentUUID) { + + console.log("New Stream Requested, Desroying old stream"); + streamsToErrorCatch[0].destroy(); + console.log("Old Stream Desroyed"); + //delete tempStorage[tempUUID]; + } + + // if (tempStorage[tempUUID] !== undefined && tempStorage[tempUUID] !== currentUUID) { + + // console.log("New Stream Requested, Desroying old stream"); + // streamsToErrorCatch[0].destroy(); + // console.log("Old Stream Desroyed"); + // delete tempStorage[tempUUID]; + + // } else { + + // tempStorage[tempUUID] = currentUUID; + // } + + if (+start === 0 && +end === 1) { const dataCoverted = data.toString("hex"); diff --git a/backend/tempStorage/tempStorage.ts b/backend/tempStorage/tempStorage.ts new file mode 100644 index 0000000..270004f --- /dev/null +++ b/backend/tempStorage/tempStorage.ts @@ -0,0 +1,3 @@ +const tempStorage: any = {}; + +export default tempStorage; \ No newline at end of file