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

Infer content type in alloc fs stat endpoint #5907

Merged
merged 2 commits into from
Jul 1, 2019
Merged

Conversation

preetapan
Copy link
Contributor

This PR adds a new field to AllocFileInfo, used in the stat endpoint. It uses http.DetectContentType to infer content type for files.

Copy link
Contributor

@notnoop notnoop left a comment

Choose a reason for hiding this comment

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

lgtm - but few minor nit picky things but feel to merge when ready.

// detectContentType tries to infer the file type by reading the first
// 512 bytes of the file. Json file extensions are special cased.
func detectContentType(fileInfo os.FileInfo, path string) string {
contentType := "unknown"
Copy link
Contributor

Choose a reason for hiding this comment

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

I would default to application/octet-stream and consider returning rather than assigning to variable.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to this. It's essentially how I'd interpret unknown anyway, might as well use the proper generic binary type.

}
}
// Special case json files
if strings.HasSuffix(path, ".json") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that this is a cheaper check, I'd consider checking it first.

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably an unnecessary caution, but it's possible that a *.json file isn't actually json. Could be useful to only do this more targeted content typing if the file sniffing content type is in fact text.

fileInfo, err := os.Stat(imgPath)
require.Nil(err)
res := detectContentType(fileInfo, imgPath)
require.Equal("image/png", res)
Copy link
Contributor

Choose a reason for hiding this comment

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

more nit picking - seems like a good candidate for table testing - I would test go source files and maybe some application/octet-stream file as well.

Copy link
Contributor

@DingoEatingFuzz DingoEatingFuzz left a comment

Choose a reason for hiding this comment

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

Thank you for doing this! Looks good to me. Just had a couple picky things to say.

@preetapan preetapan merged commit 15df89b into master Jul 1, 2019
@endocrimes endocrimes deleted the f-infer-contenttype branch July 3, 2019 08:46
@github-actions
Copy link

github-actions bot commented Feb 7, 2023

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants