Fix init being smarter and less io intensive, skip reading size of files during startup
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed

This commit is contained in:
Jonatan Nilsson 2022-08-15 19:51:13 +00:00
parent 71c8937f93
commit 9790f298f3
2 changed files with 19 additions and 19 deletions

View file

@ -19,7 +19,7 @@ export default class MediaRoutes {
} }
register(server) { register(server) {
this.init().then(function() {}, function(err) { this.init(server).then(function() {}, function(err) {
server.core.log.error(err, 'Error initing media') server.core.log.error(err, 'Error initing media')
}) })
@ -32,24 +32,24 @@ export default class MediaRoutes {
server.flaska.delete('/media/:filename', [server.queryHandler()], this.remove.bind(this)) server.flaska.delete('/media/:filename', [server.queryHandler()], this.remove.bind(this))
} }
init() { init(server) {
return fs.readdir(config.get('uploadFolder')).then(folders => { let folders = Object.keys(config.get('sites'))
return Promise.all(folders.map(folder => { return Promise.all(folders.map(folder => {
return fs.readdir(config.get('uploadFolder') + '/' + folder) return fs.readdir(config.get('uploadFolder') + '/' + folder)
.then(files => { .then(files => {
return Promise.all(files.map(file => { return files.map(file => {
return fs.stat(`${config.get('uploadFolder')}/${folder}/${file}`) return { filename: file, size: null }
.then(function(stat) {
return { filename: file, size: stat.size }
}) })
})) })
.catch(err => {
server.core.log.error(err, `Error reading folder "${config.get('uploadFolder')}/${folder}"`)
return []
}) })
.then(files => { .then(files => {
this.siteCache.set(folder, files) this.siteCache.set(folder, files)
}) })
.catch(function() {})
})) }))
})
} }
filesCacheGet(site) { filesCacheGet(site) {

View file

@ -1,6 +1,6 @@
{ {
"name": "storage-upload", "name": "storage-upload",
"version": "2.2.3", "version": "2.2.4",
"description": "Micro service for uploading and image resizing files to a storage server.", "description": "Micro service for uploading and image resizing files to a storage server.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {