2b3cc8075be80bfcdd6611d274e935da65ff16d8
[pithos-ios] / Classes / StorageObject.m
1 //
2 //  Object.m
3 //  OpenStack
4 //
5 //  Created by Mike Mayo on 12/7/10.
6 //  The OpenStack project is provided under the Apache 2.0 license.
7 //
8
9 #import "StorageObject.h"
10 #import "ComputeModel.h"
11 #import "NSObject+NSCoding.h"
12
13
14 @implementation StorageObject
15
16 @synthesize name, fullPath, hash, bytes, contentType, lastModified, data, publicURI, metadata;
17
18 #pragma mark -
19 #pragma mark Serialization
20
21 - (void)encodeWithCoder:(NSCoder *)coder {
22     [self autoEncodeWithCoder:coder];
23     /*
24     [coder encodeObject:name forKey:@"name"];
25     [coder encodeObject:fullPath forKey:@"fullPath"];
26     [coder encodeObject:hash forKey:@"hash"];
27     [coder encodeInt:bytes forKey:@"bytes"];
28     [coder encodeObject:contentType forKey:@"contentType"];
29     [coder encodeObject:lastModified forKey:@"lastModified"];
30     // not persisting data to save space
31     [coder encodeObject:metadata forKey:@"metadata"];
32      */
33 }
34
35 - (id)initWithCoder:(NSCoder *)coder {
36     self = [super init];
37     if (self) {
38         [self autoDecode:coder];
39         /*
40         name = [[coder decodeObjectForKey:@"name"] retain];
41         fullPath = [[coder decodeObjectForKey:@"fullPath"] retain];
42         hash = [[coder decodeObjectForKey:@"hash"] retain];
43         bytes = [coder decodeIntForKey:@"bytes"];
44         contentType = [[coder decodeObjectForKey:@"contentType"] retain];
45         lastModified = [[coder decodeObjectForKey:@"lastModified"] retain];
46         data = [[coder decodeObjectForKey:@"data"] retain];
47         metadata = [[coder decodeObjectForKey:@"metadata"] retain];
48          */
49     }
50     return self;
51 }
52
53 #pragma mark -
54 #pragma mark JSON
55
56 + (StorageObject *)fromJSON:(NSDictionary *)dict {
57     
58     StorageObject *object = [[[StorageObject alloc] init] autorelease];
59     
60     object.name = [dict objectForKey:@"name"]; // this may be shortened by folder parsing later
61     object.fullPath = [dict objectForKey:@"name"];
62     object.hash = [dict objectForKey:@"hash"];
63     object.bytes = [[dict objectForKey:@"bytes"] intValue];
64     object.contentType = [dict objectForKey:@"content_type"];
65     object.lastModified = [ComputeModel dateFromString:[dict objectForKey:@"last_modified"]];
66     object.publicURI = [dict objectForKey:@"x_object_public"];
67         
68     object.metadata = [NSMutableDictionary dictionary];
69     for (NSString *key in dict) {
70         if ([key hasPrefix:@"x_object_meta_"]) {
71             NSString *metadataKey = [key substringFromIndex:14];
72             [object.metadata setObject:[dict objectForKey:key] forKey:metadataKey];
73         }
74     }
75         
76     return object;
77 }
78
79 #pragma mark -
80 #pragma mark Presentation
81
82 -(NSString *)humanizedBytes {
83         NSString *result;       
84         if (self.bytes >= 1024000000) {
85                 result = [NSString stringWithFormat:@"%.2f GiB", self.bytes / 1024000000.0];
86         } else if (self.bytes >= 1024000) {
87                 result = [NSString stringWithFormat:@"%.2f MiB", self.bytes / 1024000.0];
88         } else if (self.bytes >= 1024) {
89                 result = [NSString stringWithFormat:@"%.2f KiB", self.bytes / 1024.0];
90         } else {
91                 result = [NSString stringWithFormat:@"%i bytes", self.bytes];
92         }
93         return result;
94 }
95
96 - (BOOL)isPlayableMedia {
97     
98     // check file extension
99     NSString *extensionPattern = @"^.+\\.((mov)|(m4a)|(mp3)|(wav)|(aiff)|(aac)|(aif)|(aifc)|(amr)|(caf)|(m2a)|(m4p)|(mp4)|(mpv)|(3gp))$";
100     NSRegularExpression *extensionRegex = [NSRegularExpression regularExpressionWithPattern:extensionPattern options:NSRegularExpressionCaseInsensitive error:nil];
101     NSInteger matches = [extensionRegex numberOfMatchesInString:self.name options:0 range:NSMakeRange(0, [self.name length])];
102
103     // check content type
104     NSString *contentTypePattern = @"^(video|audio)/";
105     NSRegularExpression *contentTypeRegex = [NSRegularExpression regularExpressionWithPattern:contentTypePattern options:NSRegularExpressionCaseInsensitive error:nil];
106     matches += [contentTypeRegex numberOfMatchesInString:self.contentType options:0 range:NSMakeRange(0, [self.contentType length])];
107     
108     return matches > 0;
109 }
110
111 - (NSComparisonResult)compare:(StorageObject *)anObject {
112     return [self.name caseInsensitiveCompare:anObject.name];
113 }
114
115 #pragma mark -
116 #pragma mark Memory Management
117
118 -(void)dealloc {
119         [name release];
120     [fullPath release];
121         [hash release];
122         [contentType release];
123         [lastModified release];
124         [data release];
125         [metadata release];
126         [super dealloc];
127 }
128
129 @end