85 lines
2.3 KiB
Go
85 lines
2.3 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)
|
|
ExportAlbumPreviews(albumID, outputDir string, targetSize int) (int, error)
|
|
ExportAlbumOriginals(albumID, outputDir string) (int, error)
|
|
ExportPreview(assetID, outputDir string, targetSize, index int) (ExportResult, error)
|
|
ExportOriginal(assetID, outputDir string, index int) (ExportResult, error)
|
|
BackupAll(outputDir string, targetSize int, originals bool) (int, error)
|
|
Cancel()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func InterpretExportResult(rc int) (int, error) {
|
|
switch rc {
|
|
case -1:
|
|
return 0, fmt.Errorf("invalid arguments or directory creation failed")
|
|
case -2:
|
|
return 0, fmt.Errorf("could not create output directory")
|
|
case -3:
|
|
return 0, fmt.Errorf("album not found")
|
|
case -4:
|
|
return 0, fmt.Errorf("all exports failed")
|
|
case -5:
|
|
return 0, fmt.Errorf("cancelled")
|
|
default:
|
|
if rc < 0 {
|
|
return 0, fmt.Errorf("unknown error (code %d)", rc)
|
|
}
|
|
return rc, nil
|
|
}
|
|
}
|