-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pixeldrain.go
76 lines (62 loc) · 1.82 KB
/
pixeldrain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// pixeldrain.go
//
// Copyright (c) 2018-2023 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
package pixeldrain
import (
"net/url"
"strings"
"github.com/go-openapi/strfmt"
"github.com/jkawamoto/go-pixeldrain/client"
)
// Default is a default client.
var Default = New(nil, nil)
const (
// fileBasePath is the base path to file download URLs.
fileBasePath = "/file"
// listBasePath is the base path to list URLs.
listBasePath = "/l"
)
// New creates a new PixelDrain client with the given configurations. formats and cfg can be nil.
func New(formats strfmt.Registry, cfg *client.TransportConfig) *client.PixeldrainAPI {
cli := client.NewHTTPClientWithConfig(formats, cfg)
cli.SetTransport(ContentTypeFixer(cli.Transport))
return cli
}
// DownloadURL returns the URL associated with the given file ID.
func DownloadURL(id string) string {
u := url.URL{
Scheme: client.DefaultSchemes[0],
}
return u.JoinPath(client.DefaultHost, client.DefaultBasePath, fileBasePath, id).String()
}
// ListURL returns the URL associated with the given list ID.
func ListURL(id string) string {
u := url.URL{
Scheme: client.DefaultSchemes[0],
}
return u.JoinPath(client.DefaultHost, listBasePath, id).String()
}
// IsDownloadURL returns true if the given url points a file.
func IsDownloadURL(u string) (bool, error) {
parse, err := url.Parse(u)
if err != nil {
return false, err
}
prefix, err := url.JoinPath(client.DefaultBasePath, fileBasePath)
if err != nil {
return false, err
}
return strings.HasPrefix(parse.Path, prefix), nil
}
// IsListURL returns true if the given url points a list.
func IsListURL(u string) (bool, error) {
parse, err := url.Parse(u)
if err != nil {
return false, err
}
return strings.HasPrefix(parse.Path, listBasePath), nil
}