Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 479c284dfc | |||
| 009c71e6bb | |||
| b2d4c6188d |
@@ -1,6 +1,6 @@
|
||||
BINARY := ./bin/photoscli
|
||||
MODULE := gitea.k3s.k0.nu/tools/photocli
|
||||
VERSION := 0.2.1
|
||||
VERSION := 0.2.4
|
||||
BRIDGE_DIR := bridge
|
||||
LDFLAGS := -X main.version=$(VERSION)
|
||||
OBJ := $(BRIDGE_DIR)/photokit_bridge.o
|
||||
|
||||
@@ -28,6 +28,8 @@ char *photos_list_tree_json(void);
|
||||
|
||||
void photos_request_cancel(void);
|
||||
|
||||
int photos_request_is_cancelled(void);
|
||||
|
||||
void photos_free_string(char *value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -10,8 +10,15 @@ static NSDictionary *make_error_dict(NSString *message) {
|
||||
}
|
||||
|
||||
static BOOL semaphore_wait_with_timeout(dispatch_semaphore_t sem, int64_t seconds) {
|
||||
dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC);
|
||||
return dispatch_semaphore_wait(sem, timeout) == 0;
|
||||
int64_t deadline = (int64_t)[NSDate timeIntervalSinceReferenceDate] + seconds;
|
||||
while (1) {
|
||||
if (photos_cancelled) return NO;
|
||||
int64_t remaining = deadline - (int64_t)[NSDate timeIntervalSinceReferenceDate];
|
||||
if (remaining <= 0) return NO;
|
||||
int64_t waitSecs = remaining < 1 ? remaining : 1;
|
||||
dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, waitSecs * NSEC_PER_SEC);
|
||||
if (dispatch_semaphore_wait(sem, timeout) == 0) return YES;
|
||||
}
|
||||
}
|
||||
|
||||
static NSDictionary *collection_to_dict(PHCollection *collection) {
|
||||
@@ -267,6 +274,10 @@ char *photos_export_preview_json(const char *asset_id, const char *output_dir, i
|
||||
NSString *nsOutputDir = [NSString stringWithUTF8String:output_dir];
|
||||
if (!nsAssetId || !nsOutputDir) return json_from_object(make_error_dict(@"invalid UTF-8 in arguments"));
|
||||
|
||||
if (!ensure_directory(nsOutputDir)) {
|
||||
return json_from_object(make_error_dict(@"failed to create output directory"));
|
||||
}
|
||||
|
||||
PHFetchResult<PHAsset *> *fetch = [PHAsset fetchAssetsWithLocalIdentifiers:@[nsAssetId] options:nil];
|
||||
if (fetch.count == 0) return json_from_object(make_error_dict(@"asset not found"));
|
||||
|
||||
@@ -315,7 +326,8 @@ char *photos_export_preview_json(const char *asset_id, const char *output_dir, i
|
||||
|
||||
NSError *writeErr = nil;
|
||||
if (![imageData writeToFile:filepath options:NSDataWritingAtomic error:&writeErr]) {
|
||||
return json_from_object(@{@"error": @"write failed", @"cloud": asset_cloud_status_string(asset)});
|
||||
NSString *msg = writeErr ? writeErr.localizedDescription : @"unknown write error";
|
||||
return json_from_object(@{@"error": [NSString stringWithFormat:@"write failed: %@", msg], @"cloud": asset_cloud_status_string(asset)});
|
||||
}
|
||||
|
||||
NSNumber *fileSize = nil;
|
||||
@@ -338,6 +350,10 @@ char *photos_export_original_json(const char *asset_id, const char *output_dir,
|
||||
NSString *nsOutputDir = [NSString stringWithUTF8String:output_dir];
|
||||
if (!nsAssetId || !nsOutputDir) return json_from_object(make_error_dict(@"invalid UTF-8 in arguments"));
|
||||
|
||||
if (!ensure_directory(nsOutputDir)) {
|
||||
return json_from_object(make_error_dict(@"failed to create output directory"));
|
||||
}
|
||||
|
||||
PHFetchResult<PHAsset *> *fetch = [PHAsset fetchAssetsWithLocalIdentifiers:@[nsAssetId] options:nil];
|
||||
if (fetch.count == 0) return json_from_object(make_error_dict(@"asset not found"));
|
||||
|
||||
@@ -375,7 +391,7 @@ char *photos_export_original_json(const char *asset_id, const char *output_dir,
|
||||
}
|
||||
|
||||
if (writeErr) {
|
||||
return json_from_object(@{@"error": @"write failed", @"cloud": asset_cloud_status_string(asset)});
|
||||
return json_from_object(@{@"error": [NSString stringWithFormat:@"write failed: %@", writeErr.localizedDescription], @"cloud": asset_cloud_status_string(asset)});
|
||||
}
|
||||
|
||||
NSString *writtenFilename = [filepath lastPathComponent];
|
||||
@@ -399,3 +415,7 @@ void photos_free_string(char *value) {
|
||||
void photos_request_cancel(void) {
|
||||
photos_cancelled = 1;
|
||||
}
|
||||
|
||||
int photos_request_is_cancelled(void) {
|
||||
return photos_cancelled;
|
||||
}
|
||||
|
||||
@@ -74,6 +74,10 @@ void photos_request_cancel(void) {
|
||||
stub_cancelled = 1;
|
||||
}
|
||||
|
||||
int photos_request_is_cancelled(void) {
|
||||
return stub_cancelled;
|
||||
}
|
||||
|
||||
void photos_test_set_export_preview_json(const char *json) {
|
||||
stub_export_preview_json = json;
|
||||
}
|
||||
|
||||
@@ -263,6 +263,9 @@ func backupTree(nodes []photos.CollectionNode, outDir string, targetSize int, or
|
||||
total := 0
|
||||
failed := 0
|
||||
for _, node := range nodes {
|
||||
if bridge.IsCancelled() {
|
||||
break
|
||||
}
|
||||
path := outDir + "/" + sanitizePathComponent(node.Name)
|
||||
if node.Kind == "folder" {
|
||||
n, t, f, err := backupTree(node.Children, path, targetSize, originals, stderr, bridge)
|
||||
@@ -306,6 +309,9 @@ func exportAssetsSerial(assets []photos.Asset, outDir string, targetSize int, or
|
||||
exported := 0
|
||||
failed := 0
|
||||
for i, a := range assets {
|
||||
if bridge.IsCancelled() {
|
||||
break
|
||||
}
|
||||
result, exportErr := exportOne(bridge, a, outDir, targetSize, originals, i)
|
||||
progressBar(stderr, exported+failed+1, total, dirPrefix+result.Filename, result.Size, result.Cloud)
|
||||
if exportErr != nil {
|
||||
@@ -337,6 +343,11 @@ func exportAssetsParallel(assets []photos.Asset, outDir string, targetSize int,
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := range jobs {
|
||||
if bridge.IsCancelled() {
|
||||
slots[i].err = fmt.Errorf("cancelled")
|
||||
close(slots[i].done)
|
||||
continue
|
||||
}
|
||||
result, exportErr := exportOne(bridge, assets[i], outDir, targetSize, originals, i)
|
||||
slots[i].result = result
|
||||
slots[i].err = exportErr
|
||||
|
||||
@@ -50,6 +50,7 @@ func (m *mockBridge) ExportOriginal(assetID, out string, index int) (photos.Expo
|
||||
return photos.ExportResult{Filename: "test.jpg", Size: 2048, Cloud: "cloud"}, nil
|
||||
}
|
||||
func (m *mockBridge) Cancel() { m.cancelled = true }
|
||||
func (m *mockBridge) IsCancelled() bool { return m.cancelled }
|
||||
|
||||
func runWith(args []string, b photos.Bridge) (string, string, int) {
|
||||
var out, err bytes.Buffer
|
||||
|
||||
@@ -13,6 +13,7 @@ type Bridge interface {
|
||||
ExportPreview(assetID, outputDir string, targetSize, index int) (ExportResult, error)
|
||||
ExportOriginal(assetID, outputDir string, index int) (ExportResult, error)
|
||||
Cancel()
|
||||
IsCancelled() bool
|
||||
}
|
||||
|
||||
func ParseAlbumsJSON(jsonStr string) ([]Album, error) {
|
||||
|
||||
@@ -58,6 +58,10 @@ 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, index int) (ExportResult, error) {
|
||||
cid := C.CString(assetID)
|
||||
defer C.free(unsafe.Pointer(cid))
|
||||
|
||||
@@ -78,6 +78,10 @@ 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, index int) (ExportResult, error) {
|
||||
cid := C.CString(assetID)
|
||||
defer C.free(unsafe.Pointer(cid))
|
||||
|
||||
Reference in New Issue
Block a user