parent
66963053f5
commit
b0629be093
|
@ -1,9 +1,12 @@
|
||||||
package gokapi
|
package gokapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"code.jhot.me/jhot/hats/internal/util"
|
"code.jhot.me/jhot/hats/internal/util"
|
||||||
"github.com/gabriel-vasile/mimetype"
|
"github.com/gabriel-vasile/mimetype"
|
||||||
|
@ -64,12 +67,36 @@ type UploadOptions struct {
|
||||||
FilenameOverride string
|
FilenameOverride string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
req := c.restClient.R().
|
||||||
|
SetFileReader("file", opts.FilenameOverride, bytes.NewReader(data)).
|
||||||
|
SetMultipartFormData(map[string]string{
|
||||||
|
"allowedDownloads": fmt.Sprintf("%d", opts.AllowedDownloads),
|
||||||
|
"expiryDays": fmt.Sprintf("%d", opts.ExpiryDays),
|
||||||
|
"password": opts.Password,
|
||||||
|
})
|
||||||
|
|
||||||
|
var returnData struct {
|
||||||
|
Result string `json:"Result"`
|
||||||
|
FileInfo GokapiFile `json:"FileInfo"`
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := util.CheckSuccess(req.SetResult(&returnData).Post("files/add"))
|
||||||
|
if err != nil {
|
||||||
|
return GokapiFile{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnData.FileInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *GokapiClient) UploadFile(filePath string, opts *UploadOptions) (GokapiFile, error) {
|
func (c *GokapiClient) UploadFile(filePath string, opts *UploadOptions) (GokapiFile, error) {
|
||||||
var fileName string
|
if opts.FilenameOverride == "" {
|
||||||
if opts.FilenameOverride != "" {
|
opts.FilenameOverride = filepath.Base(filePath)
|
||||||
fileName = opts.FilenameOverride
|
|
||||||
} else {
|
|
||||||
fileName = filepath.Base(filePath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.Open(filePath)
|
f, err := os.Open(filePath)
|
||||||
|
@ -78,27 +105,11 @@ func (c *GokapiClient) UploadFile(filePath string, opts *UploadOptions) (GokapiF
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
mimeType, _ := mimetype.DetectFile(filePath)
|
data, err := os.ReadFile(filePath)
|
||||||
|
|
||||||
req := c.restClient.R().
|
|
||||||
SetMultipartField("file", fileName, mimeType.String(), f).
|
|
||||||
SetMultipartFormData(map[string]string{
|
|
||||||
"allowedDownloads": fmt.Sprintf("%d", opts.AllowedDownloads),
|
|
||||||
"expiryDays": fmt.Sprintf("%d", opts.ExpiryDays),
|
|
||||||
"password": opts.Password,
|
|
||||||
})
|
|
||||||
|
|
||||||
var data struct {
|
|
||||||
Result string `json:"Result"`
|
|
||||||
FileInfo GokapiFile `json:"FileInfo"`
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = util.CheckSuccess(req.SetResult(&data).Post("files/add"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return GokapiFile{}, err
|
return GokapiFile{}, fmt.Errorf("error reading file: %w", err)
|
||||||
}
|
}
|
||||||
|
return c.UploadBytes(data, opts)
|
||||||
return data.FileInfo, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GokapiClient) GetDownloadUrl(f GokapiFile) string {
|
func (c *GokapiClient) GetDownloadUrl(f GokapiFile) string {
|
||||||
|
|
Loading…
Reference in New Issue