admin: Implement sychronization backends
We are now able, depending on configuration, to retrieve files from either WebDAV or local file system.
This commit is contained in:
parent
6237f7755a
commit
8f7de926d3
7 changed files with 281 additions and 160 deletions
96
admin/sync/importer_cloud.go
Normal file
96
admin/sync/importer_cloud.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/studio-b12/gowebdav"
|
||||
)
|
||||
|
||||
var CloudDAVBase string = "https://srs.epita.fr/owncloud/remote.php/webdav/FIC 2018"
|
||||
var CloudUsername string = "fic"
|
||||
var CloudPassword string = ""
|
||||
|
||||
type CloudImporter struct {
|
||||
baseDAV url.URL
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
func (i CloudImporter) exists(filename string) bool {
|
||||
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()
|
||||
}
|
||||
|
||||
func (i CloudImporter) getFile(filename string, writer *bufio.Writer) error {
|
||||
fullURL := i.baseDAV
|
||||
fullURL.Path = path.Join(fullURL.Path, filename)
|
||||
|
||||
client := http.Client{}
|
||||
if req, err := http.NewRequest("GET", fullURL.String(), nil); 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 {
|
||||
reader := bufio.NewReader(resp.Body)
|
||||
reader.WriteTo(writer)
|
||||
writer.Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i CloudImporter) listDir(filename string) ([]string, error) {
|
||||
client := gowebdav.NewClient(i.baseDAV.String(), i.username, i.password)
|
||||
|
||||
if files, err := client.ReadDir(filename); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
res := make([]string, 0)
|
||||
for _, file := range files {
|
||||
res = append(res, file.Name())
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
Reference in a new issue