2023-04-26 16:51:13 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-04-28 15:57:09 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-04-26 16:51:13 -04:00
|
|
|
"fmt"
|
2023-04-28 16:52:16 -04:00
|
|
|
"io"
|
2023-04-28 15:57:09 -04:00
|
|
|
"mime/multipart"
|
2023-04-26 16:51:13 -04:00
|
|
|
"net/http"
|
2023-07-14 20:43:40 -04:00
|
|
|
"net/url"
|
2023-07-14 20:14:15 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
"codeberg.org/vlbeaudoin/voki"
|
2023-07-14 20:14:15 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/config"
|
2023-08-19 15:49:57 -04:00
|
|
|
"git.agecem.com/agecem/agecem-org/models"
|
2023-07-14 20:14:15 -04:00
|
|
|
"github.com/spf13/viper"
|
2023-04-26 16:51:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
2023-10-24 17:00:49 -04:00
|
|
|
Voki *voki.Voki
|
2023-04-26 16:51:13 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
// NewFromViper returns a pointer to a new API object,
|
2023-08-19 14:45:07 -04:00
|
|
|
// provided the configuration options are managed by
|
|
|
|
// https://git.agecem.com/agecem/agecem-org/config
|
2023-10-24 17:00:49 -04:00
|
|
|
func NewFromViper(client *http.Client) (api *API, err error) {
|
2023-07-14 20:14:15 -04:00
|
|
|
var config config.Config
|
2023-10-24 17:00:49 -04:00
|
|
|
if err = viper.Unmarshal(&config); err != nil {
|
2023-07-14 20:14:15 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
return New(client, config.Server.Api.Host, config.Server.Api.Key, config.Server.Api.Port, config.Server.Api.Protocol)
|
2023-07-14 20:14:15 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
func New(client *http.Client, host, key string, port int, protocol string) (*API, error) {
|
|
|
|
return &API{Voki: voki.New(client, host, key, port, protocol)}, nil
|
2023-04-26 16:51:13 -04:00
|
|
|
}
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-08-23 16:01:57 -04:00
|
|
|
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.V1DocumentCreateResponse, error) {
|
|
|
|
var response models.V1DocumentCreateResponse
|
2023-04-28 15:57:09 -04:00
|
|
|
endpoint := fmt.Sprintf("%s://%s:%d",
|
2023-10-24 17:00:49 -04:00
|
|
|
a.Voki.Protocol,
|
|
|
|
a.Voki.Host,
|
|
|
|
a.Voki.Port,
|
2023-04-28 15:57:09 -04:00
|
|
|
)
|
|
|
|
|
2023-07-14 20:43:40 -04:00
|
|
|
current_url := fmt.Sprintf("%s/v1/bucket/%s", endpoint, bucket)
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
// Create a new multipart writer
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(body)
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
// Add the file to the request
|
|
|
|
file, err := file_header.Open()
|
2023-04-28 15:57:09 -04:00
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
2023-04-28 16:52:16 -04:00
|
|
|
defer file.Close()
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-07-14 20:43:40 -04:00
|
|
|
filename_processed, err := url.QueryUnescape(file_header.Filename)
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err)
|
2023-07-14 20:43:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
part, err := writer.CreateFormFile("document", filename_processed)
|
2023-04-28 16:52:16 -04:00
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
_, err = io.Copy(part, file)
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#io.Copy: %s", err)
|
2023-04-28 16:52:16 -04:00
|
|
|
}
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#writer.Close: %s", err)
|
2023-04-28 16:52:16 -04:00
|
|
|
}
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
// Create a new HTTP request with the multipart body
|
2023-07-14 20:43:40 -04:00
|
|
|
req, err := http.NewRequest(http.MethodPost, current_url, body)
|
2023-04-28 15:57:09 -04:00
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:52:16 -04:00
|
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
if a.Voki.Key != "" {
|
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.Voki.Key))
|
2023-04-28 16:52:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the HTTP request
|
|
|
|
client := &http.Client{}
|
2023-04-28 15:57:09 -04:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2023-08-19 15:35:56 -04:00
|
|
|
return response, fmt.Errorf("UploadDocument#client.Do: %s", err)
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
2023-04-28 16:52:16 -04:00
|
|
|
defer resp.Body.Close()
|
2023-04-28 15:57:09 -04:00
|
|
|
|
2023-08-19 15:35:56 -04:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
return response, err
|
2023-04-28 15:57:09 -04:00
|
|
|
}
|
|
|
|
|
2023-10-24 17:00:49 -04:00
|
|
|
func (a *API) ListBuckets() (response models.V1BucketListResponse, err error) {
|
|
|
|
return response, a.Voki.Unmarshal(http.MethodGet, "/v1/bucket", nil, true, &response)
|
2023-08-20 17:57:36 -04:00
|
|
|
}
|