Skip to content
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

Serve files with no extension #43

Merged
merged 2 commits into from
Nov 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions bin/list
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ const renderDirectory = async dir => {
const index = files.indexOf(file)
const details = path.parse(filePath)

details.ext = details.ext.split('.')[1]
details.relative = path.join(subPath, details.base)

if (await isDir(filePath)) {
details.base += '/'
} else {
details.ext = details.ext.split('.')[1] || 'txt'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the || 'txt'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For files with no extension, details.ext.split('.')[1] is undefined. details.ext is used here to add a class that determines which icon is used, and if it's undefined, the class that renders the directory icon will apply.

Using anyrandomstring instead of txt would yield the same results, but I chose txt in order to be consistent with how.txt files are handled.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for the explanation! I asked because it was confusing. 👍


let fileStats

try {
Expand Down Expand Up @@ -184,13 +185,9 @@ const handler = async (req, res) => {
}

// Check if file or directory path
if (path.parse(related).ext === '') {
if (await isDir(related)) {
let indexPath = path.join(related, '/index.html')

if (!await isDir(related) && flags.single === undefined) {
return send(res, 404, notFoundResponse)
}

res.setHeader('Content-Type', mime.contentType(path.extname(indexPath)))

if (!fs.existsSync(indexPath)) {
Expand Down