diff options
| author | Pitu <[email protected]> | 2020-12-27 04:48:03 +0900 |
|---|---|---|
| committer | Pitu <[email protected]> | 2020-12-27 04:48:03 +0900 |
| commit | aa7d2453171b3a596a1be6676eaf39cc93fe178f (patch) | |
| tree | 8896bc57bd01ef1ed16e5d066f57be2bcd8d856f /src/api | |
| parent | Fixes chunked uploads not being saved to albums or thumbnails (diff) | |
| download | host.fuwn.me-aa7d2453171b3a596a1be6676eaf39cc93fe178f.tar.xz host.fuwn.me-aa7d2453171b3a596a1be6676eaf39cc93fe178f.zip | |
feat: Add hash checking for chunked uploads
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/routes/uploads/chunksPOST.js | 22 | ||||
| -rw-r--r-- | src/api/routes/uploads/uploadPOST.js | 13 | ||||
| -rw-r--r-- | src/api/utils/Util.js | 11 |
3 files changed, 34 insertions, 12 deletions
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 diff --git a/src/api/routes/uploads/uploadPOST.js b/src/api/routes/uploads/uploadPOST.js index 5d04da1..449999e 100644 --- a/src/api/routes/uploads/uploadPOST.js +++ b/src/api/routes/uploads/uploadPOST.js @@ -79,7 +79,7 @@ class uploadPOST extends Route { For this we need to wait until we have a filename so that we can delete the uploaded file. */ - const exists = await this.checkIfFileExists(db, user, hash); + const exists = await Util.checkIfFileExists(db, user, hash); if (exists) return this.fileExists(res, exists, filename); if (remappedKeys && remappedKeys.uuid) { @@ -140,17 +140,6 @@ class uploadPOST extends Route { return Util.deleteFile(filename); } - async checkIfFileExists(db, user, hash) { - const exists = await db.table('files') - .where(function() { // eslint-disable-line func-names - if (user) this.where('userId', user.id); - else this.whereNull('userId'); - }) - .where({ hash }) - .first(); - return exists; - } - _remapKeys(body) { const keys = Object.keys(body); if (keys.length) { diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 905f217..e52fac2 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -108,6 +108,17 @@ class Util { return hash; } + static async checkIfFileExists(db, user, hash) { + const exists = await db.table('files') + .where(function() { // eslint-disable-line func-names + if (user) this.where('userId', user.id); + else this.whereNull('userId'); + }) + .where({ hash }) + .first(); + return exists; + } + static getFilenameFromPath(fullPath) { return fullPath.replace(/^.*[\\\/]/, ''); // eslint-disable-line no-useless-escape } |