From e97fee48441717f3b508ac855339d0fb8210be53 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 27 Dec 2020 04:27:56 +0900 Subject: Fixes chunked uploads not being saved to albums or thumbnails --- src/api/routes/uploads/chunksPOST.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/api/routes/uploads/chunksPOST.js') diff --git a/src/api/routes/uploads/chunksPOST.js b/src/api/routes/uploads/chunksPOST.js index 061cfb0..ee95227 100644 --- a/src/api/routes/uploads/chunksPOST.js +++ b/src/api/routes/uploads/chunksPOST.js @@ -12,7 +12,10 @@ class uploadPOST extends Route { }); } - async run(req, res) { + async run(req, res, db) { + const user = await Util.isAuthorized(req); + if (!user && process.env.PUBLIC_MODE === 'false') return res.status(401).json({ message: 'Not authorized to use this resource' }); + const filename = Util.getUniqueFilename(randomstring.generate(32)); // console.log('Files', req.body.files); const info = { @@ -40,6 +43,7 @@ class uploadPOST extends Route { // Save some data info.name = `${filename}${ext || ''}`; info.url += `${filename}${ext || ''}`; + info.data = chunk; for (let i = 0; i < chunkDir.length; i++) { const dir = path.join(__dirname, @@ -54,15 +58,18 @@ class uploadPOST extends Route { await jetpack.removeAsync(chunkOutput); } + Util.generateThumbnails(info.name); + const insertedId = await Util.saveFileToDatabase(req, res, user, db, info, { + originalname: info.data.original, mimetype: info.data.type + }); + if (!insertedId) return res.status(500).json({ message: 'There was an error saving the file.' }); + info.deleteUrl = `${process.env.DOMAIN}/api/file/${insertedId[0]}`; + Util.saveFileToAlbum(db, req.headers.albumid, insertedId); + delete info.chunk; + return res.status(201).send({ message: 'Sucessfully merged the chunk(s).', ...info - /* - name: `${filename}${ext || ''}`, - size: exists.size, - url: `${process.env.DOMAIN}/${exists.name}`, - deleteUrl: `${process.env.DOMAIN}/api/file/${exists.id}` - */ }); } } -- cgit v1.2.3 From aa7d2453171b3a596a1be6676eaf39cc93fe178f Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 27 Dec 2020 04:48:03 +0900 Subject: feat: Add hash checking for chunked uploads --- src/api/routes/uploads/chunksPOST.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/api/routes/uploads/chunksPOST.js') diff --git a/src/api/routes/uploads/chunksPOST.js b/src/api/routes/uploads/chunksPOST.js index ee95227..9cf7338 100644 --- a/src/api/routes/uploads/chunksPOST.js +++ b/src/api/routes/uploads/chunksPOST.js @@ -58,6 +58,28 @@ class uploadPOST extends Route { await jetpack.removeAsync(chunkOutput); } + /* + If a file with the same hash and user is found, delete this + uploaded copy and return a link to the original + */ + info.hash = await Util.getFileHash(info.name); + let existingFile = await Util.checkIfFileExists(db, user, info.hash); + if (existingFile) { + existingFile = Util.constructFilePublicLink(existingFile); + res.json({ + message: 'Successfully uploaded the file.', + name: existingFile.name, + hash: existingFile.hash, + size: existingFile.size, + url: `${process.env.DOMAIN}/${existingFile.name}`, + deleteUrl: `${process.env.DOMAIN}/api/file/${existingFile.id}`, + repeated: true + }); + + return Util.deleteFile(info.name); + } + + // Otherwise generate thumbs and do the rest Util.generateThumbnails(info.name); const insertedId = await Util.saveFileToDatabase(req, res, user, db, info, { originalname: info.data.original, mimetype: info.data.type -- cgit v1.2.3