aboutsummaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/database/migrations/20201227023216_addUniques.js2
-rw-r--r--src/api/routes/uploads/uploadPOST.js36
-rw-r--r--src/api/utils/Util.js5
-rw-r--r--src/api/utils/multerStorage.js6
4 files changed, 27 insertions, 22 deletions
diff --git a/src/api/database/migrations/20201227023216_addUniques.js b/src/api/database/migrations/20201227023216_addUniques.js
index 14f9e7f..d031991 100644
--- a/src/api/database/migrations/20201227023216_addUniques.js
+++ b/src/api/database/migrations/20201227023216_addUniques.js
@@ -28,6 +28,6 @@ exports.up = async knex => {
table.unique(['fileId', 'tagId']);
});
};
-exports.down = async knex => {
+exports.down = async () => {
// Nothing
};
diff --git a/src/api/routes/uploads/uploadPOST.js b/src/api/routes/uploads/uploadPOST.js
index bba7989..8e26079 100644
--- a/src/api/routes/uploads/uploadPOST.js
+++ b/src/api/routes/uploads/uploadPOST.js
@@ -1,6 +1,7 @@
const path = require('path');
const jetpack = require('fs-jetpack');
const multer = require('multer');
+
const Util = require('../../utils/Util');
const Route = require('../../structures/Route');
const multerStorage = require('../../utils/multerStorage');
@@ -10,6 +11,22 @@ const chunkedUploadsTimeout = 1800000;
const chunksDir = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER, 'chunks');
const uploadDir = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER);
+
+const cleanUpChunks = async (uuid, onTimeout) => {
+ // Remove tmp file
+ await jetpack.removeAsync(path.join(chunksData[uuid].root, chunksData[uuid].filename))
+ .catch(error => {
+ if (error.code !== 'ENOENT') console.error(error);
+ });
+
+ // Remove UUID dir
+ await jetpack.removeAsync(chunksData[uuid].root);
+
+ // Delete cached chunks data
+ if (!onTimeout) chunksData[uuid].clearTimeout();
+ delete chunksData[uuid];
+};
+
class ChunksData {
constructor(uuid, root) {
this.uuid = uuid;
@@ -134,7 +151,7 @@ const uploadFile = async (req, res) => {
// If the uploaded file is a chunk then just say that it was a success
const uuid = req.body.uuid;
if (chunksData[uuid] !== undefined) {
- req.files.forEach(file => {
+ req.files.forEach(() => {
chunksData[uuid].chunks++;
});
res.json({ success: true });
@@ -149,7 +166,7 @@ const uploadFile = async (req, res) => {
return infoMap[0];
};
-const finishChunks = async (req, res) => {
+const finishChunks = async req => {
const check = file => typeof file.uuid !== 'string' ||
!chunksData[file.uuid] ||
chunksData[file.uuid].chunks < 2;
@@ -228,21 +245,6 @@ const finishChunks = async (req, res) => {
}
};
-const cleanUpChunks = async (uuid, onTimeout) => {
- // Remove tmp file
- await jetpack.removeAsync(path.join(chunksData[uuid].root, chunksData[uuid].filename))
- .catch(error => {
- if (error.code !== 'ENOENT') console.error(error);
- });
-
- // Remove UUID dir
- await jetpack.removeAsync(chunksData[uuid].root);
-
- // Delete cached chunks data
- if (!onTimeout) chunksData[uuid].clearTimeout();
- delete chunksData[uuid];
-};
-
class uploadPOST extends Route {
constructor() {
super('/upload', 'post', {
diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js
index ae13eb5..9d5021d 100644
--- a/src/api/utils/Util.js
+++ b/src/api/utils/Util.js
@@ -35,6 +35,10 @@ class Util {
return blockedExtensions.includes(extension);
}
+ static getMimeFromType(fileTypeMimeObj) {
+ return fileTypeMimeObj ? fileTypeMimeObj.mime : undefined;
+ }
+
static constructFilePublicLink(file) {
/*
TODO: This wont work without a reverse proxy serving both
@@ -225,6 +229,7 @@ class Util {
static async storeFileToDb(req, res, user, file, db) {
const dbFile = await db.table('files')
+ // eslint-disable-next-line func-names
.where(function() {
if (user === undefined) {
this.whereNull('userId');
diff --git a/src/api/utils/multerStorage.js b/src/api/utils/multerStorage.js
index a2d01a4..9c4d94f 100644
--- a/src/api/utils/multerStorage.js
+++ b/src/api/utils/multerStorage.js
@@ -8,7 +8,7 @@ function DiskStorage(opts) {
if (typeof opts.destination === 'string') {
jetpack.dir(opts.destination);
- this.getDestination = function($0, $1, cb) { cb(null, opts.destination); };
+ this.getDestination = ($0, $1, cb) => { cb(null, opts.destination); };
} else {
this.getDestination = opts.destination;
}
@@ -86,6 +86,4 @@ DiskStorage.prototype._removeFile = function _removeFile(req, file, cb) {
fs.unlink(path, cb);
};
-module.exports = function(opts) {
- return new DiskStorage(opts);
-};
+module.exports = opts => new DiskStorage(opts);