github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/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 _asset = asset; 23 24 self.blobRef = [LACamliUtil blobRef:[self fileData]]; 25 26 float chunkCount = (float)[self size] / (float)ChunkSize; 27 28 _uploadMarks = [NSMutableArray array]; 29 for (int i = 0; i < chunkCount; i++) { 30 [_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 = [_asset defaultRepresentation]; 56 Byte* buf = (Byte*)malloc((int)rep.size); 57 NSUInteger bufferLength = [rep getBytes:buf 58 fromOffset:0.0 59 length:(int)rep.size 60 error:nil]; 61 62 return [NSData dataWithBytesNoCopy:buf 63 length:bufferLength 64 freeWhenDone:YES]; 65 } 66 67 - (long long)size 68 { 69 return [_asset defaultRepresentation].size; 70 } 71 72 - (NSString *)name 73 { 74 return [_asset defaultRepresentation].filename; 75 } 76 77 - (NSDate*)creation 78 { 79 return [_asset valueForProperty:ALAssetPropertyDate]; 80 } 81 82 - (UIImage*)thumbnail 83 { 84 return [UIImage imageWithCGImage:[_asset thumbnail]]; 85 } 86 87 - (NSArray*)blobsToUpload 88 { 89 NSMutableArray* blobs = [NSMutableArray array]; 90 91 int i = 0; 92 for (NSData* blob in _allBlobs) { 93 if ([[_uploadMarks objectAtIndex:i] boolValue]) { 94 [blobs addObject:blob]; 95 } 96 i++; 97 } 98 99 return blobs; 100 } 101 102 #pragma mark - delayed creation methods 103 104 - (void)setAllBlobs:(NSMutableArray*)allBlobs 105 { 106 _allBlobs = allBlobs; 107 } 108 109 - (NSMutableArray*)allBlobs 110 { 111 if (!_allBlobs) { 112 [self makeBlobsAndRefs]; 113 } 114 115 // not a huge fan of how this doesn't obviously assign to _allBlobs 116 return _allBlobs; 117 } 118 119 - (void)setAllBlobRefs:(NSArray*)allBlobRefs 120 { 121 _allBlobRefs = allBlobRefs; 122 } 123 124 - (NSArray*)allBlobRefs 125 { 126 if (!_allBlobRefs) { 127 [self makeBlobsAndRefs]; 128 } 129 130 // not a huge fan of how this doesn't obviously assign to _allBlobRefs 131 return _allBlobRefs; 132 } 133 134 - (void)makeBlobsAndRefs 135 { 136 LALog(@"making blob refs"); 137 138 NSMutableArray* chunks = [NSMutableArray array]; 139 NSMutableArray* blobRefs = [NSMutableArray array]; 140 141 float chunkCount = (float)[self size] / (float)ChunkSize; 142 143 NSData* fileData = [self fileData]; 144 145 for (int i = 0; i < chunkCount; i++) { 146 147 // ChunkSize size chunks, unless the last one is less 148 NSData* chunkData; 149 if (ChunkSize * (i + 1) <= [self size]) { 150 chunkData = [fileData subdataWithRange:NSMakeRange(ChunkSize * i, ChunkSize)]; 151 } else { 152 chunkData = [fileData subdataWithRange:NSMakeRange(ChunkSize * i, (int)[self size] - (ChunkSize * i))]; 153 } 154 155 [chunks addObject:chunkData]; 156 [blobRefs addObject:[LACamliUtil blobRef:chunkData]]; 157 } 158 159 _allBlobs = chunks; 160 _allBlobRefs = blobRefs; 161 } 162 163 @end