github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/ios-objc/photobackup/LACamliClient/LACamliFile.m (about) 1 // 2 // LACamliFile.m 3 // 4 // Created by Nick O'Neill on 1/13/13. 5 // Copyright (c) 2013 The Camlistore Authors. All rights reserved. 6 // 7 8 #import "LACamliFile.h" 9 #import "LACamliUtil.h" 10 #import <AssetsLibrary/AssetsLibrary.h> 11 12 @implementation LACamliFile 13 14 @synthesize allBlobs = _allBlobs; 15 @synthesize allBlobRefs = _allBlobRefs; 16 17 static NSUInteger const ChunkSize = 64000; 18 19 - (id)initWithAsset:(ALAsset *)asset 20 { 21 if (self = [super init]) { 22 self.asset = asset; 23 24 [self setBlobRef:[LACamliUtil blobRef:[self fileData]]]; 25 26 float chunkCount = (float)[self size] / (float)ChunkSize; 27 28 self.uploadMarks = [NSMutableArray array]; 29 for (int i = 0; i < chunkCount; i++) { 30 [self.uploadMarks addObject:@YES]; 31 } 32 } 33 34 return self; 35 } 36 37 - (id)initWithPath:(NSString *)path 38 { 39 // TODO, can init from random path to file 40 41 if (self = [super init]) { 42 // [self setBlobRef:[LACamliClient blobRef:data]]; 43 // [self setFileData:data]; 44 45 // set time, size and other properties here? 46 } 47 48 return self; 49 } 50 51 #pragma mark - convenience 52 53 - (NSData *)fileData 54 { 55 ALAssetRepresentation *rep = [self.asset defaultRepresentation]; 56 Byte *buf = (Byte*)malloc((int)rep.size); 57 NSUInteger bufferLength = [rep getBytes:buf fromOffset:0.0 length:(int)rep.size error:nil]; 58 59 return [NSData dataWithBytesNoCopy:buf length:bufferLength freeWhenDone:YES]; 60 } 61 62 - (long long)size 63 { 64 return [self.asset defaultRepresentation].size; 65 } 66 67 - (NSDate *)creation 68 { 69 return [self.asset valueForProperty:ALAssetPropertyDate]; 70 } 71 72 - (NSArray *)blobsToUpload 73 { 74 NSMutableArray *blobs = [NSMutableArray array]; 75 76 int i = 0; 77 for (NSData *blob in self.allBlobs) { 78 if ([[self.uploadMarks objectAtIndex:i] boolValue]) { 79 [blobs addObject:blob]; 80 } 81 i++; 82 } 83 84 return blobs; 85 } 86 87 #pragma mark - delayed creation methods 88 89 - (void)setAllBlobs:(NSMutableArray *)allBlobs 90 { 91 _allBlobs = allBlobs; 92 } 93 94 - (NSMutableArray *)allBlobs 95 { 96 if (!_allBlobs) { 97 [self makeBlobsAndRefs]; 98 } 99 100 // not a huge fan of how this doesn't obviously assign to _allBlobs 101 return _allBlobs; 102 } 103 104 - (void)setAllBlobRefs:(NSArray *)allBlobRefs 105 { 106 _allBlobRefs = allBlobRefs; 107 } 108 109 - (NSArray *)allBlobRefs 110 { 111 if (!_allBlobRefs) { 112 [self makeBlobsAndRefs]; 113 } 114 115 // not a huge fan of how this doesn't obviously assign to _allBlobRefs 116 return _allBlobRefs; 117 } 118 119 120 - (void)makeBlobsAndRefs 121 { 122 LALog(@"making blob refs"); 123 124 NSMutableArray *chunks = [NSMutableArray array]; 125 NSMutableArray *blobRefs = [NSMutableArray array]; 126 127 float chunkCount = (float)self.size / (float)ChunkSize; 128 129 NSData *fileData = [self fileData]; 130 131 for (int i = 0; i < chunkCount; i++) { 132 133 // ChunkSize size chunks, unless the last one is less 134 NSData *chunkData; 135 if (ChunkSize*(i+1) <= [self size]) { 136 chunkData = [fileData subdataWithRange:NSMakeRange(ChunkSize*i, ChunkSize)]; 137 } else { 138 chunkData = [fileData subdataWithRange:NSMakeRange(ChunkSize*i, (int)[self size]-(ChunkSize*i))]; 139 } 140 141 [chunks addObject:chunkData]; 142 [blobRefs addObject:[LACamliUtil blobRef:chunkData]]; 143 } 144 145 self.allBlobs = chunks; 146 self.allBlobRefs = blobRefs; 147 } 148 149 @end