2021-08-18 00:01:11 +02:00
|
|
|
const fs = require('fs-extra')
|
2021-09-30 03:43:36 +02:00
|
|
|
const Logger = require('../Logger')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
async function getFileStat(path) {
|
|
|
|
try {
|
|
|
|
var stat = await fs.stat(path)
|
|
|
|
return {
|
|
|
|
size: stat.size,
|
|
|
|
atime: stat.atime,
|
|
|
|
mtime: stat.mtime,
|
|
|
|
ctime: stat.ctime,
|
|
|
|
birthtime: stat.birthtime
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed to stat', err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports.getFileStat = getFileStat
|
|
|
|
|
2021-09-04 21:17:26 +02:00
|
|
|
async function getFileSize(path) {
|
|
|
|
var stat = await getFileStat(path)
|
|
|
|
if (!stat) return 0
|
|
|
|
return stat.size || 0
|
|
|
|
}
|
|
|
|
module.exports.getFileSize = getFileSize
|
|
|
|
|
2021-09-30 03:43:36 +02:00
|
|
|
async function readTextFile(path) {
|
|
|
|
try {
|
|
|
|
var data = await fs.readFile(path)
|
|
|
|
return String(data)
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`[FileUtils] ReadTextFile error ${error}`)
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports.readTextFile = readTextFile
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
function bytesPretty(bytes, decimals = 0) {
|
|
|
|
if (bytes === 0) {
|
|
|
|
return '0 Bytes'
|
|
|
|
}
|
|
|
|
const k = 1024
|
2021-09-29 17:16:38 +02:00
|
|
|
var dm = decimals < 0 ? 0 : decimals
|
2021-08-18 00:01:11 +02:00
|
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
2021-09-29 17:16:38 +02:00
|
|
|
if (i > 2 && dm === 0) dm = 1
|
2021-08-18 00:01:11 +02:00
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
|
|
|
|
}
|
|
|
|
module.exports.bytesPretty = bytesPretty
|
|
|
|
|
|
|
|
function elapsedPretty(seconds) {
|
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
if (minutes < 70) {
|
|
|
|
return `${minutes} min`
|
|
|
|
}
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
|
|
|
if (!minutes) {
|
|
|
|
return `${hours} hr`
|
|
|
|
}
|
|
|
|
return `${hours} hr ${minutes} min`
|
|
|
|
}
|
|
|
|
module.exports.elapsedPretty = elapsedPretty
|
|
|
|
|
|
|
|
function secondsToTimestamp(seconds) {
|
|
|
|
var _seconds = seconds
|
|
|
|
var _minutes = Math.floor(seconds / 60)
|
|
|
|
_seconds -= _minutes * 60
|
|
|
|
var _hours = Math.floor(_minutes / 60)
|
|
|
|
_minutes -= _hours * 60
|
|
|
|
_seconds = Math.round(_seconds)
|
|
|
|
if (!_hours) {
|
|
|
|
return `${_minutes}:${_seconds.toString().padStart(2, '0')}`
|
|
|
|
}
|
|
|
|
return `${_hours}:${_minutes.toString().padStart(2, '0')}:${_seconds.toString().padStart(2, '0')}`
|
|
|
|
}
|
2021-10-07 04:08:52 +02:00
|
|
|
module.exports.secondsToTimestamp = secondsToTimestamp
|
|
|
|
|
|
|
|
function setFileOwner(path, uid, gid) {
|
|
|
|
try {
|
|
|
|
return fs.chown(path, uid, gid).then(() => true)
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Failed set file owner', err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports.setFileOwner = setFileOwner
|