121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package photos
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewClient(baseURL string) *Client {
|
|
return &Client{
|
|
baseURL: baseURL,
|
|
httpClient: &http.Client{
|
|
Timeout: 60 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
type Asset struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
OriginalPath string `json:"originalPath"`
|
|
OriginalName string `json:"originalFileName"`
|
|
MimeType string `json:"originalMimeType"`
|
|
FileSize int64 `json:"exifInfo.fileSizeInByte"`
|
|
CreatedAt string `json:"fileCreatedAt"`
|
|
IsFavorite bool `json:"isFavorite"`
|
|
ThumbHash string `json:"thumbhash"`
|
|
}
|
|
|
|
type Album struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"albumName"`
|
|
AssetCount int `json:"assetCount"`
|
|
CreatedAt string `json:"createdAt"`
|
|
}
|
|
|
|
func (c *Client) GetAssets(ctx context.Context, apiKey string, page, size int) ([]Asset, error) {
|
|
url := fmt.Sprintf("%s/assets?page=%d&size=%d", c.baseURL, page, size)
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
req.Header.Set("x-api-key", apiKey)
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var assets []Asset
|
|
json.NewDecoder(resp.Body).Decode(&assets)
|
|
return assets, nil
|
|
}
|
|
|
|
func (c *Client) GetAlbums(ctx context.Context, apiKey string) ([]Album, error) {
|
|
url := fmt.Sprintf("%s/albums", c.baseURL)
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
req.Header.Set("x-api-key", apiKey)
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var albums []Album
|
|
json.NewDecoder(resp.Body).Decode(&albums)
|
|
return albums, nil
|
|
}
|
|
|
|
func (c *Client) UploadAsset(ctx context.Context, apiKey string, body io.Reader, contentType string) (string, error) {
|
|
url := fmt.Sprintf("%s/assets", c.baseURL)
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", url, body)
|
|
req.Header.Set("x-api-key", apiKey)
|
|
req.Header.Set("Content-Type", contentType)
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result struct {
|
|
ID string `json:"id"`
|
|
}
|
|
json.NewDecoder(resp.Body).Decode(&result)
|
|
return result.ID, nil
|
|
}
|
|
|
|
func (c *Client) GetAssetThumbnail(ctx context.Context, apiKey, assetID string) (io.ReadCloser, string, error) {
|
|
url := fmt.Sprintf("%s/assets/%s/thumbnail", c.baseURL, assetID)
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
req.Header.Set("x-api-key", apiKey)
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
return resp.Body, resp.Header.Get("Content-Type"), nil
|
|
}
|
|
|
|
func (c *Client) DeleteAsset(ctx context.Context, apiKey, assetID string) error {
|
|
url := fmt.Sprintf("%s/assets/%s", c.baseURL, assetID)
|
|
req, _ := http.NewRequestWithContext(ctx, "DELETE", url, nil)
|
|
req.Header.Set("x-api-key", apiKey)
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp.Body.Close()
|
|
return nil
|
|
}
|