171 lines
5.5 KiB
Go
171 lines
5.5 KiB
Go
//go:build test
|
|
|
|
package photos
|
|
|
|
/*
|
|
#cgo CFLAGS: -I${SRCDIR}/../../bridge
|
|
#cgo LDFLAGS: -L${SRCDIR}/../../bridge -lphotokit_bridge_stub
|
|
#include "photokit_bridge.h"
|
|
#include <stdlib.h>
|
|
|
|
void photos_test_set_access(int rc);
|
|
void photos_test_set_albums(const char *json);
|
|
void photos_test_set_assets(const char *json);
|
|
void photos_test_set_tree(const char *json);
|
|
void photos_test_set_albums_null(void);
|
|
void photos_test_set_assets_null(void);
|
|
void photos_test_set_tree_null(void);
|
|
void photos_request_cancel(void);
|
|
void photos_test_set_export_preview_json(const char *json);
|
|
void photos_test_set_export_preview_json_null(void);
|
|
void photos_test_set_export_original_json(const char *json);
|
|
void photos_test_set_export_original_json_null(void);
|
|
void photos_test_set_progress_slot_count(int count);
|
|
void photos_test_set_progress_slots_null(int val);
|
|
*/
|
|
import "C"
|
|
|
|
import "unsafe"
|
|
|
|
type CgoBridge struct{}
|
|
|
|
var DefaultBridge Bridge = &CgoBridge{}
|
|
|
|
func SetTestAccessRC(rc int) { C.photos_test_set_access(C.int(rc)) }
|
|
func SetTestAlbumsJSON(json string) { C.photos_test_set_albums(C.CString(json)) }
|
|
func SetTestAssetsJSON(json string) { C.photos_test_set_assets(C.CString(json)) }
|
|
func SetTestTreeJSON(json string) { C.photos_test_set_tree(C.CString(json)) }
|
|
func SetTestAlbumsNull() { C.photos_test_set_albums_null() }
|
|
func SetTestAssetsNull() { C.photos_test_set_assets_null() }
|
|
func SetTestTreeNull() { C.photos_test_set_tree_null() }
|
|
func SetTestExportPreviewJSON(json string) { C.photos_test_set_export_preview_json(C.CString(json)) }
|
|
func SetTestExportPreviewJSONNull() { C.photos_test_set_export_preview_json_null() }
|
|
func SetTestExportOriginalJSON(json string) { C.photos_test_set_export_original_json(C.CString(json)) }
|
|
func SetTestExportOriginalJSONNull() { C.photos_test_set_export_original_json_null() }
|
|
func SetTestProgressSlotCount(count int) { C.photos_test_set_progress_slot_count(C.int(count)) }
|
|
func SetTestProgressSlotsNull(val int) { C.photos_test_set_progress_slots_null(C.int(val)) }
|
|
|
|
func (*CgoBridge) RequestAccess() error {
|
|
rc := C.photos_request_access()
|
|
if rc != 0 {
|
|
return errAccessDenied
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (*CgoBridge) ListAlbums() ([]Album, error) {
|
|
cs := C.photos_list_albums_json()
|
|
if cs == nil {
|
|
return nil, errBridgeNil
|
|
}
|
|
defer C.photos_free_string(cs)
|
|
return ParseAlbumsJSON(C.GoString(cs))
|
|
}
|
|
|
|
func (*CgoBridge) ListAssets(albumID string) ([]Asset, int, error) {
|
|
cid := C.CString(albumID)
|
|
defer C.free(unsafe.Pointer(cid))
|
|
cs := C.photos_list_assets_json(cid)
|
|
if cs == nil {
|
|
return nil, 0, errBridgeNil
|
|
}
|
|
defer C.photos_free_string(cs)
|
|
return ParseAssetsJSON(C.GoString(cs))
|
|
}
|
|
|
|
func (*CgoBridge) ListTree() ([]CollectionNode, error) {
|
|
cs := C.photos_list_tree_json()
|
|
if cs == nil {
|
|
return nil, errBridgeNil
|
|
}
|
|
defer C.photos_free_string(cs)
|
|
return ParseTreeJSON(C.GoString(cs))
|
|
}
|
|
|
|
func (*CgoBridge) Cancel() {
|
|
C.photos_request_cancel()
|
|
}
|
|
|
|
func (*CgoBridge) IsCancelled() bool {
|
|
return C.photos_request_is_cancelled() != 0
|
|
}
|
|
|
|
func (*CgoBridge) ExportPreview(assetID, outputDir string, targetSize, quality, index int) (ExportResult, error) {
|
|
return exportPreviewWithSlotTest(assetID, outputDir, targetSize, quality, index, -1)
|
|
}
|
|
|
|
func (*CgoBridge) ExportOriginal(assetID, outputDir string, index int) (ExportResult, error) {
|
|
return exportOriginalWithSlotTest(assetID, outputDir, index, -1)
|
|
}
|
|
|
|
func (*CgoBridge) ExportPreviewWithSlot(assetID, outputDir string, targetSize, quality, index, slotIndex int) (ExportResult, error) {
|
|
return exportPreviewWithSlotTest(assetID, outputDir, targetSize, quality, index, slotIndex)
|
|
}
|
|
|
|
func (*CgoBridge) ExportOriginalWithSlot(assetID, outputDir string, index, slotIndex int) (ExportResult, error) {
|
|
return exportOriginalWithSlotTest(assetID, outputDir, index, slotIndex)
|
|
}
|
|
|
|
func exportPreviewWithSlotTest(assetID, outputDir string, targetSize, quality, index, slotIndex int) (ExportResult, error) {
|
|
cid := C.CString(assetID)
|
|
defer C.free(unsafe.Pointer(cid))
|
|
cdir := C.CString(outputDir)
|
|
defer C.free(unsafe.Pointer(cdir))
|
|
cs := C.photos_export_preview_json(cid, cdir, C.int(targetSize), C.int(quality), C.int(index), C.int(slotIndex))
|
|
if cs == nil {
|
|
return ExportResult{}, errBridgeNil
|
|
}
|
|
defer C.photos_free_string(cs)
|
|
return ParseExportResultJSON(C.GoString(cs))
|
|
}
|
|
|
|
func exportOriginalWithSlotTest(assetID, outputDir string, index, slotIndex int) (ExportResult, error) {
|
|
cid := C.CString(assetID)
|
|
defer C.free(unsafe.Pointer(cid))
|
|
cdir := C.CString(outputDir)
|
|
defer C.free(unsafe.Pointer(cdir))
|
|
cs := C.photos_export_original_json(cid, cdir, C.int(index), C.int(slotIndex))
|
|
if cs == nil {
|
|
return ExportResult{}, errBridgeNil
|
|
}
|
|
defer C.photos_free_string(cs)
|
|
return ParseExportResultJSON(C.GoString(cs))
|
|
}
|
|
|
|
type ExportProgressSlot struct {
|
|
Active bool
|
|
Progress float64
|
|
BytesDone int64
|
|
BytesTotal int64
|
|
}
|
|
|
|
func GetProgressSlots() []ExportProgressSlot {
|
|
count := int(C.photos_get_progress_slot_count())
|
|
if count <= 0 {
|
|
return nil
|
|
}
|
|
cSlots := C.photos_get_progress_slots()
|
|
if cSlots == nil {
|
|
return nil
|
|
}
|
|
slots := make([]ExportProgressSlot, count)
|
|
for i := 0; i < count; i++ {
|
|
s := C.photos_get_progress_slot(cSlots, C.int(i))
|
|
slots[i] = ExportProgressSlot{
|
|
Active: s.active != 0,
|
|
Progress: float64(s.progress),
|
|
BytesDone: int64(s.bytes_done),
|
|
BytesTotal: int64(s.bytes_total),
|
|
}
|
|
}
|
|
return slots
|
|
}
|
|
|
|
func ResetProgressSlots() {
|
|
C.photos_reset_progress_slots()
|
|
}
|
|
|
|
func GetProgressSlotCount() int {
|
|
return int(C.photos_get_progress_slot_count())
|
|
}
|