-
-
Notifications
You must be signed in to change notification settings - Fork 21
Add totalSize and totalCount values to VFS stats #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,20 +36,36 @@ const chokidar = require('chokidar'); | |
/* | ||
* Creates an object readable by client | ||
*/ | ||
const createFileIter = (core, realRoot, file) => { | ||
const createFileIter = (core, realRoot, file, options = {}) => { | ||
const filename = path.basename(file); | ||
const realPath = path.join(realRoot, filename); | ||
const {mime} = core.make('osjs/vfs'); | ||
|
||
const createStat = stat => ({ | ||
isDirectory: stat.isDirectory(), | ||
isFile: stat.isFile(), | ||
mime: stat.isFile() ? mime(realPath) : null, | ||
size: stat.size, | ||
path: file, | ||
filename, | ||
stat | ||
}); | ||
const createStat = async stat => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method of figuring out the total size of a directory seems very inefficient, especially when there's a lot of files. Maybe it would be better looking into spawning a system process to do this in a native way ? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I searched, const child = spawn('sh', ['-c', 'du -s ${realPath} | cut -f1']); But the problem is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But I would assume it's faster than doing it with node, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I'm not 100% sure, but I assume that this wouldn't be cross-compatible with users hoping to run OS.js locally on Windows. IMHO native system processes shouldn't be included without a fallback for Windows There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a du npm package that handles Windows etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In new commit du package is used for totalSize. @andersevenrud There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am thinking about our own vfs system adapter, which is a cloud object storage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I can't think of a good solution to that, especially if there's pagination involved :/ |
||
let totalSize = 0; | ||
let files = []; | ||
if (stat.isDirectory()) { | ||
files = await fs.readdir(realPath); | ||
if(!options.showHiddenFiles) { | ||
files = files.filter(f => f.substr(0, 1) !== '.'); | ||
} | ||
const promises = files.map(f => fs.stat(path.join(realPath, f))); | ||
const allPromises = await Promise.all(promises); | ||
allPromises.map(s => totalSize += s.size); | ||
} | ||
|
||
return ({ | ||
isDirectory: stat.isDirectory(), | ||
isFile: stat.isFile(), | ||
mime: stat.isFile() ? mime(realPath) : null, | ||
size: stat.size, | ||
path: file, | ||
totalCount: stat.isDirectory() ? files.length : null, | ||
totalSize: stat.isDirectory() ? totalSize : null, | ||
filename, | ||
stat | ||
}); | ||
}; | ||
|
||
return fs.stat(realPath) | ||
.then(createStat) | ||
|
@@ -196,7 +212,7 @@ module.exports = (core) => { | |
Promise.resolve(getRealPath(core, options.session, vfs.mount, file)) | ||
.then(realPath => { | ||
return fs.access(realPath, fs.F_OK) | ||
.then(() => createFileIter(core, path.dirname(realPath), realPath)); | ||
.then(() => createFileIter(core, path.dirname(realPath), realPath, options)); | ||
}), | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.