Added metadata functionality.
[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, 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     
67     object.metadata = [NSMutableDictionary dictionary];
68     for (NSString *key in dict) {
69         if ([key hasPrefix:@"x_object_meta_"]) {
70             NSString *metadataKey = [key substringFromIndex:14];
71             [object.metadata setObject:[dict objectForKey:key] forKey:metadataKey];
72         }
73     }
74         
75     return object;
76 }
77
78 #pragma mark -
79 #pragma mark Presentation
80
81 -(NSString *)humanizedBytes {
82         NSString *result;       
83         if (self.bytes >= 1024000000) {
84                 result = [NSString stringWithFormat:@"%.2f GiB", self.bytes / 1024000000.0];
85         } else if (self.bytes >= 1024000) {
86                 result = [NSString stringWithFormat:@"%.2f MiB", self.bytes / 1024000.0];
87         } else if (self.bytes >= 1024) {
88                 result = [NSString stringWithFormat:@"%.2f KiB", self.bytes / 1024.0];
89         } else {
90                 result = [NSString stringWithFormat:@"%i bytes", self.bytes];
91         }
92         return result;
93 }
94
95 - (BOOL)isPlayableMedia {
96     
97     // check file extension
98     NSString *extensionPattern = @"^.+\\.((mov)|(m4a)|(mp3)|(wav)|(aiff)|(aac)|(aif)|(aifc)|(amr)|(caf)|(m2a)|(m4p)|(mp4)|(mpv)|(3gp))$";
99     NSRegularExpression *extensionRegex = [NSRegularExpression regularExpressionWithPattern:extensionPattern options:NSRegularExpressionCaseInsensitive error:nil];
100     NSInteger matches = [extensionRegex numberOfMatchesInString:self.name options:0 range:NSMakeRange(0, [self.name length])];
101
102     // check content type
103     NSString *contentTypePattern = @"^(video|audio)/";
104     NSRegularExpression *contentTypeRegex = [NSRegularExpression regularExpressionWithPattern:contentTypePattern options:NSRegularExpressionCaseInsensitive error:nil];
105     matches += [contentTypeRegex numberOfMatchesInString:self.contentType options:0 range:NSMakeRange(0, [self.contentType length])];
106     
107     return matches > 0;
108 }
109
110 - (NSComparisonResult)compare:(StorageObject *)anObject {
111     return [self.name caseInsensitiveCompare:anObject.name];
112 }
113
114 #pragma mark -
115 #pragma mark Memory Management
116
117 -(void)dealloc {
118         [name release];
119     [fullPath release];
120         [hash release];
121         [contentType release];
122         [lastModified release];
123         [data release];
124         [metadata release];
125         [super dealloc];
126 }
127
128 @end