2021-08-18 00:01:11 +02:00
|
|
|
const fs = require('fs-extra')
|
2021-11-06 23:26:44 +01:00
|
|
|
const rra = require('recursive-readdir-async')
|
2021-11-19 01:53:00 +01:00
|
|
|
const axios = require('axios')
|
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
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
async function recurseFiles(path, relPathToReplace = null) {
|
2021-11-06 23:26:44 +01:00
|
|
|
path = path.replace(/\\/g, '/')
|
|
|
|
if (!path.endsWith('/')) path = path + '/'
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
if (relPathToReplace) {
|
|
|
|
relPathToReplace = relPathToReplace.replace(/\\/g, '/')
|
|
|
|
if (!relPathToReplace.endsWith('/')) relPathToReplace += '/'
|
|
|
|
} else {
|
|
|
|
relPathToReplace = path
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
const options = {
|
|
|
|
mode: rra.LIST,
|
|
|
|
recursive: true,
|
|
|
|
stats: false,
|
|
|
|
ignoreFolders: true,
|
|
|
|
extensions: true,
|
|
|
|
deep: true,
|
|
|
|
realPath: true,
|
|
|
|
normalizePath: true
|
|
|
|
}
|
|
|
|
var list = await rra.list(path, options)
|
|
|
|
if (list.error) {
|
|
|
|
Logger.error('[fileUtils] Recurse files error', list.error)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
list = list.filter((item) => {
|
|
|
|
if (item.error) {
|
|
|
|
Logger.error(`[fileUtils] Recurse files file "${item.fullName}" has error`, item.error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore any file if a directory or the filename starts with "."
|
2021-11-22 03:00:40 +01:00
|
|
|
var relpath = item.fullname.replace(relPathToReplace, '')
|
2021-11-06 23:26:44 +01:00
|
|
|
var pathStartsWithPeriod = relpath.split('/').find(p => p.startsWith('.'))
|
|
|
|
if (pathStartsWithPeriod) {
|
|
|
|
Logger.debug(`[fileUtils] Ignoring path has . "${relpath}"`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}).map((item) => ({
|
|
|
|
name: item.name,
|
2021-11-22 03:00:40 +01:00
|
|
|
path: item.fullname.replace(relPathToReplace, ''),
|
2021-11-06 23:26:44 +01:00
|
|
|
dirpath: item.path,
|
2021-11-22 03:00:40 +01:00
|
|
|
reldirpath: item.path.replace(relPathToReplace, ''),
|
2021-11-06 23:26:44 +01:00
|
|
|
fullpath: item.fullname,
|
|
|
|
extension: item.extension,
|
|
|
|
deep: item.deep
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Sort from least deep to most
|
|
|
|
list.sort((a, b) => a.deep - b.deep)
|
|
|
|
|
|
|
|
// list.forEach((l) => {
|
|
|
|
// console.log(`${l.deep}: ${l.path}`)
|
|
|
|
// })
|
|
|
|
return list
|
|
|
|
}
|
2021-11-18 02:19:24 +01:00
|
|
|
module.exports.recurseFiles = recurseFiles
|
|
|
|
|
|
|
|
module.exports.downloadFile = async (url, filepath) => {
|
|
|
|
Logger.debug(`[fileUtils] Downloading file to ${filepath}`)
|
|
|
|
|
|
|
|
const writer = fs.createWriteStream(filepath)
|
|
|
|
const response = await axios({
|
|
|
|
url,
|
|
|
|
method: 'GET',
|
|
|
|
responseType: 'stream'
|
|
|
|
})
|
|
|
|
response.data.pipe(writer)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
writer.on('finish', resolve)
|
|
|
|
writer.on('error', reject)
|
|
|
|
})
|
|
|
|
}
|