first round of modifications to the core webdav stack to support custom dead properties (#438)

This commit is contained in:
Michael Quigley 2023-12-01 11:44:20 -05:00
parent 9d80e1cf66
commit 837d2289f2
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -6,13 +6,18 @@ package webdav
import ( import (
"context" "context"
"crypto/md5"
"encoding/xml" "encoding/xml"
"fmt"
"github.com/sirupsen/logrus"
"io" "io"
"io/fs"
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -55,6 +60,63 @@ type File interface {
io.Writer io.Writer
} }
type webdavFile struct {
File
name string
}
func (f *webdavFile) DeadProps() (map[xml.Name]Property, error) {
logrus.Infof("DeadProps(%v)", f.name)
var (
xmlName xml.Name
property Property
properties = make(map[xml.Name]Property)
checksum, err = f.md5()
)
if err == nil {
xmlName.Space = "http://owncloud.org/ns"
xmlName.Local = "checksums"
property.XMLName = xmlName
property.InnerXML = append(property.InnerXML, "<checksum xmlns=\"http://owncloud.org/ns\">"...)
property.InnerXML = append(property.InnerXML, checksum...)
property.InnerXML = append(property.InnerXML, "</checksum>"...)
properties[xmlName] = property
}
var stat fs.FileInfo
stat, err = f.Stat()
if err == nil {
xmlName.Space = "DAV:"
xmlName.Local = "lastmodified"
property.XMLName = xmlName
property.InnerXML = strconv.AppendInt(nil, stat.ModTime().Unix(), 10)
properties[xmlName] = property
}
return properties, nil
}
func (f *webdavFile) Patch(proppatches []Proppatch) ([]Propstat, error) {
var stat Propstat
stat.Status = http.StatusOK
return []Propstat{stat}, nil
}
func (f *webdavFile) md5() (string, error) {
file, err := os.Open(f.name)
if err != nil {
return "", err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
xhash := fmt.Sprintf("%x", hash.Sum(nil))
logrus.Infof("hashed %v = %v", f.name, xhash)
return xhash, nil
}
// A Dir implements FileSystem using the native file system restricted to a // A Dir implements FileSystem using the native file system restricted to a
// specific directory tree. // specific directory tree.
// //
@ -93,7 +155,7 @@ func (d Dir) OpenFile(ctx context.Context, name string, flag int, perm os.FileMo
if err != nil { if err != nil {
return nil, err return nil, err
} }
return f, nil return &webdavFile{f, name}, nil
} }
func (d Dir) RemoveAll(ctx context.Context, name string) error { func (d Dir) RemoveAll(ctx context.Context, name string) error {