filesafe/logger.js
Bobby Wibowo 51c8df71bc
Added deletion URL for ShareX or derivatives
For registered users only!
This requires adding a basic GET API for file deletion, so that I did.
Configs which guests download will not include pattern for delete URL,
so they won't get notified of unusable delete URL or anything like that.

dev: Improved logger.debug() to support specifying options for node's
Util.inspect() if an object is set as its last param
(assuming >1 params).
Default options now also includes enabling colors.

src/js/utils.js: Simplified dynamic ShareX config generator.
Among other things, it will now use JSON.stringify().
I don't even remember why we didn't use that in the first place..

Some logic improvements in src/js/home.js.

Bumped v1 version string and rebuilt client assets.
2020-06-20 01:28:23 +07:00

62 lines
1.4 KiB
JavaScript

const { inspect } = require('util')
const self = {}
// Only show shortened version (time) during development
const short = process.env.NODE_ENV === 'development'
const now = () => {
const time = new Date()
const parsed = {
hours: time.getHours(),
minutes: time.getMinutes(),
seconds: time.getSeconds()
}
if (!short) {
parsed.month = time.getMonth() + 1
parsed.date = time.getDate()
}
// Add leading zeroes and slice
Object.keys(parsed).forEach(key => {
parsed[key] = ('0' + parsed[key]).slice(-2)
})
return (!short ? `${time.getFullYear()}-${parsed.month}-${parsed.date} ` : '') +
`${parsed.hours}:${parsed.minutes}:${parsed.seconds}`
}
const clean = item => {
if (typeof item === 'string') return item
const cleaned = inspect(item, { depth: 0 })
return cleaned
}
const write = (content, options = {}) => {
const stream = options.error ? process.stderr : process.stdout
stream.write(`[${now()}] ${options.prefix || ''}${clean(content)}\n`)
}
self.log = write
self.error = (content, options = {}) => {
options.error = true
write(content, options)
}
self.debug = (...args) => {
const options = {
colors: true,
depth: Infinity
}
if (args.length > 1 && typeof args[args.length - 1] === 'object') {
Object.assign(options, args[args.length - 1])
args.splice(args.length - 1, 1)
}
for (const arg of args)
console.log(inspect(arg, options))
}
module.exports = self