2020-02-21 04:23:16 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-02-09 13:14:56 +01:00
|
|
|
"fmt"
|
2020-02-21 04:23:16 +01:00
|
|
|
"net/http"
|
2021-04-30 16:52:54 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-02-21 04:23:16 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-06-16 01:20:44 +02:00
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
|
2021-01-02 18:56:15 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/auto"
|
2020-02-21 04:23:16 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2022-06-16 01:20:44 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-02-21 04:23:16 +01:00
|
|
|
)
|
|
|
|
|
2021-01-02 18:56:15 +01:00
|
|
|
const WebDAVOriginals = "/originals"
|
|
|
|
const WebDAVImport = "/import"
|
|
|
|
|
2022-10-18 14:21:23 +02:00
|
|
|
// registerWebDAVRoutes configures the built-in WebDAV server.
|
|
|
|
func registerWebDAVRoutes(router *gin.Engine, conf *config.Config) {
|
|
|
|
if conf.DisableWebDAV() {
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Info("webdav: disabled")
|
2022-10-18 14:21:23 +02:00
|
|
|
} else {
|
2023-02-09 13:14:56 +01:00
|
|
|
var info string
|
|
|
|
if conf.ReadOnly() {
|
|
|
|
info = " in read-only mode"
|
|
|
|
} else {
|
|
|
|
info = ""
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:47:14 +01:00
|
|
|
WebDAV(conf.OriginalsPath(), router.Group(conf.BaseUri(WebDAVOriginals), BasicAuth(conf)), conf)
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Infof("webdav: shared %s/%s", conf.BaseUri(WebDAVOriginals), info)
|
2022-10-18 14:21:23 +02:00
|
|
|
|
|
|
|
if conf.ImportPath() != "" {
|
2023-03-14 21:47:14 +01:00
|
|
|
WebDAV(conf.ImportPath(), router.Group(conf.BaseUri(WebDAVImport), BasicAuth(conf)), conf)
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Infof("webdav: shared %s/%s", conf.BaseUri(WebDAVImport), info)
|
2022-10-18 14:21:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 23:34:43 +02:00
|
|
|
var WebDAVHandler = func(c *gin.Context, router *gin.RouterGroup, srv *webdav.Handler) {
|
|
|
|
srv.ServeHTTP(c.Writer, c.Request)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebDAV handles requests to the /originals and /import endpoints.
|
|
|
|
func WebDAV(filePath string, router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
if router == nil {
|
|
|
|
log.Error("webdav: router is nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf == nil {
|
|
|
|
log.Error("webdav: conf is nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Native file system restricted to a specific directory.
|
|
|
|
fileSystem := webdav.Dir(filePath)
|
|
|
|
|
|
|
|
// Request logger function.
|
|
|
|
loggerFunc := func(r *http.Request, err error) {
|
|
|
|
if err != nil {
|
|
|
|
switch r.Method {
|
|
|
|
case MethodPut, MethodPost, MethodPatch, MethodDelete, MethodCopy, MethodMove:
|
2023-08-15 08:12:52 +02:00
|
|
|
log.Errorf("webdav: %s in %s %s", clean.LogError(err), clean.Log(r.Method), clean.Log(r.URL.String()))
|
2022-10-08 23:34:43 +02:00
|
|
|
case MethodPropfind:
|
2023-08-15 08:12:52 +02:00
|
|
|
log.Tracef("webdav: %s in %s %s", clean.LogError(err), clean.Log(r.Method), clean.Log(r.URL.String()))
|
2022-10-08 23:34:43 +02:00
|
|
|
default:
|
2023-08-15 08:12:52 +02:00
|
|
|
log.Debugf("webdav: %s in %s %s", clean.LogError(err), clean.Log(r.Method), clean.Log(r.URL.String()))
|
2022-10-08 23:34:43 +02:00
|
|
|
}
|
|
|
|
} 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() == conf.BaseUri(WebDAVOriginals) {
|
|
|
|
MarkUploadAsFavorite(filepath.Join(conf.OriginalsPath(), strings.TrimPrefix(r.URL.Path, router.BasePath())))
|
|
|
|
} else if router.BasePath() == conf.BaseUri(WebDAVImport) {
|
|
|
|
MarkUploadAsFavorite(filepath.Join(conf.ImportPath(), strings.TrimPrefix(r.URL.Path, router.BasePath())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case MethodPut, MethodPost, MethodPatch, MethodDelete, MethodCopy, MethodMove:
|
|
|
|
log.Infof("webdav: %s %s", clean.Log(r.Method), clean.Log(r.URL.String()))
|
|
|
|
|
|
|
|
if router.BasePath() == conf.BaseUri(WebDAVOriginals) {
|
|
|
|
auto.ShouldIndex()
|
|
|
|
} else if router.BasePath() == conf.BaseUri(WebDAVImport) {
|
|
|
|
auto.ShouldImport()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Tracef("webdav: %s %s", clean.Log(r.Method), clean.Log(r.URL.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebDAV request handler.
|
|
|
|
srv := &webdav.Handler{
|
|
|
|
Prefix: router.BasePath(),
|
|
|
|
FileSystem: fileSystem,
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
Logger: loggerFunc,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request handler wrapper function.
|
|
|
|
handlerFunc := func(c *gin.Context) {
|
|
|
|
WebDAVHandler(c, router, srv)
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:14:56 +01:00
|
|
|
// handleRead registers WebDAV methods used for browsing and downloading.
|
|
|
|
handleRead := func(h func(*gin.Context)) {
|
|
|
|
router.Handle(MethodHead, "/*path", h)
|
|
|
|
router.Handle(MethodGet, "/*path", h)
|
|
|
|
router.Handle(MethodOptions, "/*path", h)
|
|
|
|
router.Handle(MethodLock, "/*path", h)
|
|
|
|
router.Handle(MethodUnlock, "/*path", h)
|
|
|
|
router.Handle(MethodPropfind, "/*path", h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleWrite registers WebDAV methods to may modify the file system.
|
|
|
|
handleWrite := func(h func(*gin.Context)) {
|
|
|
|
router.Handle(MethodPut, "/*path", h)
|
|
|
|
router.Handle(MethodPost, "/*path", h)
|
|
|
|
router.Handle(MethodPatch, "/*path", h)
|
|
|
|
router.Handle(MethodDelete, "/*path", h)
|
|
|
|
router.Handle(MethodMkcol, "/*path", h)
|
|
|
|
router.Handle(MethodCopy, "/*path", h)
|
|
|
|
router.Handle(MethodMove, "/*path", h)
|
|
|
|
router.Handle(MethodProppatch, "/*path", h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle supported WebDAV request methods.
|
|
|
|
handleRead(handlerFunc)
|
|
|
|
|
|
|
|
// Only supported with read-only mode disabled.
|
|
|
|
if conf.ReadOnly() {
|
|
|
|
handleWrite(func(c *gin.Context) {
|
|
|
|
_ = c.AbortWithError(http.StatusForbidden, fmt.Errorf("forbidden in read-only mode"))
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
handleWrite(handlerFunc)
|
|
|
|
}
|
2022-10-08 23:34:43 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 16:52:54 +02:00
|
|
|
// MarkUploadAsFavorite sets the favorite flag for newly uploaded files.
|
|
|
|
func MarkUploadAsFavorite(fileName string) {
|
2022-04-15 09:42:07 +02:00
|
|
|
yamlName := fs.AbsPrefix(fileName, false) + fs.ExtYAML
|
2021-04-30 16:52:54 +02:00
|
|
|
|
|
|
|
// Abort if YAML file already exists to avoid overwriting metadata.
|
|
|
|
if fs.FileExists(yamlName) {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Warnf("webdav: %s already exists", clean.Log(filepath.Base(yamlName)))
|
2021-04-30 16:52:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure directory exists.
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(filepath.Dir(yamlName), fs.ModeDir); err != nil {
|
2021-04-30 16:52:54 +02:00
|
|
|
log.Errorf("webdav: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write YAML data to file.
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.WriteFile(yamlName, []byte("Favorite: true\n"), fs.ModeFile); err != nil {
|
2021-04-30 16:52:54 +02:00
|
|
|
log.Errorf("webdav: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log success.
|
2023-03-08 23:30:39 +01:00
|
|
|
log.Infof("webdav: flagged %s as favorite", clean.Log(filepath.Base(fileName)))
|
2021-04-30 16:52:54 +02:00
|
|
|
}
|