-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Conversation
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.
lgtm - but few minor nit picky things but feel to merge when ready.
client/allocdir/alloc_dir.go
Outdated
// 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" |
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.
I would default to application/octet-stream
and consider returning rather than assigning to variable.
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.
+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") { |
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.
Given that this is a cheaper check, I'd consider checking it first.
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.
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.
client/allocdir/alloc_dir_test.go
Outdated
fileInfo, err := os.Stat(imgPath) | ||
require.Nil(err) | ||
res := detectContentType(fileInfo, imgPath) | ||
require.Equal("image/png", res) |
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.
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.
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.
Thank you for doing this! Looks good to me. Just had a couple picky things to say.
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. |
This PR adds a new field to
AllocFileInfo
, used in the stat endpoint. It uses http.DetectContentType to infer content type for files.