mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
we need a own implementation :/
This commit is contained in:
parent
c97468dd70
commit
ef4d64b0a8
2 changed files with 23 additions and 14 deletions
|
@ -4,7 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
|
@ -16,7 +16,7 @@ const giteaAPIRepos = "/api/v1/repos/"
|
||||||
var ErrorNotFound = errors.New("not found")
|
var ErrorNotFound = errors.New("not found")
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
giteaRoot *url.URL
|
giteaRoot string
|
||||||
giteaAPIToken string
|
giteaAPIToken string
|
||||||
fastClient *fasthttp.Client
|
fastClient *fasthttp.Client
|
||||||
infoTimeout time.Duration
|
infoTimeout time.Duration
|
||||||
|
@ -29,18 +29,27 @@ type FileResponse struct {
|
||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func joinURL(baseURL *url.URL, paths ...string) string {
|
func joinURL(baseURL string, paths ...string) string {
|
||||||
b := *baseURL
|
p := make([]string, 0, len(paths))
|
||||||
b.Path = path.Join(append([]string{b.Path}, paths...)...)
|
for i := range paths {
|
||||||
return b.String()
|
path := strings.TrimSpace(paths[i])
|
||||||
|
path = strings.Trim(path, "/")
|
||||||
|
if len(path) != 0 {
|
||||||
|
p = append(p, path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return baseURL + "/" + strings.Join(p, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 }
|
func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 }
|
||||||
|
|
||||||
func NewClient(giteaRoot, giteaAPIToken string) (*Client, error) {
|
func NewClient(giteaRoot, giteaAPIToken string) (*Client, error) {
|
||||||
rootURL, err := url.Parse(giteaRoot)
|
rootURL, err := url.Parse(giteaRoot)
|
||||||
|
giteaRoot = strings.Trim(rootURL.String(), "/")
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
giteaRoot: rootURL,
|
giteaRoot: giteaRoot,
|
||||||
giteaAPIToken: giteaAPIToken,
|
giteaAPIToken: giteaAPIToken,
|
||||||
infoTimeout: 5 * time.Second,
|
infoTimeout: 5 * time.Second,
|
||||||
contentTimeout: 10 * time.Second,
|
contentTimeout: 10 * time.Second,
|
||||||
|
|
|
@ -8,16 +8,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJoinURL(t *testing.T) {
|
func TestJoinURL(t *testing.T) {
|
||||||
baseURL, _ := url.Parse("")
|
baseURL := ""
|
||||||
assert.EqualValues(t, "", joinURL(baseURL))
|
assert.EqualValues(t, "/", joinURL(baseURL))
|
||||||
assert.EqualValues(t, "", joinURL(baseURL, "", ""))
|
assert.EqualValues(t, "/", joinURL(baseURL, "", ""))
|
||||||
|
|
||||||
baseURL, _ = url.Parse("http://wwow.url.com")
|
baseURL = "http://wwow.url.com"
|
||||||
assert.EqualValues(t, "http://wwow.url.com/a/b/c/d", joinURL(baseURL, "a", "b/c/", "d"))
|
assert.EqualValues(t, "http://wwow.url.com/a/b/c/d", joinURL(baseURL, "a", "b/c/", "d"))
|
||||||
|
|
||||||
baseURL, _ = url.Parse("http://wow.url.com/subpath/2/")
|
baseURL = "http://wow.url.com/subpath/2"
|
||||||
assert.EqualValues(t, "http://wow.url.com/subpath/2/content.pdf", joinURL(baseURL, "/content.pdf"))
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/content.pdf", joinURL(baseURL, "/content.pdf"))
|
||||||
assert.EqualValues(t, "http://wow.url.com/subpath/2/wonderful.jpg", joinURL(baseURL, "wonderful.jpg"))
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/wonderful.jpg", joinURL(baseURL, "wonderful.jpg"))
|
||||||
assert.EqualValues(t, "http://wow.url.com/subpath/2/wonderful.jpg?ref=main", joinURL(baseURL, "raw", "wonderful.jpg"+"?ref="+url.QueryEscape("main")))
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/raw/wonderful.jpg?ref=main", joinURL(baseURL, "raw", "wonderful.jpg"+"?ref="+url.QueryEscape("main")))
|
||||||
assert.EqualValues(t, "http://wow.url.com/subpath/2/wonderful.jpg%3Fref=main", joinURL(baseURL, "raw", "wonderful.jpg%3Fref=main"))
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/raw/wonderful.jpg%3Fref=main", joinURL(baseURL, "raw", "wonderful.jpg%3Fref=main"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue