mirror of
https://github.com/rclone/rclone.git
synced 2025-12-06 00:03:32 +00:00
http: add basic metadata and provide it via serve
Co-authored-by: dougal <147946567+roucc@users.noreply.github.com>
This commit is contained in:
@@ -39,6 +39,26 @@ func Object(w http.ResponseWriter, r *http.Request, o fs.Object) {
|
||||
modTime := o.ModTime(r.Context())
|
||||
w.Header().Set("Last-Modified", modTime.UTC().Format(http.TimeFormat))
|
||||
|
||||
// Set metadata headers if present
|
||||
metadata, err := fs.GetMetadata(r.Context(), o)
|
||||
if err != nil {
|
||||
fs.Debugf(o, "Request get metadata error: %v", err)
|
||||
}
|
||||
if metadata != nil {
|
||||
if metadata["content-disposition"] != "" {
|
||||
w.Header().Set("Content-Disposition", metadata["content-disposition"])
|
||||
}
|
||||
if metadata["cache-control"] != "" {
|
||||
w.Header().Set("Cache-Control", metadata["cache-control"])
|
||||
}
|
||||
if metadata["content-language"] != "" {
|
||||
w.Header().Set("Content-Language", metadata["content-language"])
|
||||
}
|
||||
if metadata["content-encoding"] != "" {
|
||||
w.Header().Set("Content-Encoding", metadata["content-encoding"])
|
||||
}
|
||||
}
|
||||
|
||||
if r.Method == "HEAD" {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/fs/object"
|
||||
"github.com/rclone/rclone/fstest/mockobject"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -82,3 +84,23 @@ func TestObjectBadRange(t *testing.T) {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
assert.Equal(t, "Bad Request\n", string(body))
|
||||
}
|
||||
|
||||
func TestObjectHEADMetadata(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
r := httptest.NewRequest("HEAD", "http://example.com/aFile", nil)
|
||||
m := fs.Metadata{
|
||||
"content-disposition": "inline",
|
||||
"cache-control": "no-cache",
|
||||
"content-language": "en",
|
||||
"content-encoding": "gzip",
|
||||
}
|
||||
o := object.NewMemoryObject("aFile", time.Now(), []byte("")).
|
||||
WithMetadata(m).WithMimeType("text/plain; charset=utf-8")
|
||||
Object(w, r, o)
|
||||
resp := w.Result()
|
||||
assert.Equal(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
||||
assert.Equal(t, "inline", resp.Header.Get("Content-Disposition"))
|
||||
assert.Equal(t, "no-cache", resp.Header.Get("Cache-Control"))
|
||||
assert.Equal(t, "en", resp.Header.Get("Content-Language"))
|
||||
assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user