filesafe/scripts/migrate.js

105 lines
3.1 KiB
JavaScript
Raw Normal View History

const jetpack = require('fs-jetpack')
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 perms = require('./../controllers/permissionController')
const config = require('./../controllers/utils/ConfigManager')
const map = {
files: {
expirydate: 'integer'
},
albums: {
editedAt: 'integer',
More improvements to albums, and others Improvements related to albums: * Changed "rename album" option with a better "edit album" feature. With it you can also disable download or public link and even request a new public link (https://i.fiery.me/fz1y.png). This also adds a new API route: /api/albums/edit. The old API route, /api/albums/rename, is still available but will silently be using the new API in backend. * Deleting album will now also delete its zip archive if exists. * Renaming albums will also rename its zip archive if exists. * Generating zip will use async fs.readFile instead of fs.readFileSync. This should improve generating speed somewhat. * The codes that tries to generate random identifier for album will now check whether an album with the same identifier already exists. It will also rely on "uploads.maxTries" config option to limit how many times it will try to re-generate a new random identifier. * Added a new config option "uploads.albumIdentifierLength" which sets the length of the randomly generated identifier. * Added "download" and "public" columns to "albums" table in database/db.js. Existing users can run "node database/migration.js" to add the columns. Others: * uploadsController.getUniqueRandomName will no longer accept 3 paramters (previously it would accept a callback in the third parameter). It will now instead return a Promise. * Album name of disabled/deleted albums will no longer be shown in uploads list. * Added "fileLength" column to "users" table in database/db.js. * Renamed HTTP404.html and HTTP500.html in /pages/error to 404.html and 500.html respectively. I'm still using symlinks though. * Added a new CSS named sweetalert.css which will be used in homepage, auth and dashboard. It will style all sweetalert modals with dark theme (matching the current color scheme used in this branch). * Updated icons (added download icon). * Some other improvements/tweaks here and there.
2018-04-28 17:26:39 +00:00
zipGeneratedAt: 'integer',
download: 'integer',
public: 'integer',
description: 'string'
},
users: {
enabled: 'integer',
permission: 'integer',
registration: 'integer'
}
}
;(async () => {
2022-06-22 07:23:54 +00:00
if (['better-sqlite3', 'sqlite3'].includes(config.database.client)) {
if (!await jetpack.existsAsync(config.database.connection.filename)) {
console.log('Sqlite3 database file missing. Assumes first install, migration skipped.')
process.exit(0)
}
}
const db = require('knex')(config.database)
let done = 0
const tableNames = Object.keys(map)
for (const tableName of tableNames) {
const columnNames = Object.keys(map[tableName])
for (const columnName of columnNames) {
if (await db.schema.hasColumn(tableName, columnName)) continue
const columnType = map[tableName][columnName]
await db.schema.table(tableName, table => {
table[columnType](columnName)
})
console.log(`OK: ${tableName} <- ${columnName} (${columnType})`)
done++
}
}
const root = await db.table('users')
2018-10-09 19:52:41 +00:00
.where('username', 'root')
.select('permission')
2018-10-09 19:52:41 +00:00
.first()
if (root && root.permission !== perms.permissions.superadmin) {
await db.table('users')
.where('username', 'root')
.first()
.update({
permission: perms.permissions.superadmin
})
.then(result => {
// NOTE: permissionController.js actually has a hard-coded check for "root" account so that
// it will always have "superadmin" permission regardless of its permission value in database
console.log(`Updated root's permission to ${perms.permissions.superadmin} (superadmin).`)
done++
})
}
2018-10-09 19:52:41 +00:00
const filesOutdatedSize = await db.table('files')
.where('size', 'like', '%.0')
if (filesOutdatedSize.length) {
console.log(`Found ${filesOutdatedSize.length} files with outdated "size" field, converting\u2026`)
for (const file of filesOutdatedSize) {
const size = file.size.replace(/\.0$/, '')
await db.table('files')
.update('size', size)
.where('id', file.id)
done++
}
}
const filesMissingType = await db.table('files')
.where('type', '')
.orWhereNull('type')
if (filesMissingType.length) {
console.log(`Found ${filesMissingType.length} files with invalid "type" field, converting\u2026`)
for (const file of filesMissingType) {
await db.table('files')
.update('type', 'application/octet-stream')
.where('id', file.id)
done++
}
}
let status = 'Database migration was not required.'
if (done) {
status = `Completed ${done} database migration task(s).`
}
console.log(`${status} You may now start lolisafe normally.`)
})()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})