diff options
| author | Zephyrrus <[email protected]> | 2020-12-24 10:40:50 +0200 |
|---|---|---|
| committer | Zephyrrus <[email protected]> | 2020-12-24 10:40:50 +0200 |
| commit | 90001c2df56d58e69fd199a518ae7f3e4ed327fc (patch) | |
| tree | ac601537b5f464a1b03b084e5139e460f42e3473 /src/api/databaseMigration.js | |
| parent | chore: update lock files (diff) | |
| download | host.fuwn.me-90001c2df56d58e69fd199a518ae7f3e4ed327fc.tar.xz host.fuwn.me-90001c2df56d58e69fd199a518ae7f3e4ed327fc.zip | |
chore: remove trailing commas
Diffstat (limited to 'src/api/databaseMigration.js')
| -rw-r--r-- | src/api/databaseMigration.js | 111 |
1 files changed, 55 insertions, 56 deletions
diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 4bd45b7..7e919f3 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -4,31 +4,71 @@ const nodePath = require('path'); const moment = require('moment'); const jetpack = require('fs-jetpack'); -const { path } = require('fs-jetpack'); const sharp = require('sharp'); const ffmpeg = require('fluent-ffmpeg'); const imageExtensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webp']; const videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov']; +const generateThumbnailForImage = async (filename, output) => { + try { + const file = await jetpack.readAsync(nodePath.join(__dirname, '../../uploads', filename), 'buffer'); + await sharp(file) + .resize(64, 64) + .toFormat('webp') + .toFile(nodePath.join(__dirname, '../../uploads/thumbs/square', output)); + await sharp(file) + .resize(225, null) + .toFormat('webp') + .toFile(nodePath.join(__dirname, '../../uploads/thumbs', output)); + console.log('finished', filename); + } catch (error) { + console.log('error', filename); + } +}; + +const generateThumbnailForVideo = (filename) => { + try { + ffmpeg(nodePath.join(__dirname, '../../uploads', filename)) + .thumbnail({ + timestamps: [0], + filename: '%b.png', + folder: nodePath.join(__dirname, '../../uploads/thumbs/square'), + size: '64x64' + }) + .on('error', (error) => console.error(error.message)); + ffmpeg(nodePath.join(__dirname, '../../uploads', filename)) + .thumbnail({ + timestamps: [0], + filename: '%b.png', + folder: nodePath.join(__dirname, '../../uploads/thumbs'), + size: '150x?' + }) + .on('error', (error) => console.error(error.message)); + console.log('finished', filename); + } catch (error) { + console.log('error', filename); + } +}; + const oldDb = require('knex')({ client: 'sqlite3', connection: { - filename: nodePath.join(__dirname, '..', '..', 'db'), + filename: nodePath.join(__dirname, '../../', 'db') }, - useNullAsDefault: true, + useNullAsDefault: true }); const newDb = require('knex')({ client: 'sqlite3', connection: { - filename: nodePath.join(__dirname, '..', '..', 'database.sqlite'), + filename: nodePath.join(__dirname, '../../', 'database.sqlite') }, postProcessResponse: (result) => { const booleanFields = [ 'enabled', 'enableDownload', - 'isAdmin', + 'isAdmin' ]; const processResponse = (row) => { @@ -45,15 +85,15 @@ const newDb = require('knex')({ if (typeof result === 'object') return processResponse(result); return result; }, - useNullAsDefault: true, + useNullAsDefault: true }); const start = async () => { console.log('Starting migration, this may take a few minutes...'); // Because I half assed it console.log('Please do NOT kill the process. Wait for it to finish.'); - await jetpack.removeAsync(nodePath.join(__dirname, '..', '..', 'uploads', 'thumbs')); - await jetpack.dirAsync(nodePath.join(__dirname, '..', '..', 'uploads', 'thumbs', 'square')); + await jetpack.removeAsync(nodePath.join(__dirname, '../../uploads/thumbs')); + await jetpack.dirAsync(nodePath.join(__dirname, '../../uploads/thumbs/square')); console.log('Finished deleting old thumbnails to create new ones'); const users = await oldDb.table('users').where('username', '<>', 'root'); @@ -69,7 +109,7 @@ const start = async () => { passwordEditedAt: now, apiKeyEditedAt: now, createdAt: now, - editedAt: now, + editedAt: now }; await newDb.table('users').insert(userToInsert); } @@ -85,7 +125,7 @@ const start = async () => { name: album.name, zippedAt: album.zipGeneratedAt ? moment.unix(album.zipGeneratedAt).toDate() : null, createdAt: moment.unix(album.timestamp).toDate(), - editedAt: moment.unix(album.editedAt).toDate(), + editedAt: moment.unix(album.editedAt).toDate() }; const linkToInsert = { userId: album.userid, @@ -95,13 +135,13 @@ const start = async () => { enabled: true, enableDownload: true, createdAt: now, - editedAt: now, + editedAt: now }; await newDb.table('albums').insert(albumToInsert); const insertedId = await newDb.table('links').insert(linkToInsert); await newDb.table('albumsLinks').insert({ albumId: album.id, - linkId: insertedId[0], + linkId: insertedId[0] }); } console.log('Finished migrating albums...'); @@ -120,16 +160,16 @@ const start = async () => { hash: file.hash, ip: file.ip, createdAt: moment.unix(file.timestamp).toDate(), - editedAt: moment.unix(file.timestamp).toDate(), + editedAt: moment.unix(file.timestamp).toDate() }; filesToInsert.push(fileToInsert); albumsFilesToInsert.push({ albumId: file.albumid, - fileId: file.id, + fileId: file.id }); const filename = file.name; - if (!jetpack.exists(nodePath.join(__dirname, '..', '..', 'uploads', filename))) continue; + if (!jetpack.exists(nodePath.join(__dirname, '../../uploads', filename))) continue; const ext = nodePath.extname(filename).toLowerCase(); const output = `${filename.slice(0, -ext.length)}.webp`; if (imageExtensions.includes(ext)) await generateThumbnailForImage(filename, output); @@ -143,45 +183,4 @@ const start = async () => { process.exit(0); }; -const generateThumbnailForImage = async (filename, output) => { - try { - const file = await jetpack.readAsync(nodePath.join(__dirname, '..', '..', 'uploads', filename), 'buffer'); - await sharp(file) - .resize(64, 64) - .toFormat('webp') - .toFile(nodePath.join(__dirname, '..', '..', 'uploads', 'thumbs', 'square', output)); - await sharp(file) - .resize(225, null) - .toFormat('webp') - .toFile(nodePath.join(__dirname, '..', '..', 'uploads', 'thumbs', output)); - console.log('finished', filename); - } catch (error) { - console.log('error', filename); - } -}; - -const generateThumbnailForVideo = filename => { - try { - ffmpeg(nodePath.join(__dirname, '..', '..', 'uploads', filename)) - .thumbnail({ - timestamps: [0], - filename: '%b.png', - folder: nodePath.join(__dirname, '..', '..', 'uploads', 'thumbs', 'square'), - size: '64x64' - }) - .on('error', error => console.error(error.message)); - ffmpeg(nodePath.join(__dirname, '..', '..', 'uploads', filename)) - .thumbnail({ - timestamps: [0], - filename: '%b.png', - folder: nodePath.join(__dirname, '..', '..', 'uploads', 'thumbs'), - size: '150x?' - }) - .on('error', error => console.error(error.message)); - console.log('finished', filename); - } catch (error) { - console.log('error', filename); - } -}; - start(); |