2023-11-27 21:44:38 +01:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2024-01-09 18:57:42 +01:00
|
|
|
"context"
|
2023-12-14 20:25:24 +01:00
|
|
|
"github.com/openziti/zrok/environment/env_core"
|
2024-01-09 18:57:42 +01:00
|
|
|
"github.com/openziti/zrok/util/sync/driveClient"
|
2023-11-27 23:31:13 +01:00
|
|
|
"io"
|
2024-01-09 18:57:42 +01:00
|
|
|
"net/http"
|
2023-12-14 20:25:24 +01:00
|
|
|
"net/url"
|
2023-11-27 23:31:13 +01:00
|
|
|
"os"
|
2023-11-28 03:11:19 +01:00
|
|
|
"time"
|
2023-11-27 21:44:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebDAVTargetConfig struct {
|
2023-12-14 20:25:24 +01:00
|
|
|
URL *url.URL
|
2023-11-27 21:44:38 +01:00
|
|
|
Username string
|
|
|
|
Password string
|
2023-12-14 20:25:24 +01:00
|
|
|
Root env_core.Root
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type WebDAVTarget struct {
|
2023-12-15 18:10:35 +01:00
|
|
|
cfg *WebDAVTargetConfig
|
2024-01-09 18:57:42 +01:00
|
|
|
dc *driveClient.Client
|
2023-12-15 18:10:35 +01:00
|
|
|
isDir bool
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebDAVTarget(cfg *WebDAVTargetConfig) (*WebDAVTarget, error) {
|
2024-01-09 18:57:42 +01:00
|
|
|
dc, err := driveClient.NewClient(http.DefaultClient, cfg.URL.String())
|
2023-12-14 20:25:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-09 18:57:42 +01:00
|
|
|
return &WebDAVTarget{cfg: cfg, dc: dc}, nil
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *WebDAVTarget) Inventory() ([]*Object, error) {
|
2024-01-09 18:57:42 +01:00
|
|
|
fis, err := t.dc.Readdir(context.Background(), "", true)
|
2023-11-27 21:44:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-09 18:57:42 +01:00
|
|
|
var objects []*Object
|
|
|
|
for _, fi := range fis {
|
2024-01-09 19:58:38 +01:00
|
|
|
if fi.Path != "/" {
|
2024-01-09 18:57:42 +01:00
|
|
|
objects = append(objects, &Object{
|
|
|
|
Path: fi.Path,
|
2024-01-09 19:58:38 +01:00
|
|
|
IsDir: fi.IsDir,
|
2024-01-09 18:57:42 +01:00
|
|
|
Size: fi.Size,
|
|
|
|
Modified: fi.ModTime,
|
|
|
|
ETag: fi.ETag,
|
|
|
|
})
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-09 18:57:42 +01:00
|
|
|
return objects, nil
|
2023-11-27 21:44:38 +01:00
|
|
|
}
|
2023-11-27 23:31:13 +01:00
|
|
|
|
2024-01-09 19:58:38 +01:00
|
|
|
func (t *WebDAVTarget) Mkdir(path string) error {
|
|
|
|
return t.dc.Mkdir(context.Background(), path)
|
|
|
|
}
|
|
|
|
|
2023-11-27 23:31:13 +01:00
|
|
|
func (t *WebDAVTarget) ReadStream(path string) (io.ReadCloser, error) {
|
2024-01-09 18:57:42 +01:00
|
|
|
return t.dc.Open(context.Background(), path)
|
2023-11-27 23:31:13 +01:00
|
|
|
}
|
2023-11-28 03:11:19 +01:00
|
|
|
|
2024-01-09 18:57:42 +01:00
|
|
|
func (t *WebDAVTarget) WriteStream(path string, rs io.Reader, _ os.FileMode) error {
|
|
|
|
ws, err := t.dc.Create(context.Background(), path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-09 19:58:38 +01:00
|
|
|
defer func() { _ = ws.Close() }()
|
2024-01-09 18:57:42 +01:00
|
|
|
_, err = io.Copy(ws, rs)
|
|
|
|
if err != nil {
|
2023-12-01 19:53:36 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-11-28 03:11:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-09 18:57:42 +01:00
|
|
|
|
|
|
|
func (t *WebDAVTarget) SetModificationTime(path string, mtime time.Time) error {
|
|
|
|
return t.dc.Touch(context.Background(), path, mtime)
|
|
|
|
}
|