Files
photocli/internal/photos/bridge.go
T

65 lines
1.8 KiB
Go

package photos
import (
"encoding/json"
"fmt"
)
type Bridge interface {
RequestAccess() error
ListAlbums() ([]Album, error)
ListAssets(albumID string) ([]Asset, int, error)
ListTree() ([]CollectionNode, error)
ExportPreview(assetID, outputDir string, targetSize, index int) (ExportResult, error)
ExportOriginal(assetID, outputDir string, index int) (ExportResult, error)
ExportPreviewWithSlot(assetID, outputDir string, targetSize, index, slotIndex int) (ExportResult, error)
ExportOriginalWithSlot(assetID, outputDir string, index, slotIndex int) (ExportResult, error)
Cancel()
IsCancelled() bool
}
func ParseAlbumsJSON(jsonStr string) ([]Album, error) {
var resp AlbumsResponse
if err := json.Unmarshal([]byte(jsonStr), &resp); err != nil {
return nil, err
}
return resp.Albums, nil
}
func ParseAssetsJSON(jsonStr string) ([]Asset, int, error) {
var errResp ErrorResponse
if err := json.Unmarshal([]byte(jsonStr), &errResp); err == nil && errResp.Error != "" {
return nil, 0, fmt.Errorf("%s", errResp.Error)
}
var resp AssetsResponse
if err := json.Unmarshal([]byte(jsonStr), &resp); err != nil {
return nil, 0, err
}
return resp.Assets, resp.Total, nil
}
func ParseTreeJSON(jsonStr string) ([]CollectionNode, error) {
var errResp ErrorResponse
if err := json.Unmarshal([]byte(jsonStr), &errResp); err == nil && errResp.Error != "" {
return nil, fmt.Errorf("%s", errResp.Error)
}
var resp TreeResponse
if err := json.Unmarshal([]byte(jsonStr), &resp); err != nil {
return nil, err
}
return resp.Collections, nil
}
func ParseExportResultJSON(jsonStr string) (ExportResult, error) {
var resp ExportResultResponse
if err := json.Unmarshal([]byte(jsonStr), &resp); err != nil {
return ExportResult{}, err
}
if resp.Error != "" {
return ExportResult{}, fmt.Errorf("%s", resp.Error)
}
return resp.ExportResult, nil
}