1
0
Fork 0
hats/pkg/gokapi/api.go

118 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-11-10 23:22:46 +00:00
package gokapi
import (
2023-12-14 16:57:43 +00:00
"bytes"
"encoding/base64"
2023-11-10 23:22:46 +00:00
"fmt"
"os"
"path/filepath"
2023-12-14 16:57:43 +00:00
"strings"
2023-11-10 23:22:46 +00:00
"code.jhot.me/jhot/hats/internal/util"
"github.com/gabriel-vasile/mimetype"
"github.com/go-resty/resty/v2"
)
type GokapiClient struct {
host string
restClient *resty.Client
}
func New(host string, token string) *GokapiClient {
return &GokapiClient{
host: host,
restClient: resty.New().
SetHeader("apikey", token).
SetBaseURL(fmt.Sprintf("%s/api", host)),
}
}
func NewFromEnv() *GokapiClient {
return New(os.Getenv("GOKAPI_HOST"), os.Getenv("GOKAPI_TOKEN"))
}
type GokapiFile struct {
ID string `json:"Id"`
Name string `json:"Name"`
Size string `json:"Size"`
HotlinkId string `json:"HotlinkId"`
ContentType string `json:"ContentType"`
ExpireAt int64 `json:"ExpireAt"`
SizeBytes int64 `json:"SizeBytes"`
ExpireAtString string `json:"ExpireAtString"`
DownloadsRemaining int `json:"DownloadsRemaining"`
DownloadCount int `json:"DownloadCount"`
UnlimitedDownloads bool `json:"UnlimitedDownloads"`
UnlimitedTime bool `json:"UnlimitedTime"`
RequiresClientSideDecryption bool `json:"RequiresClientSideDecryption"`
IsEncrypted bool `json:"IsEncrypted"`
IsPasswordProtected bool `json:"IsPasswordProtected"`
IsSavedOnLocalStorage bool `json:"IsSavedOnLocalStorage"`
}
func (c *GokapiClient) ListFiles() ([]GokapiFile, error) {
var data []GokapiFile
_, err := util.CheckSuccess(c.restClient.R().SetHeader("Accept", "application/json").SetResult(&data).Get("files/list"))
if err != nil {
return data, err
}
return data, nil
}
type UploadOptions struct {
AllowedDownloads int
ExpiryDays int
Password string
FilenameOverride string
}
2023-12-14 16:57:43 +00:00
func (c *GokapiClient) UploadBytes(data []byte, opts *UploadOptions) (GokapiFile, error) {
mimeType := mimetype.Detect(data)
if opts.FilenameOverride == "" {
opts.FilenameOverride = strings.TrimRight(base64.StdEncoding.EncodeToString(data), "=") + mimeType.Extension()
2023-11-10 23:22:46 +00:00
}
req := c.restClient.R().
2023-12-14 16:57:43 +00:00
SetFileReader("file", opts.FilenameOverride, bytes.NewReader(data)).
2023-11-10 23:22:46 +00:00
SetMultipartFormData(map[string]string{
"allowedDownloads": fmt.Sprintf("%d", opts.AllowedDownloads),
"expiryDays": fmt.Sprintf("%d", opts.ExpiryDays),
"password": opts.Password,
})
2023-12-14 16:57:43 +00:00
var returnData struct {
2023-11-10 23:22:46 +00:00
Result string `json:"Result"`
FileInfo GokapiFile `json:"FileInfo"`
}
2023-12-14 16:57:43 +00:00
_, err := util.CheckSuccess(req.SetResult(&returnData).Post("files/add"))
2023-11-10 23:22:46 +00:00
if err != nil {
return GokapiFile{}, err
}
2023-12-14 16:57:43 +00:00
return returnData.FileInfo, nil
}
func (c *GokapiClient) UploadFile(filePath string, opts *UploadOptions) (GokapiFile, error) {
if opts.FilenameOverride == "" {
opts.FilenameOverride = filepath.Base(filePath)
}
f, err := os.Open(filePath)
if err != nil {
return GokapiFile{}, err
}
defer f.Close()
data, err := os.ReadFile(filePath)
if err != nil {
return GokapiFile{}, fmt.Errorf("error reading file: %w", err)
}
return c.UploadBytes(data, opts)
2023-11-10 23:22:46 +00:00
}
func (c *GokapiClient) GetDownloadUrl(f GokapiFile) string {
return fmt.Sprintf("%s/downloadFile?id=%s", c.host, f.ID)
}