mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +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 {
|
||||
|
||||
@@ -472,3 +472,18 @@ func TestPathFuncs(t *testing.T) {
|
||||
t.Errorf("%q is not empty. empty=%v error=%v", dir, empty, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocDir_DetectContentType(t *testing.T) {
|
||||
require := require.New(t)
|
||||
imgPath := "input/image.png"
|
||||
fileInfo, err := os.Stat(imgPath)
|
||||
require.Nil(err)
|
||||
res := detectContentType(fileInfo, imgPath)
|
||||
require.Equal("image/png", res)
|
||||
|
||||
jsonPath := "input/test.json"
|
||||
fileInfo, err = os.Stat(jsonPath)
|
||||
require.Nil(err)
|
||||
res = detectContentType(fileInfo, jsonPath)
|
||||
require.Equal("application/json", res)
|
||||
}
|
||||
|
||||
BIN
client/allocdir/input/image.png
Normal file
BIN
client/allocdir/input/image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 164 KiB |
3
client/allocdir/input/test.json
Normal file
3
client/allocdir/input/test.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"test":"test"
|
||||
}
|
||||
Reference in New Issue
Block a user