56bf303e009c4e0dd77c1b72e180529f27408db0
[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 @implementation StorageObject
14
15 @synthesize name, fullPath, hash, bytes, contentType, lastModified, data, publicURI, sharing, metadata, iso8601DateString, lastModifiedString;
16
17 #pragma mark - Object lifecycle
18
19 + (StorageObject *)fromJSON:(NSDictionary *)dict {
20     StorageObject *object = [[[StorageObject alloc] init] autorelease];
21     
22     object.name = [dict objectForKey:@"name"]; // this may be shortened by folder parsing later
23     object.fullPath = [dict objectForKey:@"name"];
24     object.hash = [dict objectForKey:@"x_object_hash"];
25     object.bytes = [[dict objectForKey:@"bytes"] intValue];
26     object.contentType = [dict objectForKey:@"content_type"];
27     //object.lastModified = [ComputeModel dateFromString:[dict objectForKey:@"last_modified"]];
28     object.iso8601DateString = [dict objectForKey:@"last_modified"];
29     object.publicURI = [dict objectForKey:@"x_object_public"];
30     object.sharing = [dict objectForKey:@"x_object_sharing"];
31     object.metadata = nil;
32     return object;
33 }
34
35 - (NSComparisonResult)compareByName:(id)item {
36     return [self.name caseInsensitiveCompare:[item name]];
37 }
38
39 - (NSComparisonResult)compareByDate:(id)item {
40     return [self.lastModifiedString caseInsensitiveCompare:[item lastModifiedString]];
41 }
42
43 #pragma mark - Serialization
44
45 - (id)initWithCoder:(NSCoder *)coder {
46     if ((self = [super init])) {
47         [self autoDecode:coder];
48     }
49     return self;
50 }
51
52 - (void)encodeWithCoder:(NSCoder *)coder {
53     [self autoEncodeWithCoder:coder];
54 }
55
56 #pragma mark - Memory Management
57
58 -(void)dealloc {
59         [name release];
60     [fullPath release];
61         [hash release];
62         [contentType release];
63         [lastModified release];
64         [data release];
65         [metadata release];
66     [iso8601DateString release];
67     [lastModifiedString release];
68         [super dealloc];
69 }
70
71 #pragma mark - Properties
72
73 - (NSString *)lastModifiedString {
74     if (!lastModifiedString) {
75         if (!self.lastModified && [self.iso8601DateString length])
76             self.lastModified = [ComputeModel dateFromString:self.iso8601DateString];
77         if (self.lastModified)
78             lastModifiedString = [[ComputeModel localDateDescriptionFromDate:self.lastModified] retain];
79     }
80     return lastModifiedString;
81 }
82
83 #pragma mark - Actions
84
85 - (void)setPropertiesfromResponseHeaders:(NSDictionary *)headers {
86     self.hash = [headers objectForKey:@"X-Object-Hash"];
87     self.bytes = [[headers objectForKey:@"Content-Length"] intValue];
88     self.contentType = [headers objectForKey:@"Content-Type"];
89     self.lastModified = [headers objectForKey:@"Last-Modified"];
90     self.publicURI = [headers objectForKey:@"X-Object-Public"];
91     self.sharing = [headers objectForKey:@"X-Object-Sharing"];
92     
93     self.metadata = [NSMutableDictionary dictionary];
94     for (NSString *key in headers) {
95         if ([key hasPrefix:@"X-Object-Meta-"]) {
96             NSString *metadataValue = [headers objectForKey:key];
97             NSString *metadataKey = [key substringFromIndex:14];
98             [self.metadata setObject:metadataValue forKey:metadataKey];
99         }
100     }
101 }
102
103 -(NSString *)humanizedBytes {
104         NSString *result;       
105         if (self.bytes >= 1024000000) {
106                 result = [NSString stringWithFormat:@"%.2f GiB", self.bytes / 1024000000.0];
107         } else if (self.bytes >= 1024000) {
108                 result = [NSString stringWithFormat:@"%.2f MiB", self.bytes / 1024000.0];
109         } else if (self.bytes >= 1024) {
110                 result = [NSString stringWithFormat:@"%.2f KiB", self.bytes / 1024.0];
111         } else {
112                 result = [NSString stringWithFormat:@"%i bytes", self.bytes];
113         }
114         return result;
115 }
116
117 - (BOOL)isPlayableMedia {
118     // check file extension
119     NSString *extensionPattern = @"^.+\\.((mov)|(m4a)|(mp3)|(wav)|(aiff)|(aac)|(aif)|(aifc)|(amr)|(caf)|(m2a)|(m4p)|(mp4)|(mpv)|(3gp))$";
120     NSRegularExpression *extensionRegex = [NSRegularExpression regularExpressionWithPattern:extensionPattern options:NSRegularExpressionCaseInsensitive error:nil];
121     NSInteger matches = [extensionRegex numberOfMatchesInString:self.name options:0 range:NSMakeRange(0, [self.name length])];
122
123     // check content type
124     NSString *contentTypePattern = @"^(video|audio)/";
125     NSRegularExpression *contentTypeRegex = [NSRegularExpression regularExpressionWithPattern:contentTypePattern options:NSRegularExpressionCaseInsensitive error:nil];
126     matches += [contentTypeRegex numberOfMatchesInString:self.contentType options:0 range:NSMakeRange(0, [self.contentType length])];
127     
128     return matches > 0;
129 }
130
131 @end