From feb2e939fb1d020c0aec8383a395652fe1c738c7 Mon Sep 17 00:00:00 2001 From: subnub Date: Thu, 26 Dec 2024 04:56:02 -0500 Subject: [PATCH] removed unneeded files --- .../utils/createVideoThumbnail.ts | 4 +- .../utils/createVideoThumbnailFS.ts | 115 --------- .../utils/tempCreateVideoThumbnail.ts | 226 ++++++++---------- .../utils/tempCreateVideoThumbnailFS.ts | 136 ----------- public/.DS_Store | Bin 8196 -> 8196 bytes 5 files changed, 98 insertions(+), 383 deletions(-) delete mode 100644 backend/services/chunk-service/utils/createVideoThumbnailFS.ts delete mode 100644 backend/services/chunk-service/utils/tempCreateVideoThumbnailFS.ts diff --git a/backend/services/chunk-service/utils/createVideoThumbnail.ts b/backend/services/chunk-service/utils/createVideoThumbnail.ts index debb611..cff4710 100644 --- a/backend/services/chunk-service/utils/createVideoThumbnail.ts +++ b/backend/services/chunk-service/utils/createVideoThumbnail.ts @@ -10,7 +10,7 @@ import env from "../../../enviroment/env"; import { ObjectId } from "mongodb"; import File from "../../../models/file-model"; import ffmpeg from "fluent-ffmpeg"; -import tempCreateVideoThumbnailFS from "./tempCreateVideoThumbnailFS"; +import tempCreateVideoThumbnail from "./tempCreateVideoThumbnail"; import { S3Actions } from "../actions/S3-actions"; import { FilesystemActions } from "../actions/file-system-actions"; import { createGenericParams } from "./storageHelper"; @@ -163,7 +163,7 @@ const createVideoThumbnail = ( await attemptToRemoveChunks(file, thumbnailFilename); if (env.tempDirectory) { - const updatedFile = await tempCreateVideoThumbnailFS( + const updatedFile = await tempCreateVideoThumbnail( file, filename, user diff --git a/backend/services/chunk-service/utils/createVideoThumbnailFS.ts b/backend/services/chunk-service/utils/createVideoThumbnailFS.ts deleted file mode 100644 index 8a60c65..0000000 --- a/backend/services/chunk-service/utils/createVideoThumbnailFS.ts +++ /dev/null @@ -1,115 +0,0 @@ -import mongoose from "../../../db/connections/mongoose"; -import crypto from "crypto"; -import Thumbnail from "../../../models/thumbnail-model"; -import sharp from "sharp"; -import { FileInterface } from "../../../models/file-model"; -import { UserInterface } from "../../../models/user-model"; -import fs from "fs"; -import uuid from "uuid"; -import env from "../../../enviroment/env"; -import { ObjectId } from "mongodb"; -import File from "../../../models/file-model"; -import ffmpeg from "fluent-ffmpeg"; -import tempCreateVideoThumbnailFS from "./tempCreateVideoThumbnailFS"; - -const createVideoThumbnailFS = ( - file: FileInterface, - filename: string, - user: UserInterface -) => { - return new Promise(async (resolve, reject) => { - const password = user.getEncryptionKey(); - - let CIPHER_KEY = crypto.createHash("sha256").update(password!).digest(); - - const thumbnailFilename = uuid.v4(); - - const readStream = fs.createReadStream(file.metadata.filePath!); - console.log("env", env); - const writeStream = fs.createWriteStream( - env.fsDirectory + thumbnailFilename - ); - const decipher = crypto.createDecipheriv( - "aes256", - CIPHER_KEY, - file.metadata.IV - ); - - const thumbnailIV = crypto.randomBytes(16); - - const thumbnailCipher = crypto.createCipheriv( - "aes256", - CIPHER_KEY, - thumbnailIV - ); - - const decryptedReadStream = readStream.pipe(decipher); - - ffmpeg(decryptedReadStream, { - timeout: 60, - }) - .seek(1) - .format("image2pipe") - .outputOptions([ - "-f image2pipe", - "-vframes 1", - "-vf scale='if(gt(iw,ih),600,-1):if(gt(ih,iw),300,-1)'", - ]) - .on("start", (command) => { - /** - * log - */ - }) - .on("end", async () => { - console.log("end"); - const thumbnailModel = new Thumbnail({ - name: filename, - owner: user._id, - IV: thumbnailIV, - path: env.fsDirectory + thumbnailFilename, - }); - - await thumbnailModel.save(); - if (!file._id) { - return reject(); - } - const updateFileResponse = await File.updateOne( - { _id: new ObjectId(file._id), "metadata.owner": user._id }, - { - $set: { - "metadata.hasThumbnail": true, - "metadata.thumbnailID": thumbnailModel._id, - }, - } - ); - if (updateFileResponse.modifiedCount === 0) { - return reject(); - } - - const updatedFile = await File.findById({ - _id: new ObjectId(file._id), - "metadata.owner": user._id, - }); - - if (!updatedFile) return reject(); - - resolve(updatedFile?.toObject()); - }) - .on("error", async (err, _, stderr) => { - console.log("thumbnail error attempting temp directory fix"); - const updatedFile = await tempCreateVideoThumbnailFS( - file, - filename, - user - ); - resolve(updatedFile); - /** - * log - */ - }) - .pipe(thumbnailCipher) - .pipe(writeStream, { end: true }); - }); -}; - -export default createVideoThumbnailFS; diff --git a/backend/services/chunk-service/utils/tempCreateVideoThumbnail.ts b/backend/services/chunk-service/utils/tempCreateVideoThumbnail.ts index fdefdfb..853dcc5 100644 --- a/backend/services/chunk-service/utils/tempCreateVideoThumbnail.ts +++ b/backend/services/chunk-service/utils/tempCreateVideoThumbnail.ts @@ -22,142 +22,108 @@ const tempCreateVideoThumbnail = ( filename: string, user: UserInterface ) => { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { + const password = user.getEncryptionKey(); + + let CIPHER_KEY = crypto.createHash("sha256").update(password!).digest(); + const thumbnailFilename = uuid.v4(); - const tempDirectory = env.fsDirectory + "temp/" + thumbnailFilename; - const cleanup = () => { + + const readStreamParams = createGenericParams({ + filePath: file.metadata.filePath, + Key: file.metadata.s3ID, + }); + + const readStream = storageActions.createReadStream(readStreamParams); + + const { writeStream, emitter } = storageActions.createWriteStream( + readStreamParams, + readStream, + thumbnailFilename + ); + + const tempDirectory = env.tempDirectory + thumbnailFilename; + const tempWriteStream = fs.createWriteStream(tempDirectory); + const decipher = crypto.createDecipheriv( + "aes256", + CIPHER_KEY, + file.metadata.IV + ); + + const thumbnailIV = crypto.randomBytes(16); + + const thumbnailCipher = crypto.createCipheriv( + "aes256", + CIPHER_KEY, + thumbnailIV + ); + + const decryptedReadStream = readStream.pipe(decipher); + + decryptedReadStream.pipe(tempWriteStream, { end: true }); + + if (emitter) { + emitter.on("finish", async () => { + await handleFinish(); + }); + } + + const handleFinish = async () => { + const thumbnailModel = new Thumbnail({ + name: filename, + owner: user._id, + IV: thumbnailIV, + path: env.fsDirectory + thumbnailFilename, + s3ID: thumbnailFilename, + }); + + await thumbnailModel.save(); + if (!file._id) { + return reject(); + } + const updatedFile = await File.findOneAndUpdate( + { _id: new ObjectId(file._id), "metadata.owner": user._id }, + { + $set: { + "metadata.hasThumbnail": true, + "metadata.thumbnailID": thumbnailModel._id, + "metadata.isVideo": true, + }, + }, + { new: true } + ); + + if (!updatedFile) return reject(); + fs.unlink(tempDirectory, (err) => { - if (err) console.error("CLEANUP ERROR:", err); + resolve(updatedFile); }); }; - try { - const password = user.getEncryptionKey(); - - let CIPHER_KEY = crypto.createHash("sha256").update(password!).digest(); - - const readStreamParams = createGenericParams({ - filePath: file.metadata.filePath, - Key: file.metadata.s3ID, - }); - - const readStream = storageActions.createReadStream(readStreamParams); - - const { writeStream, emitter } = storageActions.createWriteStream( - readStreamParams, - readStream, - thumbnailFilename - ); - - const tempWriteStream = fs.createWriteStream(tempDirectory); - const decipher = crypto.createDecipheriv( - "aes256", - CIPHER_KEY, - file.metadata.IV - ); - - const thumbnailIV = crypto.randomBytes(16); - - const thumbnailCipher = crypto.createCipheriv( - "aes256", - CIPHER_KEY, - thumbnailIV - ); - - const handleError = (e: Error) => { - console.log("thumbnail error", e); - cleanup(); - resolve(file); - }; - - const decryptedReadStream = readStream.pipe(decipher); - - readStream.on("error", handleError); - - decipher.on("error", handleError); - - writeStream.on("error", handleError); - - thumbnailCipher.on("error", handleError); - - decryptedReadStream.on("error", handleError); - - tempWriteStream.on("error", handleError); - - decryptedReadStream.pipe(tempWriteStream, { end: true }); - - const handleFinish = async () => { - try { - const thumbnailModel = new Thumbnail({ - name: filename, - owner: user._id, - IV: thumbnailIV, - path: env.fsDirectory + thumbnailFilename, - }); - - await thumbnailModel.save(); - if (!file._id) { - return reject(); - } - const updateFileResponse = await File.updateOne( - { _id: new ObjectId(file._id), "metadata.owner": user._id }, - { - $set: { - "metadata.hasThumbnail": true, - "metadata.thumbnailID": thumbnailModel._id, - }, - } - ); - if (updateFileResponse.modifiedCount === 0) { - return reject(); - } - - const updatedFile = await File.findById({ - _id: new ObjectId(file._id), - "metadata.owner": user._id, - }); - - if (!updatedFile) return reject(); - - cleanup(); - - resolve(updatedFile?.toObject()); - } catch (e) { - console.log("thumbnail error", e); - cleanup(); - resolve(file); - } - }; - - tempWriteStream.on("finish", () => { - ffmpeg(tempDirectory, { - timeout: 60, - }) - .seek(1) - .format("image2pipe") - .outputOptions([ - "-f image2pipe", - "-vframes 1", - "-vf scale='if(gt(iw,ih),600,-1):if(gt(ih,iw),300,-1)'", - ]) - .on("start", (command) => {}) - .on("end", async () => { - console.log("end"); + tempWriteStream.on("finish", () => { + ffmpeg(tempDirectory, { + timeout: 60, + }) + .seek(1) + .format("image2pipe") + .outputOptions([ + "-f image2pipe", + "-vframes 1", + "-vf scale='if(gt(iw,ih),600,-1):if(gt(ih,iw),300,-1)'", + ]) + .on("start", () => {}) + .on("end", async () => { + if (!emitter) { await handleFinish(); - }) - .on("error", (err, _, stderr) => { - console.log("error", err, stderr); - cleanup(); - resolve(file); - }) - .pipe(thumbnailCipher) - .pipe(writeStream, { end: true }); - }); - } catch (e) { - console.log("thumbnail error", e); - cleanup(); - resolve(file); - } + } + }) + .on("error", (err, _, stderr) => { + console.log("error", err, stderr); + resolve(file); + }) + .pipe(thumbnailCipher) + .pipe(writeStream, { end: true }); + }); }); }; diff --git a/backend/services/chunk-service/utils/tempCreateVideoThumbnailFS.ts b/backend/services/chunk-service/utils/tempCreateVideoThumbnailFS.ts deleted file mode 100644 index 52bfa53..0000000 --- a/backend/services/chunk-service/utils/tempCreateVideoThumbnailFS.ts +++ /dev/null @@ -1,136 +0,0 @@ -import mongoose from "../../../db/connections/mongoose"; -import crypto from "crypto"; -import Thumbnail from "../../../models/thumbnail-model"; -import sharp from "sharp"; -import { FileInterface } from "../../../models/file-model"; -import { UserInterface } from "../../../models/user-model"; -import fs from "fs"; -import uuid from "uuid"; -import env from "../../../enviroment/env"; -import { ObjectId } from "mongodb"; -import File from "../../../models/file-model"; -import ffmpeg from "fluent-ffmpeg"; -import { S3Actions } from "../actions/S3-actions"; -import { FilesystemActions } from "../actions/file-system-actions"; -import { createGenericParams } from "./storageHelper"; -import { getStorageActions } from "../actions/helper-actions"; - -const storageActions = getStorageActions(); - -const tempCreateVideoThumbnailFS = ( - file: FileInterface, - filename: string, - user: UserInterface -) => { - return new Promise(async (resolve, reject) => { - const password = user.getEncryptionKey(); - - let CIPHER_KEY = crypto.createHash("sha256").update(password!).digest(); - - const thumbnailFilename = uuid.v4(); - - const readStreamParams = createGenericParams({ - filePath: file.metadata.filePath, - Key: file.metadata.s3ID, - }); - - const readStream = storageActions.createReadStream(readStreamParams); - - const { writeStream, emitter } = storageActions.createWriteStream( - readStreamParams, - readStream, - thumbnailFilename - ); - - const tempDirectory = env.tempDirectory + thumbnailFilename; - const tempWriteStream = fs.createWriteStream(tempDirectory); - const decipher = crypto.createDecipheriv( - "aes256", - CIPHER_KEY, - file.metadata.IV - ); - - const thumbnailIV = crypto.randomBytes(16); - - const thumbnailCipher = crypto.createCipheriv( - "aes256", - CIPHER_KEY, - thumbnailIV - ); - - const decryptedReadStream = readStream.pipe(decipher); - - decryptedReadStream.pipe(tempWriteStream, { end: true }); - - if (emitter) { - emitter.on("finish", async () => { - await handleFinish(); - }); - } - - const handleFinish = async () => { - const thumbnailModel = new Thumbnail({ - name: filename, - owner: user._id, - IV: thumbnailIV, - path: env.fsDirectory + thumbnailFilename, - s3ID: thumbnailFilename, - }); - - await thumbnailModel.save(); - if (!file._id) { - return reject(); - } - const updateFileResponse = await File.updateOne( - { _id: new ObjectId(file._id), "metadata.owner": user._id }, - { - $set: { - "metadata.hasThumbnail": true, - "metadata.thumbnailID": thumbnailModel._id, - }, - } - ); - if (updateFileResponse.modifiedCount === 0) { - return reject(); - } - - const updatedFile = await File.findById({ - _id: new ObjectId(file._id), - "metadata.owner": user._id, - }); - - if (!updatedFile) return reject(); - - fs.unlink(tempDirectory, (err) => { - resolve(updatedFile?.toObject()); - }); - }; - - tempWriteStream.on("finish", () => { - ffmpeg(tempDirectory, { - timeout: 60, - }) - .seek(1) - .format("image2pipe") - .outputOptions([ - "-f image2pipe", - "-vframes 1", - "-vf scale='if(gt(iw,ih),600,-1):if(gt(ih,iw),300,-1)'", - ]) - .on("start", () => {}) - .on("end", async () => { - if (!emitter) { - await handleFinish(); - } - }) - .on("error", (err, _, stderr) => { - console.log("error", err, stderr); - resolve(file); - }) - .pipe(thumbnailCipher) - .pipe(writeStream, { end: true }); - }); - }); -}; - -export default tempCreateVideoThumbnailFS; diff --git a/public/.DS_Store b/public/.DS_Store index c88bbcc42827eebf7b66751c56e3f2c71d6db3c3..c81ecd4daef284d05a3b95dbed4a41847e59b150 100755 GIT binary patch delta 180 zcmZp1XmOa}&&|TXz`)4BAi%(ox-n`azb}Z-#*oR7%aF*B&XCGboK#+1kd%|31Qb7+ zRFIQdTw-8woso%|g_Vt+gM*8Miq2ING!=nN}QZ4;K|4_ zdB32dp17)^t!F}RWmR=eZQV?uVL-qLv=|Kdp)`z|wV6lgH|xd*X~xa$65m)hONhN^ J+}I$&2ml}PDzX3o delta 392 zcmZp1XmOa}&&|fbz`)4BAi%&-vN38Se-en##*oNR41}o+B@D$$<;4X_Ir&LI@h?dQ zIhn;J1_n16nV4Bv+1NQaxwtvG`M5b_gER8WgG&-iN{gKmi{b@jf-_T6lM;)%s#fJ_GhP7cm^0rBc;69XLuBXhG_ z9ffLhb0FKq*sQjelY>K2-7P4tw5e~_>?KQ=EkAe&qKgseelQS((lF{Ukb&Ztvf!e; zocz3W24>S)2$ljS8F5vA0c9=wUwW*;Y&}A0JV3(O016^ij t2y&UIvc7dte0EN5UVazEcDSQ>VXlGF-J3rO{AS(EF7b_J^CMw)W&joFTc7{{