Cleaned up most of the backend, moved some of the utilties into their proper location
This commit is contained in:
@@ -13,14 +13,13 @@ import File, { FileInterface } from "../../models/file";
|
||||
import getFileSize from "./utils/getFileSize";
|
||||
import DbUtilFile from "../../db/utils/fileUtils/index";
|
||||
import awaitStream from "./utils/awaitStream";
|
||||
import createThumbnailFS from "../FileService/utils/createThumbnailFS";
|
||||
import createThumbnailFS from "./utils/createThumbnailFS";
|
||||
import imageChecker from "../../utils/imageChecker";
|
||||
import Thumbnail, {ThumbnailInterface} from "../../models/thumbnail";
|
||||
import streamToBuffer from "../../utils/streamToBuffer";
|
||||
import User from "../../models/user";
|
||||
import env from "../../enviroment/env";
|
||||
import { removeChunksFS } from "./utils/awaitUploadStreamFS";
|
||||
import removeTempToken from "./utils/removeTempToken";
|
||||
import removeChunksFS from "./utils/removeChunksFS";
|
||||
import getPrevIVFS from "./utils/getPrevIVFS";
|
||||
import awaitStreamVideo from "./utils/awaitStreamVideo";
|
||||
import fixStartChunkLength from "./utils/fixStartChunkLength";
|
||||
@@ -28,7 +27,6 @@ import Folder from "../../models/folder";
|
||||
|
||||
const dbUtilsFile = new DbUtilFile();
|
||||
|
||||
// implements ChunkInterface
|
||||
class FileSystemService implements ChunkInterface {
|
||||
|
||||
constructor() {
|
||||
@@ -232,7 +230,7 @@ class FileSystemService implements ChunkInterface {
|
||||
fixedStart = 0;
|
||||
}
|
||||
|
||||
const fixedEnd = currentFile.length; //end % 16 === 0 ? end + 15: (fixEndChunkLength(end) - 1) + 16;
|
||||
const fixedEnd = currentFile.length;
|
||||
|
||||
const differenceStart = start - fixedStart;
|
||||
|
||||
|
||||
@@ -6,22 +6,17 @@ import crypto from "crypto";
|
||||
import videoChecker from "../../utils/videoChecker"
|
||||
import imageChecker from "../../utils/imageChecker"
|
||||
import { ObjectID } from "mongodb";
|
||||
import createThumbnail from "../FileService/utils/createThumbnail";
|
||||
import createThumbnail from "./utils/createThumbnail";
|
||||
import Thumbnail, {ThumbnailInterface} from "../../models/thumbnail";
|
||||
import NotAuthorizedError from "../../utils/NotAuthorizedError";
|
||||
import InternalServerError from "../../utils/InternalServerError";
|
||||
import NotFoundError from "../../utils/NotFoundError";
|
||||
import {GridFSBucketWriteStream} from "mongodb";
|
||||
import awaitStream from "./utils/awaitStream";
|
||||
import awaitUploadStream from "./utils/awaitUploadStream";
|
||||
|
||||
import User, { UserInterface } from "../../models/user";
|
||||
import Folder from "../../models/folder";
|
||||
import { FileInterface } from "../../models/file";
|
||||
import { Stream } from "stream";
|
||||
import removeChunks from "../FileService/utils/removeChunks";
|
||||
import getBusboyData from "./utils/getBusboyData";
|
||||
|
||||
import ChunkInterface from "./utils/ChunkInterface";
|
||||
import fixStartChunkLength from "./utils/fixStartChunkLength";
|
||||
import getPrevIVMongo from "./utils/getPrevIVMongo";
|
||||
@@ -30,7 +25,6 @@ import awaitStreamVideo from "./utils/awaitStreamVideo";
|
||||
const conn = mongoose.connection;
|
||||
|
||||
const dbUtilsFile = new DbUtilFile();
|
||||
const dbUtilsFolder = new DbUtilFolder();
|
||||
|
||||
class MongoService implements ChunkInterface {
|
||||
|
||||
@@ -256,7 +250,7 @@ class MongoService implements ChunkInterface {
|
||||
fixedStart = 0;
|
||||
}
|
||||
|
||||
const fixedEnd = currentFile.length; //end % 16 === 0 ? end + 15: (fixEndChunkLength(end) - 1) + 16;
|
||||
const fixedEnd = currentFile.length;
|
||||
|
||||
const differenceStart = start - fixedStart;
|
||||
|
||||
@@ -303,14 +297,6 @@ class MongoService implements ChunkInterface {
|
||||
await Thumbnail.deleteOne({_id: file.metadata.thumbnailID});
|
||||
}
|
||||
|
||||
// if (file.metadata.isVideo && file.metadata.transcoded) {
|
||||
// try {
|
||||
// await bucket.delete(new ObjectID(file.metadata.transcodedID));
|
||||
// } catch (e) {
|
||||
// console.log("Could Not Find Transcoded Video");
|
||||
// }
|
||||
// }
|
||||
|
||||
await bucket.delete(new ObjectID(fileID));
|
||||
}
|
||||
|
||||
@@ -358,10 +344,6 @@ class MongoService implements ChunkInterface {
|
||||
chunkSizeBytes: 1024 * 255
|
||||
});
|
||||
|
||||
const videoBucket = new mongoose.mongo.GridFSBucket(conn.db, {
|
||||
bucketName: "videos"
|
||||
});
|
||||
|
||||
await Folder.deleteMany({"owner": userID});
|
||||
|
||||
const fileList = await dbUtilsFile.getFileListByOwner(userID);
|
||||
|
||||
@@ -2,7 +2,6 @@ import File, { FileInterface } from "../../models/file";
|
||||
import User, {UserInterface} from "../../models/user";
|
||||
import s3 from "../../db/s3";
|
||||
import env from "../../enviroment/env";
|
||||
|
||||
import { Response, Request } from "express";
|
||||
import ChunkInterface from "./utils/ChunkInterface";
|
||||
import NotAuthorizedError from "../../utils/NotAuthorizedError";
|
||||
@@ -10,16 +9,14 @@ import NotFoundError from "../../utils/NotFoundError";
|
||||
import crypto from "crypto";
|
||||
import getBusboyData from "./utils/getBusboyData";
|
||||
import videoChecker from "../../utils/videoChecker";
|
||||
import fs from "fs";
|
||||
import uuid from "uuid";
|
||||
import awaitUploadStreamS3 from "./utils/awaitUploadStreamS3";
|
||||
import getFileSize from "./utils/getFileSize";
|
||||
import awaitStream from "./utils/awaitStream";
|
||||
import createThumbnailS3 from "../FileService/utils/createThumbnailS3";
|
||||
import createThumbnailS3 from "./utils/createThumbnailS3";
|
||||
import imageChecker from "../../utils/imageChecker";
|
||||
import Thumbnail, {ThumbnailInterface} from "../../models/thumbnail";
|
||||
import streamToBuffer from "../../utils/streamToBuffer";
|
||||
import removeChunksS3 from "../FileService/utils/removeChunksS3";
|
||||
import removeChunksS3 from "./utils/removeChunksS3";
|
||||
import fixStartChunkLength from "./utils/fixStartChunkLength";
|
||||
import fixEndChunkLength from "./utils/fixEndChunkLength";
|
||||
import getPrevIVS3 from "./utils/getPrevIVS3";
|
||||
@@ -29,7 +26,6 @@ import Folder from "../../models/folder";
|
||||
import DbUtilFile from "../../db/utils/fileUtils/index";
|
||||
const dbUtilsFile = new DbUtilFile();
|
||||
|
||||
|
||||
class S3Service implements ChunkInterface {
|
||||
|
||||
constructor() {
|
||||
@@ -80,7 +76,7 @@ class S3Service implements ChunkInterface {
|
||||
await awaitUploadStreamS3(params);
|
||||
|
||||
const date = new Date();
|
||||
const encryptedFileSize = size; //await getFileSize(metadata.filePath);
|
||||
const encryptedFileSize = size;
|
||||
|
||||
const currentFile = new File({
|
||||
filename,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import removeChunks from "../../FileService/utils/removeChunks";
|
||||
import removeChunks from "./removeChunks";
|
||||
import {Request} from "express"
|
||||
|
||||
const awaitUploadStream = <T>(inputSteam: any, outputStream: any, req: Request, allStreamsToErrorCatch: any[]) => {
|
||||
@@ -19,28 +19,6 @@ const awaitUploadStream = <T>(inputSteam: any, outputStream: any, req: Request,
|
||||
})
|
||||
})
|
||||
|
||||
// inputSteam.on("error", (e: Error) => {
|
||||
|
||||
// removeChunks(outputStream);
|
||||
|
||||
// reject({
|
||||
// message: "Await Stream Input Error",
|
||||
// code: 500,
|
||||
// error: e
|
||||
// })
|
||||
// })
|
||||
|
||||
// outputStream.on("error", (e: Error) => {
|
||||
|
||||
// removeChunks(outputStream);
|
||||
|
||||
// reject({
|
||||
// message: "Await Stream Output Error",
|
||||
// code: 500,
|
||||
// error: e
|
||||
// })
|
||||
// })
|
||||
|
||||
req.on("aborted", () => {
|
||||
|
||||
console.log("Upload Request Cancelling...");
|
||||
@@ -49,7 +27,6 @@ const awaitUploadStream = <T>(inputSteam: any, outputStream: any, req: Request,
|
||||
})
|
||||
|
||||
inputSteam.pipe(outputStream).on("finish", (data: T) => {
|
||||
console.log("await stream finished")
|
||||
resolve(data);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,22 +1,5 @@
|
||||
import {Request} from "express"
|
||||
import fs from "fs";
|
||||
|
||||
export const removeChunksFS = (path: string) => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
fs.unlink(path, (err) => {
|
||||
|
||||
if (err) {
|
||||
console.log("Could not remove fs file", err);
|
||||
resolve();
|
||||
}
|
||||
|
||||
console.log("File Removed");
|
||||
resolve();
|
||||
})
|
||||
})
|
||||
}
|
||||
import removeChunksFS from "./removeChunksFS";
|
||||
|
||||
const awaitUploadStream = <T>(inputSteam: any, outputStream: any, req: Request, path: string, allStreamsToCatchError: any[]) => {
|
||||
|
||||
@@ -36,28 +19,6 @@ const awaitUploadStream = <T>(inputSteam: any, outputStream: any, req: Request,
|
||||
})
|
||||
})
|
||||
|
||||
// inputSteam.on("error", (e: Error) => {
|
||||
|
||||
// removeChunksFS(path);
|
||||
|
||||
// reject({
|
||||
// message: "Await Stream Input Error",
|
||||
// code: 500,
|
||||
// error: e
|
||||
// })
|
||||
// })
|
||||
|
||||
// outputStream.on("error", (e: Error) => {
|
||||
|
||||
// removeChunksFS(path);
|
||||
|
||||
// reject({
|
||||
// message: "Await Stream Output Error",
|
||||
// code: 500,
|
||||
// error: e
|
||||
// })
|
||||
// })
|
||||
|
||||
req.on("aborted", () => {
|
||||
|
||||
console.log("Upload Request Cancelling...");
|
||||
@@ -66,7 +27,6 @@ const awaitUploadStream = <T>(inputSteam: any, outputStream: any, req: Request,
|
||||
})
|
||||
|
||||
inputSteam.pipe(outputStream).on("finish", (data: T) => {
|
||||
console.log("await stream finished")
|
||||
resolve(data);
|
||||
})
|
||||
})
|
||||
|
||||
@@ -7,11 +7,9 @@ const awaitUploadStreamS3 = (params: any) => {
|
||||
s3.upload(params, (err: any, data: any) => {
|
||||
|
||||
if (err) {
|
||||
console.log("Amazon Upload Error", err);
|
||||
reject("Amazon upload error");
|
||||
}
|
||||
|
||||
console.log("Amazon File Uploaded", data);
|
||||
resolve();
|
||||
})
|
||||
|
||||
|
||||
+10
-10
@@ -1,13 +1,13 @@
|
||||
import mongoose from "../../../db/mongoose";
|
||||
const conn = mongoose.connection;
|
||||
const crypto= require("crypto");
|
||||
import env from "../../../enviroment/env";
|
||||
import crypto from "crypto";
|
||||
import Thumbnail from "../../../models/thumbnail";
|
||||
const ObjectID = require('mongodb').ObjectID
|
||||
const sharp = require("sharp");
|
||||
const concat = require("concat-stream")
|
||||
import {FileInterface} from "../../../models/file";
|
||||
import {UserInterface} from "../../../models/user";
|
||||
import { ObjectID } from "mongodb";
|
||||
import sharp from "sharp";
|
||||
import concat from "concat-stream";
|
||||
import { FileInterface } from "../../../models/file";
|
||||
import { UserInterface } from "../../../models/user";
|
||||
|
||||
const conn = mongoose.connection;
|
||||
|
||||
const createThumbnail = (file: FileInterface, filename: string, user: UserInterface) => {
|
||||
|
||||
@@ -15,13 +15,13 @@ const createThumbnail = (file: FileInterface, filename: string, user: UserInterf
|
||||
|
||||
const password = user.getEncryptionKey();
|
||||
|
||||
let CIPHER_KEY = crypto.createHash('sha256').update(password).digest()
|
||||
let CIPHER_KEY = crypto.createHash('sha256').update(password!).digest()
|
||||
|
||||
let bucket = new mongoose.mongo.GridFSBucket(conn.db, {
|
||||
chunkSizeBytes: 1024 * 255
|
||||
})
|
||||
|
||||
const readStream = bucket.openDownloadStream(ObjectID(file._id))
|
||||
const readStream = bucket.openDownloadStream(new ObjectID(file._id))
|
||||
|
||||
readStream.on("error", (e: Error) => {
|
||||
console.log("File service upload thumbnail error", e);
|
||||
+5
-51
@@ -1,23 +1,22 @@
|
||||
import mongoose from "../../../db/mongoose";
|
||||
const conn = mongoose.connection;
|
||||
const crypto= require("crypto");
|
||||
import crypto from "crypto";
|
||||
import Thumbnail from "../../../models/thumbnail";
|
||||
const ObjectID = require('mongodb').ObjectID
|
||||
const sharp = require("sharp");
|
||||
const concat = require("concat-stream")
|
||||
import sharp from "sharp";
|
||||
import {FileInterface} from "../../../models/file";
|
||||
import {UserInterface} from "../../../models/user";
|
||||
import fs from "fs";
|
||||
import uuid from "uuid";
|
||||
import env from "../../../enviroment/env";
|
||||
|
||||
const conn = mongoose.connection;
|
||||
|
||||
const createThumbnailFS = (file: FileInterface, filename: string, user: UserInterface) => {
|
||||
|
||||
return new Promise<FileInterface>((resolve) => {
|
||||
|
||||
const password = user.getEncryptionKey();
|
||||
|
||||
let CIPHER_KEY = crypto.createHash('sha256').update(password).digest()
|
||||
let CIPHER_KEY = crypto.createHash('sha256').update(password!).digest()
|
||||
|
||||
const thumbnailFilename = uuid.v4();
|
||||
|
||||
@@ -56,7 +55,6 @@ const createThumbnailFS = (file: FileInterface, filename: string, user: UserInte
|
||||
readStream.pipe(decipher).pipe(imageResize).pipe(thumbnailCipher).pipe(writeStream);
|
||||
|
||||
writeStream.on("finish", async() => {
|
||||
console.log("Thumbnail written");
|
||||
|
||||
const thumbnailModel = new Thumbnail({name: filename, owner: user._id, IV: thumbnailIV, path: env.fsDirectory + thumbnailFilename});
|
||||
|
||||
@@ -77,50 +75,6 @@ const createThumbnailFS = (file: FileInterface, filename: string, user: UserInte
|
||||
console.log("Thumbnail error", e);
|
||||
resolve(file);
|
||||
}
|
||||
|
||||
// try {
|
||||
|
||||
// const concatStream = concat(async(bufferData: Buffer) => {
|
||||
|
||||
// const thumbnailIV = crypto.randomBytes(16);
|
||||
|
||||
// const thumbnailCipher = crypto.createCipheriv("aes256", CIPHER_KEY, thumbnailIV);
|
||||
|
||||
// bufferData = Buffer.concat([thumbnailIV, thumbnailCipher.update(bufferData), thumbnailCipher.final()]);
|
||||
|
||||
// const thumbnailModel = new Thumbnail({name: filename, owner: user._id, data: bufferData});
|
||||
|
||||
// await thumbnailModel.save();
|
||||
|
||||
// let updatedFile = await conn.db.collection("fs.files")
|
||||
// .findOneAndUpdate({"_id": file._id}, {"$set": {"metadata.hasThumbnail": true, "metadata.thumbnailID": thumbnailModel._id}})
|
||||
|
||||
// updatedFile = updatedFile.value;
|
||||
|
||||
// updatedFile = {...updatedFile, metadata: {...updatedFile.metadata, hasThumbnail: true, thumbnailID: thumbnailModel._id}}
|
||||
|
||||
// resolve(updatedFile);
|
||||
|
||||
// }).on("error", (e: Error) => {
|
||||
// console.log("File service upload concat stream error", e);
|
||||
// resolve(file);
|
||||
// })
|
||||
|
||||
// const imageResize = sharp().resize(300).on("error", (e: Error) => {
|
||||
|
||||
// console.log("resize error", e);
|
||||
// resolve(file);
|
||||
// })
|
||||
|
||||
// readStream.pipe(decipher).pipe(imageResize).pipe(concatStream);
|
||||
|
||||
|
||||
// } catch (e) {
|
||||
|
||||
// console.log(e);
|
||||
// resolve(file);
|
||||
// }
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import mongoose from "../../../db/mongoose";
|
||||
import crypto from "crypto";
|
||||
import Thumbnail from "../../../models/thumbnail";
|
||||
import sharp from "sharp";
|
||||
import { FileInterface } from "../../../models/file";
|
||||
import { UserInterface } from "../../../models/user";
|
||||
import uuid from "uuid";
|
||||
import env from "../../../enviroment/env";
|
||||
import s3 from "../../../db/s3";
|
||||
|
||||
const conn = mongoose.connection;
|
||||
|
||||
const createThumbnailS3 = (file: FileInterface, filename: string, user: UserInterface) => {
|
||||
|
||||
return new Promise<FileInterface>((resolve, reject) => {
|
||||
|
||||
const password = user.getEncryptionKey();
|
||||
|
||||
let CIPHER_KEY = crypto.createHash('sha256').update(password!).digest()
|
||||
|
||||
const thumbnailFilename = uuid.v4();
|
||||
|
||||
const params: any = {Bucket: env.s3Bucket, Key: file.metadata.s3ID!};
|
||||
|
||||
const readStream = s3.getObject(params).createReadStream();
|
||||
|
||||
const decipher = crypto.createDecipheriv('aes256', CIPHER_KEY, file.metadata.IV);
|
||||
|
||||
readStream.on("error", (e: Error) => {
|
||||
console.log("File service upload thumbnail error", e);
|
||||
resolve(file);
|
||||
})
|
||||
|
||||
decipher.on("error", (e: Error) => {
|
||||
console.log("File service upload thumbnail decipher error", e);
|
||||
resolve(file)
|
||||
})
|
||||
|
||||
try {
|
||||
|
||||
const thumbnailIV = crypto.randomBytes(16);
|
||||
const thumbnailCipher = crypto.createCipheriv("aes256", CIPHER_KEY, thumbnailIV);
|
||||
|
||||
const imageResize = sharp().resize(300).on("error", (e: Error) => {
|
||||
|
||||
console.log("resize error", e);
|
||||
resolve(file);
|
||||
})
|
||||
|
||||
const paramsWrite: any = {
|
||||
Bucket: env.s3Bucket,
|
||||
Body : readStream.pipe(decipher).pipe(imageResize).pipe(thumbnailCipher),
|
||||
Key : thumbnailFilename
|
||||
};
|
||||
|
||||
s3.upload(paramsWrite, async(err: any, data: any) => {
|
||||
|
||||
if (err) {
|
||||
console.log("Amazon Upload Error", err);
|
||||
reject("Amazon upload error");
|
||||
}
|
||||
|
||||
const thumbnailModel = new Thumbnail({name: thumbnailFilename, owner: user._id, IV: thumbnailIV, s3ID: thumbnailFilename});
|
||||
|
||||
await thumbnailModel.save();
|
||||
|
||||
const getUpdatedFile = await conn.db.collection("fs.files")
|
||||
.findOneAndUpdate({"_id": file._id}, {"$set": {"metadata.hasThumbnail": true, "metadata.thumbnailID": thumbnailModel._id}})
|
||||
|
||||
let updatedFile: FileInterface = getUpdatedFile.value;
|
||||
|
||||
updatedFile = {...updatedFile, metadata: {...updatedFile.metadata, hasThumbnail: true, thumbnailID: thumbnailModel._id}} as FileInterface;
|
||||
|
||||
resolve(updatedFile);
|
||||
})
|
||||
|
||||
} catch (e) {
|
||||
|
||||
console.log("Thumbnail error", e);
|
||||
resolve(file);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default createThumbnailS3;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
import { GridFSBucketWriteStream } from "mongodb";
|
||||
|
||||
import DbUtilsFile from "../../../db/utils/fileUtils";
|
||||
|
||||
const dbUtilsFile = new DbUtilsFile();
|
||||
|
||||
const removeChunks = async(bucketStream: GridFSBucketWriteStream) => {
|
||||
@@ -0,0 +1,20 @@
|
||||
import fs from "fs";
|
||||
|
||||
const removeChunksFS = (path: string) => {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
fs.unlink(path, (err) => {
|
||||
|
||||
if (err) {
|
||||
console.log("Could not remove fs file", err);
|
||||
resolve();
|
||||
}
|
||||
|
||||
console.log("File Removed");
|
||||
resolve();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default removeChunksFS;
|
||||
@@ -1,34 +1,17 @@
|
||||
const imageChecker = require("../../utils/imageChecker");
|
||||
import crypto from "crypto";
|
||||
const videoChecker = require("../../utils/videoChecker");
|
||||
import mongoose from "../../db/mongoose";
|
||||
const conn = mongoose.connection;
|
||||
const createThumbnail = require("../../services/FileService/utils/createThumbnail");
|
||||
import Thumbnail, {ThumbnailInterface} from "../../models/thumbnail";
|
||||
const ObjectID = require('mongodb').ObjectID
|
||||
import NotAuthorizedError from "../../utils/NotAuthorizedError";
|
||||
import NotFoundError from "../../utils/NotFoundError";
|
||||
import env from "../../enviroment/env";
|
||||
const jwt = require("jsonwebtoken");
|
||||
const removeChunks = require("./utils/removeChunks");
|
||||
const User = require("../../models/user");
|
||||
|
||||
|
||||
const Folder = require("../../models/folder");
|
||||
import jwt from "jsonwebtoken";
|
||||
import Folder from "../../models/folder";
|
||||
import sortBySwitch from "../../utils/sortBySwitch";
|
||||
import createQuery from "../../utils/createQuery";
|
||||
const ffmpeg = require("fluent-ffmpeg");
|
||||
const temp = require("temp").track();
|
||||
const progress = require("progress-stream");
|
||||
const fs = require("fs")
|
||||
import DbUtilFile from "../../db/utils/fileUtils/index";
|
||||
import DbUtilFolder from "../../db/utils/folderUtils";
|
||||
import { UserInterface } from "../../models/user";
|
||||
import { FileInterface } from "../../models/file";
|
||||
|
||||
const dbUtilsFile = new DbUtilFile();
|
||||
const dbUtilsFolder = new DbUtilFolder();
|
||||
|
||||
import { UserInterface } from "../../models/user";
|
||||
import { FileInterface } from "../../models/file";
|
||||
|
||||
class MongoFileService {
|
||||
|
||||
@@ -56,7 +39,7 @@ class MongoFileService {
|
||||
makePublic = async(user: UserInterface, fileID: string) => {
|
||||
|
||||
const userID = user._id;
|
||||
const token = await jwt.sign({_id: userID.toString()}, env.password);
|
||||
const token = await jwt.sign({_id: userID.toString()}, env.password!);
|
||||
|
||||
const file = await dbUtilsFile.makePublic(fileID, userID, token);
|
||||
|
||||
@@ -81,7 +64,7 @@ class MongoFileService {
|
||||
|
||||
makeOneTimePublic = async(userID: string, fileID: string) => {
|
||||
|
||||
const token = await jwt.sign({_id: userID.toString()}, env.password);
|
||||
const token = await jwt.sign({_id: userID.toString()}, env.password!);
|
||||
|
||||
const file = await dbUtilsFile.makeOneTimePublic(fileID, userID, token);
|
||||
|
||||
@@ -176,7 +159,7 @@ class MongoFileService {
|
||||
|
||||
const key = user.getEncryptionKey();
|
||||
|
||||
const decoded = await jwt.verify(tempToken, env.password);
|
||||
const decoded = await jwt.verify(tempToken, env.password!) as any;
|
||||
|
||||
const publicKey = decoded.iv;
|
||||
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
import mongoose from "../../../db/mongoose";
|
||||
const conn = mongoose.connection;
|
||||
const crypto= require("crypto");
|
||||
import Thumbnail from "../../../models/thumbnail";
|
||||
const ObjectID = require('mongodb').ObjectID
|
||||
const sharp = require("sharp");
|
||||
const concat = require("concat-stream")
|
||||
import {FileInterface} from "../../../models/file";
|
||||
import {UserInterface} from "../../../models/user";
|
||||
import fs from "fs";
|
||||
import uuid from "uuid";
|
||||
import env from "../../../enviroment/env";
|
||||
import s3 from "../../../db/s3";
|
||||
import awaitUploadStreamS3 from "../../ChunkService/utils/awaitUploadStreamS3";
|
||||
|
||||
const createThumbnailS3 = (file: FileInterface, filename: string, user: UserInterface) => {
|
||||
|
||||
return new Promise<FileInterface>((resolve, reject) => {
|
||||
|
||||
const password = user.getEncryptionKey();
|
||||
|
||||
let CIPHER_KEY = crypto.createHash('sha256').update(password).digest()
|
||||
|
||||
const thumbnailFilename = uuid.v4();
|
||||
|
||||
const params: any = {Bucket: env.s3Bucket, Key: file.metadata.s3ID!};
|
||||
|
||||
const readStream = s3.getObject(params).createReadStream();
|
||||
|
||||
//const readStream = fs.createReadStream(file.metadata.filePath!);
|
||||
//const writeStream = fs.createWriteStream(env.fsDirectory + thumbnailFilename);
|
||||
|
||||
|
||||
const decipher = crypto.createDecipheriv('aes256', CIPHER_KEY, file.metadata.IV);
|
||||
|
||||
|
||||
|
||||
|
||||
readStream.on("error", (e: Error) => {
|
||||
console.log("File service upload thumbnail error", e);
|
||||
resolve(file);
|
||||
})
|
||||
|
||||
// writeStream.on("error", (e: Error) => {
|
||||
// console.log("File service upload write thumbnail error", e);
|
||||
// resolve(file);
|
||||
// })
|
||||
|
||||
decipher.on("error", (e: Error) => {
|
||||
console.log("File service upload thumbnail decipher error", e);
|
||||
resolve(file)
|
||||
})
|
||||
|
||||
try {
|
||||
|
||||
|
||||
const thumbnailIV = crypto.randomBytes(16);
|
||||
|
||||
const thumbnailCipher = crypto.createCipheriv("aes256", CIPHER_KEY, thumbnailIV);
|
||||
|
||||
const imageResize = sharp().resize(300).on("error", (e: Error) => {
|
||||
|
||||
console.log("resize error", e);
|
||||
resolve(file);
|
||||
|
||||
})
|
||||
|
||||
const paramsWrite: any = {
|
||||
Bucket: env.s3Bucket,
|
||||
Body : readStream.pipe(decipher).pipe(imageResize).pipe(thumbnailCipher),
|
||||
Key : thumbnailFilename
|
||||
};
|
||||
|
||||
//readStream.pipe(decipher).pipe(imageResize).pipe(thumbnailCipher).pipe(writeStream);
|
||||
//await awaitUploadStreamS3(paramsWrite);
|
||||
|
||||
s3.upload(paramsWrite, async(err: any, data: any) => {
|
||||
|
||||
if (err) {
|
||||
console.log("Amazon Upload Error", err);
|
||||
reject("Amazon upload error");
|
||||
}
|
||||
|
||||
console.log("Thumbnail written");
|
||||
|
||||
const thumbnailModel = new Thumbnail({name: thumbnailFilename, owner: user._id, IV: thumbnailIV, s3ID: thumbnailFilename});
|
||||
|
||||
await thumbnailModel.save();
|
||||
|
||||
const getUpdatedFile = await conn.db.collection("fs.files")
|
||||
.findOneAndUpdate({"_id": file._id}, {"$set": {"metadata.hasThumbnail": true, "metadata.thumbnailID": thumbnailModel._id}})
|
||||
|
||||
let updatedFile: FileInterface = getUpdatedFile.value;
|
||||
|
||||
updatedFile = {...updatedFile, metadata: {...updatedFile.metadata, hasThumbnail: true, thumbnailID: thumbnailModel._id}} as FileInterface;
|
||||
|
||||
resolve(updatedFile);
|
||||
})
|
||||
|
||||
// writeStream.on("finish", async() => {
|
||||
// console.log("Thumbnail written");
|
||||
|
||||
// const thumbnailModel = new Thumbnail({name: filename, owner: user._id, IV: thumbnailIV, path: env.fsDirectory + thumbnailFilename});
|
||||
|
||||
// await thumbnailModel.save();
|
||||
|
||||
// let updatedFile = await conn.db.collection("fs.files")
|
||||
// .findOneAndUpdate({"_id": file._id}, {"$set": {"metadata.hasThumbnail": true, "metadata.thumbnailID": thumbnailModel._id}})
|
||||
|
||||
// updatedFile = updatedFile.value;
|
||||
|
||||
// updatedFile = {...updatedFile, metadata: {...updatedFile.metadata, hasThumbnail: true, thumbnailID: thumbnailModel._id}}
|
||||
|
||||
// resolve(updatedFile);
|
||||
// })
|
||||
|
||||
} catch (e) {
|
||||
|
||||
console.log("Thumbnail error", e);
|
||||
resolve(file);
|
||||
}
|
||||
|
||||
// try {
|
||||
|
||||
// const concatStream = concat(async(bufferData: Buffer) => {
|
||||
|
||||
// const thumbnailIV = crypto.randomBytes(16);
|
||||
|
||||
// const thumbnailCipher = crypto.createCipheriv("aes256", CIPHER_KEY, thumbnailIV);
|
||||
|
||||
// bufferData = Buffer.concat([thumbnailIV, thumbnailCipher.update(bufferData), thumbnailCipher.final()]);
|
||||
|
||||
// const thumbnailModel = new Thumbnail({name: filename, owner: user._id, data: bufferData});
|
||||
|
||||
// await thumbnailModel.save();
|
||||
|
||||
// let updatedFile = await conn.db.collection("fs.files")
|
||||
// .findOneAndUpdate({"_id": file._id}, {"$set": {"metadata.hasThumbnail": true, "metadata.thumbnailID": thumbnailModel._id}})
|
||||
|
||||
// updatedFile = updatedFile.value;
|
||||
|
||||
// updatedFile = {...updatedFile, metadata: {...updatedFile.metadata, hasThumbnail: true, thumbnailID: thumbnailModel._id}}
|
||||
|
||||
// resolve(updatedFile);
|
||||
|
||||
// }).on("error", (e: Error) => {
|
||||
// console.log("File service upload concat stream error", e);
|
||||
// resolve(file);
|
||||
// })
|
||||
|
||||
// const imageResize = sharp().resize(300).on("error", (e: Error) => {
|
||||
|
||||
// console.log("resize error", e);
|
||||
// resolve(file);
|
||||
// })
|
||||
|
||||
// readStream.pipe(decipher).pipe(imageResize).pipe(concatStream);
|
||||
|
||||
|
||||
// } catch (e) {
|
||||
|
||||
// console.log(e);
|
||||
// resolve(file);
|
||||
// }
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
export default createThumbnailS3;
|
||||
@@ -2,11 +2,7 @@ import Folder from "../../models/folder";
|
||||
import InternalServerError from "../../utils/InternalServerError";
|
||||
import NotFoundError from "../../utils/NotFoundError";
|
||||
import UtilsFile from "../../db/utils/fileUtils";
|
||||
import {ObjectID} from "mongodb";
|
||||
import UtilsFolder from "../../db/utils/folderUtils";
|
||||
import mongoose from "../../db/mongoose";
|
||||
const conn = mongoose.connection;
|
||||
import Thumbnail from "../../models/thumbnail";
|
||||
import sortBySwitch from "../../utils/sortBySwitchFolder";
|
||||
|
||||
const utilsFile = new UtilsFile();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import User, {UserInterface} from "../../models/user";
|
||||
const bcrypt = require("bcrypt");
|
||||
const crypto = require("crypto");
|
||||
const NotFoundError = require("../../utils/NotFoundError");
|
||||
import bcrypt from "bcrypt";
|
||||
import NotFoundError from "../../utils/NotFoundError";
|
||||
import InternalServerError from "../../utils/InternalServerError";
|
||||
import NotAuthorizedError from "../../utils/NotAuthorizedError";
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
"@babel/types": "^7.9.5",
|
||||
"@types/bcrypt": "^3.0.0",
|
||||
"@types/compression": "^1.7.0",
|
||||
"@types/concat-stream": "^1.6.0",
|
||||
"@types/connect-busboy": "0.0.2",
|
||||
"@types/express": "^4.17.6",
|
||||
"@types/helmet": "0.0.45",
|
||||
@@ -47,6 +48,7 @@
|
||||
"@types/mongodb": "^3.5.5",
|
||||
"@types/mongoose": "^5.7.10",
|
||||
"@types/node": "^13.11.1",
|
||||
"@types/sharp": "^0.25.0",
|
||||
"@types/uuid": "^7.0.2",
|
||||
"@types/validator": "^13.0.0",
|
||||
"aws-sdk": "^2.657.0",
|
||||
|
||||
Reference in New Issue
Block a user