aboutsummaryrefslogtreecommitdiff
path: root/src/api/utils/Util.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2020-12-27 04:27:56 +0900
committerPitu <[email protected]>2020-12-27 04:27:56 +0900
commite97fee48441717f3b508ac855339d0fb8210be53 (patch)
treee7a2b9617736dc239d43e22e0b6dfc3c31e83d78 /src/api/utils/Util.js
parentSquashed commit of the following: (diff)
downloadhost.fuwn.me-e97fee48441717f3b508ac855339d0fb8210be53.tar.xz
host.fuwn.me-e97fee48441717f3b508ac855339d0fb8210be53.zip
Fixes chunked uploads not being saved to albums or thumbnails
Diffstat (limited to 'src/api/utils/Util.js')
-rw-r--r--src/api/utils/Util.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js
index 35d726e..905f217 100644
--- a/src/api/utils/Util.js
+++ b/src/api/utils/Util.js
@@ -226,6 +226,60 @@ class Util {
}
static generateThumbnails = ThumbUtil.generateThumbnails;
+ static async saveFileToDatabase(req, res, user, db, file, originalFile) {
+ /*
+ Save the upload information to the database
+ */
+ const now = moment.utc().toDate();
+ let insertedId = null;
+ try {
+ /*
+ This is so fucking dumb
+ */
+ if (process.env.DB_CLIENT === 'sqlite3') {
+ insertedId = await db.table('files').insert({
+ userId: user ? user.id : null,
+ name: file.name,
+ original: originalFile.originalname,
+ type: originalFile.mimetype || '',
+ size: file.size,
+ hash: file.hash,
+ ip: req.ip,
+ createdAt: now,
+ editedAt: now
+ });
+ } else {
+ insertedId = await db.table('files').insert({
+ userId: user ? user.id : null,
+ name: file.name,
+ original: originalFile.originalname,
+ type: originalFile.mimetype || '',
+ size: file.size,
+ hash: file.hash,
+ ip: req.ip,
+ createdAt: now,
+ editedAt: now
+ }, 'id');
+ }
+ return insertedId;
+ } catch (error) {
+ console.error('There was an error saving the file to the database');
+ console.error(error);
+ return null;
+ }
+ }
+
+ static async saveFileToAlbum(db, albumId, insertedId) {
+ if (!albumId) return;
+
+ const now = moment.utc().toDate();
+ try {
+ await db.table('albumsFiles').insert({ albumId, fileId: insertedId[0] });
+ await db.table('albums').where('id', albumId).update('editedAt', now);
+ } catch (error) {
+ console.error(error);
+ }
+ }
}
module.exports = Util;