5 // Created by Mike Mayo on 12/7/10.
6 // The OpenStack project is provided under the Apache 2.0 license.
9 #import "StorageObject.h"
10 #import "ComputeModel.h"
11 #import "NSObject+NSCoding.h"
13 @implementation StorageObject
15 @synthesize name, fullPath, hash, bytes, contentType, lastModified, data, publicURI, sharing, metadata, iso8601DateString, lastModifiedString;
17 #pragma mark - Object lifecycle
19 + (StorageObject *)fromJSON:(NSDictionary *)dict {
20 StorageObject *object = [[[StorageObject alloc] init] autorelease];
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;
35 - (NSComparisonResult)compareByName:(id)item {
36 return [self.name caseInsensitiveCompare:[item name]];
39 - (NSComparisonResult)compareByDate:(id)item {
40 return [self.lastModifiedString caseInsensitiveCompare:[item lastModifiedString]];
43 #pragma mark - Serialization
45 - (id)initWithCoder:(NSCoder *)coder {
46 if ((self = [super init])) {
47 [self autoDecode:coder];
52 - (void)encodeWithCoder:(NSCoder *)coder {
53 [self autoEncodeWithCoder:coder];
56 #pragma mark - Memory Management
62 [contentType release];
63 [lastModified release];
66 [iso8601DateString release];
67 [lastModifiedString release];
71 #pragma mark - Properties
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];
80 return lastModifiedString;
83 #pragma mark - Actions
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"];
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];
103 -(NSString *)humanizedBytes {
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];
112 result = [NSString stringWithFormat:@"%i bytes", self.bytes];
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])];
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])];