filesafe/scripts/thumbs.js

132 lines
4.1 KiB
JavaScript
Raw Normal View History

const path = require('path')
Updates (very important to read) Client-side CSS & JS files will now be processed with Gulp. Gulp tasks are configured in gulpfile.js file. CSS files will be optimized with postcss-preset-env, which will auto-add vendor prefixes and convert any parts necessary for browsers compatibility. Afterwards they will be minified with cssnano. JS files will be optimized with bublé, likewise for browsers compatibility. Afterwards they will be minified with terser. Unprocessed CSS & JS files will now be located at src directory, while the processed results will be located at dist directory. Due to bublé, the JS files should now be compatible up to IE 11 at the minimum. Previously the safe would not work in IE 11 due to extensive usage of template literals. Due to that as well, JS files in src directory will now extensively use arrow functions for my personal comfort (as they will be converted too). The server will use the processed files at dist directory by default. If you want to rebuild the files by your own, you can run "yarn build". Gulp is a development dependency, so make sure you have installed all development dependencies (e.i. NOT using "yarn install --production"). --- yarn lint -> gulp lint yarn build -> gulp default yarn watch -> gulp watch yarn develop -> env NODE_ENV=development yarn watch --- Fixed not being able to demote staff into normal users. /api/token/verify will no longer respond with 401 HTTP error code, unless an error occurred (which will be 500 HTTP error code). Fixed /nojs route not displaying file's original name when a duplicate is found on the server. Removed is-breeze CSS class name, in favor of Bulma's is-info. Removed custom styling from auth page, in favor of global styling. Removed all usage of style HTML attribute in favor of CSS classes. Renamed js/s/ to js/misc/. Use loading spinners on dashboard's sidebar menus. Disable all other sidebar menus when something is loading. Changed title HTML attribute of disabled control buttons in uploads & users list. Hid checkboxes and WIP controls from users list. Better error messages handling. Especially homepage will now support CF's HTTP error codes. Updated various icons. Also, added fontello config file at public/libs/fontello/config.json. This should let you edit them more easily with fontello. Use Gatsby icon for my blog's link in homepage's footer. A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
const paths = require('../controllers/pathsController')
const utils = require('../controllers/utilsController')
2020-12-27 11:54:05 +00:00
const config = require('./../config')
const db = require('knex')(config.database)
const self = {
mode: null,
2020-12-27 11:54:05 +00:00
mayGenerateThumb: extname => {
return ([1, 3].includes(self.mode) && utils.imageExts.includes(extname)) ||
([2, 3].includes(self.mode) && utils.videoExts.includes(extname))
2020-12-27 11:54:05 +00:00
},
getFiles: async directory => {
const names = await paths.readdir(directory)
const files = []
for (const name of names) {
const lstat = await paths.lstat(path.join(directory, name))
if (lstat.isFile() && !name.startsWith('.')) {
files.push(name)
}
}
2020-12-27 11:54:05 +00:00
return files
}
}
;(async () => {
const location = process.argv[1].replace(process.cwd() + '/', '')
const args = process.argv.slice(2)
self.mode = parseInt(args[0])
2020-12-27 11:54:05 +00:00
const force = parseInt(args[1]) || 0
const verbose = parseInt(args[2]) || 0
const cfcache = parseInt(args[3]) || 0
if (![1, 2, 3].includes(self.mode) ||
2020-12-27 11:54:05 +00:00
![0, 1].includes(force) ||
![0, 1, 2].includes(verbose) ||
![0, 1].includes(cfcache) ||
args.includes('--help') ||
args.includes('-h')) {
Updates (very important to read) Client-side CSS & JS files will now be processed with Gulp. Gulp tasks are configured in gulpfile.js file. CSS files will be optimized with postcss-preset-env, which will auto-add vendor prefixes and convert any parts necessary for browsers compatibility. Afterwards they will be minified with cssnano. JS files will be optimized with bublé, likewise for browsers compatibility. Afterwards they will be minified with terser. Unprocessed CSS & JS files will now be located at src directory, while the processed results will be located at dist directory. Due to bublé, the JS files should now be compatible up to IE 11 at the minimum. Previously the safe would not work in IE 11 due to extensive usage of template literals. Due to that as well, JS files in src directory will now extensively use arrow functions for my personal comfort (as they will be converted too). The server will use the processed files at dist directory by default. If you want to rebuild the files by your own, you can run "yarn build". Gulp is a development dependency, so make sure you have installed all development dependencies (e.i. NOT using "yarn install --production"). --- yarn lint -> gulp lint yarn build -> gulp default yarn watch -> gulp watch yarn develop -> env NODE_ENV=development yarn watch --- Fixed not being able to demote staff into normal users. /api/token/verify will no longer respond with 401 HTTP error code, unless an error occurred (which will be 500 HTTP error code). Fixed /nojs route not displaying file's original name when a duplicate is found on the server. Removed is-breeze CSS class name, in favor of Bulma's is-info. Removed custom styling from auth page, in favor of global styling. Removed all usage of style HTML attribute in favor of CSS classes. Renamed js/s/ to js/misc/. Use loading spinners on dashboard's sidebar menus. Disable all other sidebar menus when something is loading. Changed title HTML attribute of disabled control buttons in uploads & users list. Hid checkboxes and WIP controls from users list. Better error messages handling. Especially homepage will now support CF's HTTP error codes. Updated various icons. Also, added fontello config file at public/libs/fontello/config.json. This should let you edit them more easily with fontello. Use Gatsby icon for my blog's link in homepage's footer. A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
return console.log(utils.stripIndents(`
Generate thumbnails.
2020-12-27 11:54:05 +00:00
Usage:
node ${location} <mode=1|2|3> [force=0|1] [verbose=0|1] [cfcache=0|1]
2020-12-27 11:54:05 +00:00
mode : 1 = images only, 2 = videos only, 3 = both images and videos
force : 0 = no force (default), 1 = overwrite existing thumbnails
verbose : 0 = only print missing thumbs (default), 1 = print all, 2 = print nothing
cfcache : 0 = do not clear cloudflare cache (default), 1 = clear cloudflare cache
`).trim())
}
2020-05-27 21:23:26 +00:00
console.log('Looking through existing thumbnails\u2026')
2020-12-27 11:54:05 +00:00
const uploads = await db.table('files')
.select('id', 'name')
const thumbs = await self.getFiles(paths.thumbs)
.then(thumbs => thumbs.map(thumb => {
const extname = path.extname(thumb)
return thumb.slice(0, -extname.length)
}))
console.log(`Found ${thumbs.length} existing thumbnails (may include placeholder symlinks).`)
const succeeded = []
let error = 0
2020-12-27 11:54:05 +00:00
let exists = 0
let skipped = 0
const printProgress = () => {
const done = succeeded.length + error + exists + skipped
console.log(`PROGRESS: ${done}/${uploads.length}`)
if (done >= uploads.length) clearInterval(progressInterval)
}
const progressInterval = setInterval(printProgress, 1000)
printProgress()
for (const upload of uploads) {
2020-12-27 11:54:05 +00:00
const extname = utils.extname(upload.name)
const basename = upload.name.slice(0, -extname.length)
2020-12-27 11:54:05 +00:00
if (thumbs.includes(basename) && !force) {
if (verbose === 1) {
console.log(`${upload.name}: ALREADY EXISTS.`)
}
exists++
} else if (!self.mayGenerateThumb(extname)) {
2020-12-27 11:54:05 +00:00
if (verbose === 1) {
console.log(`${upload.name}: EXTENSION SKIPPED.`)
}
skipped++
} else {
const start = Date.now()
2020-12-27 11:54:05 +00:00
const generated = await utils.generateThumbs(upload.name, extname, force)
if (verbose !== 2) {
console.log(`${upload.name}: ${(Date.now() - start) / 1000}s: ${generated ? 'OK' : 'ERROR'}`)
}
generated ? succeeded.push({ upload, extname }) : error++
}
}
clearInterval(progressInterval)
printProgress()
2020-12-27 11:54:05 +00:00
console.log(utils.stripIndents(`
---
Success: ${succeeded.length}
Error: ${error}
Already exists: ${exists}
Extension skipped: ${skipped}
---
`).trim())
2020-12-27 11:54:05 +00:00
if (cfcache && succeeded.length) {
console.log('Purging Cloudflare\'s cache...')
2020-12-27 11:54:05 +00:00
const results = await utils.purgeCloudflareCache(succeeded.map(data =>
`thumbs/${data.upload.name.slice(0, -data.extname.length)}.png`
), true, false)
for (let i = 0; i < results.length; i++) {
if (results[i].errors.length) {
results[i].errors.forEach(error => console.error(`CF: ${error}`))
}
console.log(`Status [${i}]: ${results[i].success ? 'OK' : 'ERROR'}`)
}
}
})()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})