mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 18:35:44 +03:00
Infer content type in alloc fs stat endpoint
This commit is contained in:
@@ -11,6 +11,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
multierror "github.com/hashicorp/go-multierror"
|
||||
cstructs "github.com/hashicorp/nomad/client/structs"
|
||||
@@ -392,15 +395,41 @@ func (d *AllocDir) Stat(path string) (*cstructs.AllocFileInfo, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contentType := detectContentType(info, p)
|
||||
|
||||
return &cstructs.AllocFileInfo{
|
||||
Size: info.Size(),
|
||||
Name: info.Name(),
|
||||
IsDir: info.IsDir(),
|
||||
FileMode: info.Mode().String(),
|
||||
ModTime: info.ModTime(),
|
||||
Size: info.Size(),
|
||||
Name: info.Name(),
|
||||
IsDir: info.IsDir(),
|
||||
FileMode: info.Mode().String(),
|
||||
ModTime: info.ModTime(),
|
||||
ContentType: contentType,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 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"
|
||||
if !fileInfo.IsDir() {
|
||||
f, err := os.Open(path)
|
||||
// Best effort content type detection
|
||||
// We ignore errors because this is optional information
|
||||
if err == nil {
|
||||
fileBytes := make([]byte, 512)
|
||||
_, err := f.Read(fileBytes)
|
||||
if err == nil {
|
||||
contentType = http.DetectContentType(fileBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Special case json files
|
||||
if strings.HasSuffix(path, ".json") {
|
||||
contentType = "application/json"
|
||||
}
|
||||
return contentType
|
||||
}
|
||||
|
||||
// ReadAt returns a reader for a file at the path relative to the alloc dir
|
||||
func (d *AllocDir) ReadAt(path string, offset int64) (io.ReadCloser, error) {
|
||||
if escapes, err := structs.PathEscapesAllocDir("", path); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user