109 lines
2.3 KiB
Go
109 lines
2.3 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*
|
|
* This work is published and licensed under a Creative Commons
|
|
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
|
*
|
|
* Distribution of this work is permitted, but commercial use and
|
|
* modifications are strictly prohibited.
|
|
*/
|
|
package filecli
|
|
|
|
import (
|
|
"net/url"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
PathTypeIdentic = "identic"
|
|
PathTypePrefix = "prefix"
|
|
PathTypeRegexp = "regexp"
|
|
)
|
|
|
|
type Referer struct {
|
|
urlobj *url.URL
|
|
user, pass string
|
|
resource string
|
|
values url.Values
|
|
}
|
|
|
|
func ParsePath(rawpath string) (*Referer, error) {
|
|
repo := &Referer{
|
|
values: url.Values{},
|
|
}
|
|
if !strings.Contains(rawpath, "://") {
|
|
rawpath = "https://" + rawpath
|
|
}
|
|
urlobj, err := url.Parse(rawpath)
|
|
if err != nil {
|
|
return repo, err
|
|
}
|
|
if urlobj.User != nil {
|
|
repo.user = urlobj.User.Username()
|
|
repo.pass, _ = urlobj.User.Password()
|
|
urlobj.User = nil
|
|
}
|
|
repo.resource = path.Join("/", urlobj.Path)
|
|
urlobj.Path = "/"
|
|
repo.urlobj = urlobj
|
|
repo.values = urlobj.Query()
|
|
return repo, err
|
|
}
|
|
|
|
func (repo *Referer) Raw() string {
|
|
res := path.Join(repo.urlobj.Host, repo.resource)
|
|
query := repo.values.Encode()
|
|
if query != "" {
|
|
return res + "?" + query
|
|
}
|
|
return res
|
|
}
|
|
|
|
func (repo *Referer) SetResource(resource string) {
|
|
repo.resource = path.Join("/", resource)
|
|
}
|
|
|
|
func (repo *Referer) JoinResource(resource string) {
|
|
repo.resource = path.Join("/", repo.resource, resource)
|
|
}
|
|
|
|
func (repo *Referer) PathType(typ string) {
|
|
repo.values.Set("pathType", typ)
|
|
}
|
|
|
|
func (repo *Referer) DryRun(yesno bool) {
|
|
repo.values.Set("dryRun", strconv.FormatBool(yesno))
|
|
}
|
|
|
|
func (repo *Referer) FileEP() string {
|
|
curl := repo.urlobj.JoinPath("/v3/api/file/", repo.resource)
|
|
return curl.String()
|
|
}
|
|
|
|
func (repo *Referer) FilesEP() string {
|
|
curl := repo.urlobj.JoinPath("/v3/api/files/", repo.resource)
|
|
return curl.String()
|
|
}
|
|
|
|
func (repo *Referer) CollectionEP() string {
|
|
curl := repo.urlobj.JoinPath("/v3/api/collection/", repo.resource)
|
|
return curl.String()
|
|
}
|
|
|
|
func (repo *Referer) CollectionsEP() string {
|
|
curl := repo.urlobj.JoinPath("/v3/api/collections/", repo.resource)
|
|
return curl.String()
|
|
}
|
|
|
|
func (repo *Referer) Userinfo() (string, string) {
|
|
return repo.user, repo.pass
|
|
}
|
|
|
|
func (repo *Referer) SetUserinfo(user, pass string) {
|
|
if user != "" && pass != "" {
|
|
repo.user, repo.pass = user, pass
|
|
}
|
|
}
|