-
Notifications
You must be signed in to change notification settings - Fork 90
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
Extended dir-list information #241
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
49eff6a
-refactored dirlist to use promises
Jelenkee 7112d1e
-added lastModified
Jelenkee 8ee1641
-added format url parameter
Jelenkee 8b2b95e
-added docs
Jelenkee 98503ab
-fixed compatibility with node 10
Jelenkee 353ca84
-removed unnecessary request
Jelenkee 6d1094b
-replaced p-map with p-limit
Jelenkee 3769c3c
-removed logging
Jelenkee 091b356
-added error catch
Jelenkee 4acf69e
-improved test
Jelenkee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const fs = require('fs') | ||
const fs = require('fs').promises | ||
const pMap = require('p-map') | ||
|
||
const dirList = { | ||
/** | ||
|
@@ -10,36 +11,82 @@ const dirList = { | |
* @param {function(error, entries)} callback | ||
* note: can't use glob because don't get error on non existing dir | ||
*/ | ||
list: function (dir, callback) { | ||
list: async function (dir, options) { | ||
const entries = { dirs: [], files: [] } | ||
fs.readdir(dir, (err, files) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
if (files.length < 1) { | ||
callback(null, entries) | ||
const files = await fs.readdir(dir) | ||
if (files.length < 1) { | ||
return entries | ||
} | ||
|
||
await Promise.all(files.map(async filename => { | ||
let stats | ||
try { | ||
stats = await fs.stat(path.join(dir, filename)) | ||
} catch (error) { | ||
return | ||
} | ||
let j = 0 | ||
for (let i = 0; i < files.length; i++) { | ||
const filename = files[i] | ||
fs.stat(path.join(dir, filename), (err, file) => { | ||
if (!err) { | ||
if (file.isDirectory()) { | ||
entries.dirs.push(filename) | ||
} else { | ||
entries.files.push(filename) | ||
} | ||
const entry = { name: filename, stats } | ||
if (stats.isDirectory()) { | ||
if (options.extendedFolderInfo) { | ||
entry.extendedInfo = await getExtendedInfo(path.join(dir, filename)) | ||
} | ||
entries.dirs.push(entry) | ||
} else { | ||
entries.files.push(entry) | ||
} | ||
})) | ||
|
||
async function getExtendedInfo (folderPath) { | ||
const depth = folderPath.split(path.sep).length | ||
let totalSize = 0 | ||
let fileCount = 0 | ||
let totalFileCount = 0 | ||
let folderCount = 0 | ||
let totalFolderCount = 0 | ||
let lastModified = 0 | ||
|
||
async function walk (dir) { | ||
const files = await fs.readdir(dir) | ||
await pMap(files, async filename => { | ||
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. If we are not interested in the result, maybe we could just use p-limit instead. 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. done |
||
const filePath = path.join(dir, filename) | ||
let stats | ||
try { | ||
stats = await fs.stat(filePath) | ||
} catch (error) { | ||
return | ||
} | ||
|
||
if (j++ >= files.length - 1) { | ||
entries.dirs.sort() | ||
entries.files.sort() | ||
callback(null, entries) | ||
if (stats.isDirectory()) { | ||
totalFolderCount++ | ||
if (filePath.split(path.sep).length === depth + 1) { | ||
folderCount++ | ||
} | ||
await walk(filePath) | ||
} else { | ||
totalSize += stats.size | ||
totalFileCount++ | ||
if (filePath.split(path.sep).length === depth + 1) { | ||
fileCount++ | ||
} | ||
lastModified = Math.max(lastModified, stats.mtimeMs) | ||
} | ||
}) | ||
}, { concurrency: 4 }) | ||
} | ||
}) | ||
|
||
await walk(folderPath) | ||
return { | ||
totalSize, | ||
fileCount, | ||
totalFileCount, | ||
folderCount, | ||
totalFolderCount, | ||
lastModified | ||
} | ||
} | ||
|
||
entries.dirs.sort((a, b) => a.name.localeCompare(b.name)) | ||
entries.files.sort((a, b) => a.name.localeCompare(b.name)) | ||
return entries | ||
}, | ||
|
||
/** | ||
|
@@ -49,33 +96,41 @@ const dirList = { | |
* @param {ListOptions} options | ||
* @param {string} route request route | ||
*/ | ||
send: function ({ reply, dir, options, route }) { | ||
dirList.list(dir, (err, entries) => { | ||
if (err) { | ||
reply.callNotFound() | ||
return | ||
} | ||
send: async function ({ reply, dir, options, route }) { | ||
let entries | ||
try { | ||
entries = await dirList.list(dir, options) | ||
} catch (error) { | ||
return reply.callNotFound() | ||
} | ||
const format = reply.request.query.format || options.format | ||
if (format !== 'html') { | ||
if (options.jsonFormat !== 'extended') { | ||
const nameEntries = { dirs: [], files: [] } | ||
entries.dirs.forEach(entry => nameEntries.dirs.push(entry.name)) | ||
entries.files.forEach(entry => nameEntries.files.push(entry.name)) | ||
|
||
if (options.format !== 'html') { | ||
reply.send(nameEntries) | ||
} else { | ||
reply.send(entries) | ||
return | ||
} | ||
return | ||
} | ||
|
||
const html = options.render( | ||
entries.dirs.map(entry => dirList.htmlInfo(entry, route)), | ||
entries.files.map(entry => dirList.htmlInfo(entry, route))) | ||
reply.type('text/html').send(html) | ||
}) | ||
const html = options.render( | ||
entries.dirs.map(entry => dirList.htmlInfo(entry, route)), | ||
entries.files.map(entry => dirList.htmlInfo(entry, route))) | ||
reply.type('text/html').send(html) | ||
}, | ||
|
||
/** | ||
* provide the html information about entry and route, to get name and full route | ||
* @param {string} entry file or dir name | ||
* @param entry file or dir name and stats | ||
* @param {string} route request route | ||
* @return {ListFile} | ||
*/ | ||
htmlInfo: function (entry, route) { | ||
return { href: path.join(path.dirname(route), entry).replace(/\\/g, '/'), name: entry } | ||
return { href: path.join(path.dirname(route), entry.name).replace(/\\/g, '/'), name: entry.name, stats: entry.stats, extendedInfo: entry.extendedInfo } | ||
}, | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use p-limit here. We are not really interested in the result of all the promises.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done