mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
webdav: add MIMEType to FileInfo
This commit is contained in:
parent
c673e7c7e7
commit
02d1a7dbe8
@ -2,6 +2,7 @@ package webdav
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -38,6 +39,8 @@ func fileInfoFromOS(href string, fi os.FileInfo) *FileInfo {
|
|||||||
Size: fi.Size(),
|
Size: fi.Size(),
|
||||||
ModTime: fi.ModTime(),
|
ModTime: fi.ModTime(),
|
||||||
IsDir: fi.IsDir(),
|
IsDir: fi.IsDir(),
|
||||||
|
// TODO: fallback to http.DetectContentType?
|
||||||
|
MIMEType: mime.TypeByExtension(path.Ext(href)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
37
server.go
37
server.go
@ -3,10 +3,8 @@ package webdav
|
|||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/emersion/go-webdav/internal"
|
"github.com/emersion/go-webdav/internal"
|
||||||
@ -88,22 +86,18 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
|
if fi.MIMEType != "" {
|
||||||
|
w.Header().Set("Content-Type", fi.MIMEType)
|
||||||
|
}
|
||||||
|
if !fi.ModTime.IsZero() {
|
||||||
|
w.Header().Set("Last-Modified", fi.ModTime.UTC().Format(http.TimeFormat))
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Length", strconv.FormatInt(fi.Size, 10))
|
||||||
|
|
||||||
if rs, ok := f.(io.ReadSeeker); ok {
|
if rs, ok := f.(io.ReadSeeker); ok {
|
||||||
// If it's an io.Seeker, use http.ServeContent which supports ranges
|
// If it's an io.Seeker, use http.ServeContent which supports ranges
|
||||||
http.ServeContent(w, r, r.URL.Path, fi.ModTime, rs)
|
http.ServeContent(w, r, r.URL.Path, fi.ModTime, rs)
|
||||||
} else {
|
} else {
|
||||||
// TODO: fallback to http.DetectContentType
|
|
||||||
t := mime.TypeByExtension(path.Ext(r.URL.Path))
|
|
||||||
if t != "" {
|
|
||||||
w.Header().Set("Content-Type", t)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !fi.ModTime.IsZero() {
|
|
||||||
w.Header().Set("Last-Modified", fi.ModTime.UTC().Format(http.TimeFormat))
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Length", strconv.FormatInt(fi.Size, 10))
|
|
||||||
|
|
||||||
if r.Method != http.MethodHead {
|
if r.Method != http.MethodHead {
|
||||||
io.Copy(w, f)
|
io.Copy(w, f)
|
||||||
}
|
}
|
||||||
@ -172,17 +166,16 @@ func (b *backend) propfindFile(propfind *internal.Propfind, fi *FileInfo) (*inte
|
|||||||
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) {
|
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
return &internal.GetContentLength{Length: fi.Size}, nil
|
return &internal.GetContentLength{Length: fi.Size}, nil
|
||||||
}
|
}
|
||||||
props[internal.GetContentTypeName] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
||||||
t := mime.TypeByExtension(path.Ext(fi.Href))
|
|
||||||
if t == "" {
|
|
||||||
// TODO: use http.DetectContentType
|
|
||||||
return nil, &internal.HTTPError{Code: http.StatusNotFound}
|
|
||||||
}
|
|
||||||
return &internal.GetContentType{Type: t}, nil
|
|
||||||
}
|
|
||||||
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) {
|
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime)}, nil
|
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if fi.MIMEType != "" {
|
||||||
|
props[internal.GetContentTypeName] = func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
|
return &internal.GetContentType{Type: fi.MIMEType}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: getetag
|
// TODO: getetag
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
webdav.go
11
webdav.go
@ -7,11 +7,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: add ETag, MIMEType to FileInfo
|
// TODO: add ETag to FileInfo
|
||||||
|
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
Href string
|
Href string
|
||||||
Size int64
|
Size int64
|
||||||
ModTime time.Time
|
ModTime time.Time
|
||||||
IsDir bool
|
IsDir bool
|
||||||
|
MIMEType string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user