From d4ac722f58738e9367e6976342f379fe3d9b5cc5 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 10 May 2020 20:01:37 +0900 Subject: Feature: Migration script from v3 to v4 --- src/api/databaseMigration.js | 124 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/api/databaseMigration.js (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js new file mode 100644 index 0000000..253ccbd --- /dev/null +++ b/src/api/databaseMigration.js @@ -0,0 +1,124 @@ +const nodePath = require('path'); +const moment = require('moment'); + +const oldDb = require('knex')({ + client: 'sqlite3', + connection: { + filename: nodePath.join(__dirname, '..', '..', 'db') + }, + useNullAsDefault: true +}); + +const newDb = require('knex')({ + client: 'sqlite3', + connection: { + filename: nodePath.join(__dirname, '..', '..', 'database.sqlite') + }, + postProcessResponse: result => { + const booleanFields = [ + 'enabled', + 'enableDownload', + 'isAdmin' + ]; + + const processResponse = row => { + Object.keys(row).forEach(key => { + if (booleanFields.includes(key)) { + if (row[key] === 0) row[key] = false; + else if (row[key] === 1) row[key] = true; + } + }); + return row; + }; + + if (Array.isArray(result)) return result.map(row => processResponse(row)); + if (typeof result === 'object') return processResponse(result); + return result; + }, + 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.'); + + const users = await oldDb.table('users').where('username', '<>', 'root'); + for (const user of users) { + const now = moment.utc().toDate(); + const userToInsert = { + id: user.id, + username: user.username, + password: user.password, + enabled: user.enabled == 1 ? true : false, + isAdmin: false, + apiKey: user.token, // Is this the best way to do it? + passwordEditedAt: now, + apiKeyEditedAt: now, + createdAt: now, + editedAt: now + }; + await newDb.table('users').insert(userToInsert); + } + console.log('Finished migrating users...'); + + const albums = await oldDb.table('albums'); + for (const album of albums) { + const now = moment.utc().toDate(); + const albumToInsert = { + id: album.id, + userId: album.userid, + name: album.name, + zippedAt: album.zipGeneratedAt ? moment.unix(album.zipGeneratedAt).toDate() : null, + createdAt: moment.unix(album.timestamp).toDate(), + editedAt: moment.unix(album.editedAt).toDate() + }; + const linkToInsert = { + userId: album.userid, + albumId: album.id, + identifier: album.identifier, + views: 0, + enabled: album.enabled == 1 ? true : false, + enableDownload: true, + createdAt: 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] + }); + } + console.log('Finished migrating albums...'); + + const files = await oldDb.table('files'); + const filesToInsert = []; + const albumsFilesToInsert = []; + for (const file of files) { + const fileToInsert = { + id: file.id, + userId: file.userid, + name: file.name, + original: file.original, + type: file.type, + size: file.size, + hash: file.hash, + ip: file.ip, + createdAt: moment.unix(file.timestamp).toDate(), + editedAt: moment.unix(file.timestamp).toDate() + }; + filesToInsert.push(fileToInsert); + albumsFilesToInsert.push({ + albumId: file.albumid, + fileId: file.id + }); + } + await newDb.batchInsert('files', filesToInsert, 20); + await newDb.batchInsert('albumsFiles', albumsFilesToInsert, 20); + console.log('Finished migrating files...'); + + console.log('Finished migrating everything. '); + process.exit(0); +}; + +start(); -- cgit v1.2.3 From 1836c8c93a60c9dc0f86859bcfd6ef497c6f7dcd Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 10 May 2020 21:12:30 +0900 Subject: Small fix for migration script, dont import deleted albums --- src/api/databaseMigration.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 253ccbd..75611f3 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -63,6 +63,7 @@ const start = async () => { const albums = await oldDb.table('albums'); for (const album of albums) { + if (!album.enabled || album.enabled == 0) continue; const now = moment.utc().toDate(); const albumToInsert = { id: album.id, @@ -77,7 +78,7 @@ const start = async () => { albumId: album.id, identifier: album.identifier, views: 0, - enabled: album.enabled == 1 ? true : false, + enabled: true, enableDownload: true, createdAt: now, editedAt: now -- cgit v1.2.3 From 496477ebda3f6c347a9944e22daae447d15ebc31 Mon Sep 17 00:00:00 2001 From: Pitu Date: Mon, 11 May 2020 00:57:56 +0900 Subject: Feature: enable apiKey access to uploads and album fetching for the uploader/sharex/3rd party --- src/api/databaseMigration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 75611f3..5cf4b39 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -51,7 +51,7 @@ const start = async () => { password: user.password, enabled: user.enabled == 1 ? true : false, isAdmin: false, - apiKey: user.token, // Is this the best way to do it? + apiKey: user.token, passwordEditedAt: now, apiKeyEditedAt: now, createdAt: now, -- cgit v1.2.3 From ad852de51a0d2dd5d29c08838d5a430c58849e74 Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Wed, 8 Jul 2020 04:00:12 +0300 Subject: chore: linter the entire project using the new rules --- src/api/databaseMigration.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 5cf4b39..d95605d 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -1,28 +1,31 @@ +/* eslint-disable eqeqeq */ +/* eslint-disable no-await-in-loop */ +/* eslint-disable no-console */ const nodePath = require('path'); const moment = require('moment'); 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 => { + postProcessResponse: (result) => { const booleanFields = [ 'enabled', 'enableDownload', - 'isAdmin' + 'isAdmin', ]; - const processResponse = row => { - Object.keys(row).forEach(key => { + const processResponse = (row) => { + Object.keys(row).forEach((key) => { if (booleanFields.includes(key)) { if (row[key] === 0) row[key] = false; else if (row[key] === 1) row[key] = true; @@ -31,11 +34,11 @@ const newDb = require('knex')({ return row; }; - if (Array.isArray(result)) return result.map(row => processResponse(row)); + if (Array.isArray(result)) return result.map((row) => processResponse(row)); if (typeof result === 'object') return processResponse(result); return result; }, - useNullAsDefault: true + useNullAsDefault: true, }); const start = async () => { @@ -49,13 +52,13 @@ const start = async () => { id: user.id, username: user.username, password: user.password, - enabled: user.enabled == 1 ? true : false, + enabled: user.enabled == 1, isAdmin: false, apiKey: user.token, passwordEditedAt: now, apiKeyEditedAt: now, createdAt: now, - editedAt: now + editedAt: now, }; await newDb.table('users').insert(userToInsert); } @@ -71,7 +74,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, @@ -81,13 +84,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...'); @@ -106,12 +109,12 @@ 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, }); } await newDb.batchInsert('files', filesToInsert, 20); -- cgit v1.2.3 From 898a2dde78ae2ed352eec2b845fc09ecda864451 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 05:50:59 +0900 Subject: Re-process thumbnails on migration --- src/api/databaseMigration.js | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 5cf4b39..06d3849 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -1,5 +1,9 @@ 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 oldDb = require('knex')({ client: 'sqlite3', @@ -42,6 +46,10 @@ 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')); + console.log('Finished deleting old thumbnails to create new ones'); + const users = await oldDb.table('users').where('username', '<>', 'root'); for (const user of users) { const now = moment.utc().toDate(); @@ -113,6 +121,7 @@ const start = async () => { albumId: file.albumid, fileId: file.id }); + generateThumbnails(file.name); } await newDb.batchInsert('files', filesToInsert, 20); await newDb.batchInsert('albumsFiles', albumsFilesToInsert, 20); @@ -122,4 +131,47 @@ const start = async () => { process.exit(0); }; +const imageExtensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webp']; +const videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov']; + +const generateThumbnails = filename => { + if (!jetpack.exists(nodePath.join(__dirname, '..', '..', 'uploads', filename))) return; + + const ext = nodePath.extname(filename).toLowerCase(); + const output = `${filename.slice(0, -ext.length)}.webp`; + if (imageExtensions.includes(ext)) return generateThumbnailForImage(filename, output); + if (videoExtensions.includes(ext)) return generateThumbnailForVideo(filename); +}; + +const generateThumbnailForImage = async (filename, output) => { + 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)); +}; + +const generateThumbnailForVideo = filename => { + 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)); +}; + start(); -- cgit v1.2.3 From 8e3c3841a4d0f1a6bc71039f94e030b8526105e8 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 18 Jul 2020 16:39:24 +0900 Subject: Update database migration --- src/api/databaseMigration.js | 83 ++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 38 deletions(-) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 06d3849..15b2bf7 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -5,6 +5,9 @@ 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 oldDb = require('knex')({ client: 'sqlite3', connection: { @@ -121,7 +124,13 @@ const start = async () => { albumId: file.albumid, fileId: file.id }); - generateThumbnails(file.name); + + const filename = file.name; + 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); + if (videoExtensions.includes(ext)) generateThumbnailForVideo(filename); } await newDb.batchInsert('files', filesToInsert, 20); await newDb.batchInsert('albumsFiles', albumsFilesToInsert, 20); @@ -131,47 +140,45 @@ const start = async () => { process.exit(0); }; -const imageExtensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webp']; -const videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov']; - -const generateThumbnails = filename => { - if (!jetpack.exists(nodePath.join(__dirname, '..', '..', 'uploads', filename))) return; - - const ext = nodePath.extname(filename).toLowerCase(); - const output = `${filename.slice(0, -ext.length)}.webp`; - if (imageExtensions.includes(ext)) return generateThumbnailForImage(filename, output); - if (videoExtensions.includes(ext)) return generateThumbnailForVideo(filename); -}; - const generateThumbnailForImage = async (filename, output) => { - 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)); + 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 => { - 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)); + 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(); -- cgit v1.2.3 From 90001c2df56d58e69fd199a518ae7f3e4ed327fc Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Thu, 24 Dec 2020 10:40:50 +0200 Subject: chore: remove trailing commas --- src/api/databaseMigration.js | 111 +++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 56 deletions(-) (limited to 'src/api/databaseMigration.js') 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(); -- cgit v1.2.3 From fb2c27086f570fec60f4d52dcc9ca80e53186293 Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 24 Dec 2020 23:45:16 +0900 Subject: Fix ESLint rules once and for all --- src/api/databaseMigration.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 7e919f3..9afbb4a 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -27,7 +27,7 @@ const generateThumbnailForImage = async (filename, output) => { } }; -const generateThumbnailForVideo = (filename) => { +const generateThumbnailForVideo = filename => { try { ffmpeg(nodePath.join(__dirname, '../../uploads', filename)) .thumbnail({ @@ -36,7 +36,7 @@ const generateThumbnailForVideo = (filename) => { folder: nodePath.join(__dirname, '../../uploads/thumbs/square'), size: '64x64' }) - .on('error', (error) => console.error(error.message)); + .on('error', error => console.error(error.message)); ffmpeg(nodePath.join(__dirname, '../../uploads', filename)) .thumbnail({ timestamps: [0], @@ -44,7 +44,7 @@ const generateThumbnailForVideo = (filename) => { folder: nodePath.join(__dirname, '../../uploads/thumbs'), size: '150x?' }) - .on('error', (error) => console.error(error.message)); + .on('error', error => console.error(error.message)); console.log('finished', filename); } catch (error) { console.log('error', filename); @@ -64,15 +64,15 @@ const newDb = require('knex')({ connection: { filename: nodePath.join(__dirname, '../../', 'database.sqlite') }, - postProcessResponse: (result) => { + postProcessResponse: result => { const booleanFields = [ 'enabled', 'enableDownload', 'isAdmin' ]; - const processResponse = (row) => { - Object.keys(row).forEach((key) => { + const processResponse = row => { + Object.keys(row).forEach(key => { if (booleanFields.includes(key)) { if (row[key] === 0) row[key] = false; else if (row[key] === 1) row[key] = true; @@ -81,7 +81,7 @@ const newDb = require('knex')({ return row; }; - if (Array.isArray(result)) return result.map((row) => processResponse(row)); + if (Array.isArray(result)) return result.map(row => processResponse(row)); if (typeof result === 'object') return processResponse(result); return result; }, -- cgit v1.2.3 From f73cde6bb501d72e46f0aadf95e6809e9d265e5b Mon Sep 17 00:00:00 2001 From: Pitu Date: Fri, 25 Dec 2020 03:58:19 +0900 Subject: Chore: Move database to a subfolder for docker purposes --- src/api/databaseMigration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 9afbb4a..73ec85c 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -62,7 +62,7 @@ const oldDb = require('knex')({ const newDb = require('knex')({ client: 'sqlite3', connection: { - filename: nodePath.join(__dirname, '../../', 'database.sqlite') + filename: nodePath.join(__dirname, '../../database/', 'database.sqlite') }, postProcessResponse: result => { const booleanFields = [ -- cgit v1.2.3 From 7d5b3c4ac787d41d9eedf8a02315d54e6ac7e430 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 3 Jan 2021 22:48:07 +0900 Subject: Update migration --- src/api/databaseMigration.js | 58 +++----------------------------------------- 1 file changed, 4 insertions(+), 54 deletions(-) (limited to 'src/api/databaseMigration.js') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 73ec85c..71ee2e6 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -1,55 +1,7 @@ -/* eslint-disable eqeqeq */ -/* eslint-disable no-await-in-loop */ -/* eslint-disable no-console */ const nodePath = require('path'); const moment = require('moment'); const jetpack = 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 ThumbUtil = require('./utils/ThumbUtil'); const oldDb = require('knex')({ client: 'sqlite3', @@ -68,7 +20,8 @@ const newDb = require('knex')({ const booleanFields = [ 'enabled', 'enableDownload', - 'isAdmin' + 'isAdmin', + 'nsfw' ]; const processResponse = row => { @@ -170,10 +123,7 @@ const start = async () => { const filename = file.name; 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); - if (videoExtensions.includes(ext)) generateThumbnailForVideo(filename); + ThumbUtil.generateThumbnails(filename); } await newDb.batchInsert('files', filesToInsert, 20); await newDb.batchInsert('albumsFiles', albumsFilesToInsert, 20); -- cgit v1.2.3