mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 08:14:15 +00:00
webdav: add support for PUT to server
This commit is contained in:
parent
45774fe572
commit
7d6de88179
@ -260,3 +260,7 @@ func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *Address
|
||||
|
||||
return internal.NewPropfindResponse(ao.Href, propfind, props)
|
||||
}
|
||||
|
||||
func (b *backend) Put(r *http.Request) error {
|
||||
panic("TODO")
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package webdav
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
@ -52,4 +53,12 @@ func (fs LocalFileSystem) Readdir(name string) ([]os.FileInfo, error) {
|
||||
return f.Readdir(-1)
|
||||
}
|
||||
|
||||
func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) {
|
||||
p, err := fs.path(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return os.Create(p)
|
||||
}
|
||||
|
||||
var _ FileSystem = LocalFileSystem("")
|
||||
|
@ -76,6 +76,7 @@ func ServeMultistatus(w http.ResponseWriter, ms *Multistatus) error {
|
||||
type Backend interface {
|
||||
Options(r *http.Request) ([]string, error)
|
||||
HeadGet(w http.ResponseWriter, r *http.Request) error
|
||||
Put(r *http.Request) error
|
||||
Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error)
|
||||
}
|
||||
|
||||
@ -93,6 +94,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
err = h.handleOptions(w, r)
|
||||
case http.MethodGet, http.MethodHead:
|
||||
err = h.Backend.HeadGet(w, r)
|
||||
case http.MethodPut:
|
||||
err = h.Backend.Put(r)
|
||||
if err == nil {
|
||||
// TODO: Last-Modified, ETag, Content-Type if the request has
|
||||
// been copied verbatim
|
||||
// TODO: Location if the server has mutated the href
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
case "PROPFIND":
|
||||
err = h.handlePropfind(w, r)
|
||||
default:
|
||||
|
15
server.go
15
server.go
@ -23,6 +23,7 @@ type FileSystem interface {
|
||||
Open(name string) (File, error)
|
||||
Stat(name string) (os.FileInfo, error)
|
||||
Readdir(name string) ([]os.FileInfo, error)
|
||||
Create(name string) (io.WriteCloser, error)
|
||||
}
|
||||
|
||||
// Handler handles WebDAV HTTP requests. It can be used to create a WebDAV
|
||||
@ -83,6 +84,20 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *backend) Put(r *http.Request) error {
|
||||
wc, err := b.FileSystem.Create(r.URL.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer wc.Close()
|
||||
|
||||
if _, err := io.Copy(wc, r.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wc.Close()
|
||||
}
|
||||
|
||||
func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
|
||||
fi, err := b.FileSystem.Stat(r.URL.Path)
|
||||
if os.IsNotExist(err) {
|
||||
|
Loading…
Reference in New Issue
Block a user