WebDAV: Mark uploaded files as favorite #1210

This commit is contained in:
Michael Mayer 2021-04-30 16:52:54 +02:00
parent f87c9c01da
commit 8851271d55
2 changed files with 41 additions and 1 deletions

View file

@ -238,7 +238,7 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName string) (
if err := photo.LoadFromYaml(yamlName); err != nil { if err := photo.LoadFromYaml(yamlName); err != nil {
log.Errorf("index: %s in %s (restore from yaml)", err.Error(), logName) log.Errorf("index: %s in %s (restore from yaml)", err.Error(), logName)
} else if err := photo.Find(); err != nil { } else if err := photo.Find(); err != nil {
log.Infof("index: restored from %s", txt.Quote(filepath.Base(yamlName))) log.Infof("index: %s restored from %s", txt.Quote(m.BaseName()), txt.Quote(filepath.Base(yamlName)))
} else { } else {
photoExists = true photoExists = true
log.Infof("index: uid %s restored from %s", photo.PhotoUID, txt.Quote(filepath.Base(yamlName))) log.Infof("index: uid %s restored from %s", photo.PhotoUID, txt.Quote(filepath.Base(yamlName)))

View file

@ -1,7 +1,12 @@
package server package server
import ( import (
"github.com/photoprism/photoprism/pkg/fs"
"io/ioutil"
"net/http" "net/http"
"os"
"path/filepath"
"strings"
"github.com/photoprism/photoprism/pkg/txt" "github.com/photoprism/photoprism/pkg/txt"
@ -14,6 +19,32 @@ import (
const WebDAVOriginals = "/originals" const WebDAVOriginals = "/originals"
const WebDAVImport = "/import" const WebDAVImport = "/import"
// MarkUploadAsFavorite sets the favorite flag for newly uploaded files.
func MarkUploadAsFavorite(fileName string) {
yamlName := fs.AbsPrefix(fileName, false) + fs.YamlExt
// Abort if YAML file already exists to avoid overwriting metadata.
if fs.FileExists(yamlName) {
log.Warnf("webdav: %s already exists", txt.Quote(filepath.Base(yamlName)))
return
}
// Make sure directory exists.
if err := os.MkdirAll(filepath.Dir(yamlName), os.ModePerm); err != nil {
log.Errorf("webdav: %s", err.Error())
return
}
// Write YAML data to file.
if err := ioutil.WriteFile(yamlName, []byte("Favorite: true\n"), os.ModePerm); err != nil {
log.Errorf("webdav: %s", err.Error())
return
}
// Log success.
log.Infof("webdav: marked %s as favorite", txt.Quote(filepath.Base(fileName)))
}
// ANY /webdav/* // ANY /webdav/*
func WebDAV(path string, router *gin.RouterGroup, conf *config.Config) { func WebDAV(path string, router *gin.RouterGroup, conf *config.Config) {
if router == nil { if router == nil {
@ -44,6 +75,15 @@ func WebDAV(path string, router *gin.RouterGroup, conf *config.Config) {
} }
} else { } else {
// Mark uploaded files as favorite if X-Favorite HTTP header is "1".
if r.Method == MethodPut && r.Header.Get("X-Favorite") == "1" {
if router.BasePath() == WebDAVOriginals {
MarkUploadAsFavorite(filepath.Join(conf.OriginalsPath(), strings.TrimPrefix(r.URL.Path, router.BasePath())))
} else if router.BasePath() == WebDAVImport {
MarkUploadAsFavorite(filepath.Join(conf.ImportPath(), strings.TrimPrefix(r.URL.Path, router.BasePath())))
}
}
switch r.Method { switch r.Method {
case MethodPut, MethodPost, MethodPatch, MethodDelete, MethodCopy, MethodMove: case MethodPut, MethodPost, MethodPatch, MethodDelete, MethodCopy, MethodMove:
log.Infof("webdav: %s %s", r.Method, r.URL) log.Infof("webdav: %s %s", r.Method, r.URL)