2017-11-27 01:45:33 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-05-24 20:13:38 +00:00
|
|
|
"io"
|
2017-11-27 01:45:33 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2018-01-07 21:21:13 +00:00
|
|
|
"os"
|
2017-11-27 01:45:33 +00:00
|
|
|
"path"
|
2017-12-17 15:03:17 +00:00
|
|
|
"strings"
|
2017-11-27 01:45:33 +00:00
|
|
|
|
|
|
|
"github.com/studio-b12/gowebdav"
|
|
|
|
)
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// CloudImporter implements an Importer, where files to imports are located
|
|
|
|
// remotely, under a WebDAV server (such as sabre/dav, owncloud, ...).
|
2017-11-27 01:45:33 +00:00
|
|
|
type CloudImporter struct {
|
2018-05-11 23:08:37 +00:00
|
|
|
// baseDAV is the URL (most probably http or https one) to the root directory.
|
|
|
|
// It should contains all themes, in separated directories.
|
2017-11-27 01:45:33 +00:00
|
|
|
baseDAV url.URL
|
2018-05-11 23:08:37 +00:00
|
|
|
// username is the username used to perform authentication through BasicAuth.
|
2017-11-27 01:45:33 +00:00
|
|
|
username string
|
2018-05-11 23:08:37 +00:00
|
|
|
// password is the password used to perform authentication through BasicAuth.
|
2017-11-27 01:45:33 +00:00
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// NewCloudImporter registers a new object CloudImporter, as the URL conversion
|
|
|
|
// can returns errors.
|
2017-11-27 01:45:33 +00:00
|
|
|
func NewCloudImporter(baseDAV string, username string, password string) (*CloudImporter, error) {
|
|
|
|
if r, err := url.Parse(baseDAV); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return &CloudImporter{*r, username, password}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i CloudImporter) Kind() string {
|
|
|
|
return "cloud file importer: " + i.baseDAV.String()
|
|
|
|
}
|
|
|
|
|
2022-01-20 14:29:49 +00:00
|
|
|
func (i CloudImporter) Id() *string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:56:40 +00:00
|
|
|
func (i CloudImporter) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i CloudImporter) Sync() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:54:02 +00:00
|
|
|
func (i CloudImporter) Exists(filename string) bool {
|
2017-11-27 01:45:33 +00:00
|
|
|
fullURL := i.baseDAV
|
|
|
|
fullURL.Path = path.Join(fullURL.Path, filename)
|
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
if req, err := http.NewRequest("HEAD", fullURL.String(), nil); err == nil {
|
|
|
|
req.SetBasicAuth(i.username, i.password)
|
|
|
|
|
|
|
|
if resp, err := client.Do(req); err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return resp.StatusCode == http.StatusOK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i CloudImporter) toURL(filename string) string {
|
|
|
|
fullURL := i.baseDAV
|
|
|
|
fullURL.Path = path.Join(fullURL.Path, filename)
|
|
|
|
return fullURL.String()
|
|
|
|
}
|
|
|
|
|
2017-12-12 06:13:38 +00:00
|
|
|
func (i CloudImporter) importFile(URI string, next func(string, string) (interface{}, error)) (interface{}, error) {
|
|
|
|
return ImportFile(i, URI, next)
|
|
|
|
}
|
|
|
|
|
2023-11-25 16:13:31 +00:00
|
|
|
func (i CloudImporter) GetFile(filename string) (io.Reader, error) {
|
2017-11-27 01:45:33 +00:00
|
|
|
fullURL := i.baseDAV
|
|
|
|
fullURL.Path = path.Join(fullURL.Path, filename)
|
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
if req, err := http.NewRequest("GET", fullURL.String(), nil); err != nil {
|
2022-11-21 11:30:13 +00:00
|
|
|
return nil, err
|
2017-11-27 01:45:33 +00:00
|
|
|
} else {
|
|
|
|
req.SetBasicAuth(i.username, i.password)
|
|
|
|
if resp, err := client.Do(req); err != nil {
|
2022-11-21 11:30:13 +00:00
|
|
|
return nil, err
|
2017-11-27 01:45:33 +00:00
|
|
|
} else {
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2022-11-21 11:30:13 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
return nil, errors.New(resp.Status)
|
2017-11-27 01:45:33 +00:00
|
|
|
} else {
|
2022-11-21 11:30:13 +00:00
|
|
|
return resp.Body, nil
|
2017-11-27 01:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 20:13:38 +00:00
|
|
|
func (i CloudImporter) writeFile(filename string, reader io.Reader) error {
|
|
|
|
fullURL := i.baseDAV
|
|
|
|
fullURL.Path = path.Join(fullURL.Path, filename)
|
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
if req, err := http.NewRequest("PUT", fullURL.String(), reader); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
req.SetBasicAuth(i.username, i.password)
|
|
|
|
if resp, err := client.Do(req); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return errors.New(resp.Status)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-25 16:13:31 +00:00
|
|
|
func (i CloudImporter) ListDir(filename string) ([]string, error) {
|
2017-11-27 01:45:33 +00:00
|
|
|
client := gowebdav.NewClient(i.baseDAV.String(), i.username, i.password)
|
|
|
|
|
2017-12-17 15:03:17 +00:00
|
|
|
if files, err := client.ReadDir(strings.Replace(url.PathEscape(filename), "%2F", "/", -1)); err != nil {
|
2017-11-27 01:45:33 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
res := make([]string, 0)
|
|
|
|
for _, file := range files {
|
|
|
|
res = append(res, file.Name())
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 21:21:13 +00:00
|
|
|
|
2023-11-25 16:13:31 +00:00
|
|
|
func (i CloudImporter) Stat(filename string) (os.FileInfo, error) {
|
2018-01-07 21:21:13 +00:00
|
|
|
return gowebdav.NewClient(i.baseDAV.String(), i.username, i.password).Stat(strings.Replace(url.PathEscape(filename), "%2F", "/", -1))
|
|
|
|
}
|