more video streaming work

This commit is contained in:
subnub
2024-12-23 17:04:31 -05:00
parent 5a1e3044a3
commit 3a00ba51eb
3 changed files with 86 additions and 6 deletions
+46
View File
@@ -9,6 +9,7 @@ import ChunkService from "../services/chunk-service/chunk-service";
import streamToBuffer from "../utils/streamToBuffer";
import NotAuthorizedError from "../utils/NotAuthorizedError";
import { FileListQueryType } from "../types/file-types";
import fs from "fs";
const fileService = new FileService();
type userAccessType = {
@@ -374,6 +375,51 @@ class FileController {
}
};
streamVideoTest = async (
req: RequestTypeFullUser,
res: Response,
next: NextFunction
) => {
if (!req.user) {
return;
}
try {
const headers = req.headers;
console.log("headers", headers.range);
const fileSize = 26867866;
const range = headers.range!;
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunksize = end - start + 1;
const readStream = fs.createReadStream(
"/Users/kylehoell/Developer/myDrive-4/upgrade/old/video.mp4",
{ start, end }
);
const head = {
"Content-Range": "bytes " + start + "-" + end + "/" + fileSize,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "video/mp4",
};
res.writeHead(206, head);
readStream.on("data", (data) => {
res.write(data);
});
readStream.on("end", () => {
res.end();
});
} catch (e: unknown) {
next(e);
}
};
downloadFile = async (
req: RequestTypeFullUser,
res: Response,
@@ -385,6 +385,8 @@ class StorageService {
const chunksize = end - start + 1;
const IV = currentFile.metadata.IV;
console.log("chunksize", start, end, chunksize);
const head = {
"Content-Range": "bytes " + start + "-" + end + "/" + fileSize,
"Accept-Ranges": "bytes",
@@ -423,7 +425,8 @@ class StorageService {
await getFileData(res, fileID, user, currentIV, {
start: start,
end: end,
end,
chunksize,
fixedStart,
fixedEnd,
skip: start - fixedStart,
@@ -25,6 +25,7 @@ const proccessData = (
fixedStart: number;
fixedEnd: number;
skip: number;
chunksize: number;
}
) => {
const eventEmitter = new EventEmitter();
@@ -88,25 +89,54 @@ const proccessData = (
}
if (range) {
const extraBytes = range.fixedEnd - range.end;
let bytesSent = 0;
const skipStream = streamSkip(range.skip);
readStream.pipe(decipher).pipe(skipStream);
skipStream.on("data", (data: Buffer) => {
if (bytesSent + data.length > range.end) {
const neededData = data.slice(0, data.length - extraBytes);
const totalData: Buffer[] = [];
decipher.on("data", (data: Buffer) => {
// console.log("data", data.length);
if (bytesSent + data.length > range.chunksize) {
const currentDataLength = bytesSent + data.length;
const difference = currentDataLength - range.chunksize;
const neededData = data.slice(0, data.length - difference);
console.log("needed data", data.length - difference);
// console.log(
// "needed data",
// neededData.length,
// range.end - range.start,
// bytesSent
// );
res.write(neededData);
totalData.push(neededData);
} else {
res.write(data);
totalData.push(data);
}
//totalData.push(data);
bytesSent += data.length;
});
readStream.pipe(decipher);
// decipher.on("finish", () => {
// eventEmitter.emit("finish");
// });
decipher.on("finish", () => {
// console.log("finish", totalData);
const buffer = Buffer.concat(totalData);
console.log("buffer", buffer.length);
// res.write(buffer);
res.end();
eventEmitter.emit("finish");
});
// readStream
// .pipe(decipher)
// .pipe(res)
// .on("finish", () => {
// eventEmitter.emit("finish");
// });
} else {
readStream
.pipe(decipher)
@@ -136,6 +166,7 @@ const getFileData = (
fixedStart: number;
fixedEnd: number;
skip: number;
chunksize: number;
}
) => {
return new Promise((resolve, reject) => {