Revision 64929bae Classes/PithosUtilities.m

b/Classes/PithosUtilities.m
47 47
            [contentType hasPrefix:@"application/folder;"]);
48 48
}
49 49

  
50
+ (unsigned long long)sizeOfDirectory:(NSString *)directoryPath {
51
    NSFileManager *fileManager = [NSFileManager defaultManager];
52
    NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:directoryPath];
53
    unsigned long long directorySize = 0;
54
    NSString *file;
55
    while (file = [directoryEnumerator nextObject]) {
56
        NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:[NSString stringWithFormat:@"%@/%@",directoryPath,file] error:nil];
57
        directorySize += [fileAttributes fileSize];
58
    }
59
    return directorySize;
60
}
61

  
62
+ (NSString *)humanReadableSize:(unsigned long long)bytes {
63
    if (bytes <= 999) {
64
        return [NSString stringWithFormat:@"%d B", (int)bytes];
65
    } else if (bytes <= 999000) {
66
        return [NSString stringWithFormat:@"%d KB", (int)ceil(bytes / 1000)];
67
    } else if (bytes <= 999900000) {
68
        double megabytes = floor(bytes / 1000000);
69
        double hundredkilobytes = ceil((bytes - megabytes * 1000000) / 100000);
70
        if (hundredkilobytes == 10) {
71
            megabytes++;
72
            hundredkilobytes = 0;
73
        }
74
        if (hundredkilobytes == 0) {
75
            return [NSString stringWithFormat:@"%d MB", (int)megabytes];
76
        } else {
77
            return [NSString stringWithFormat:@"%d.%d MB", (int)megabytes, (int)hundredkilobytes];
78
        }
79
    } else {
80
        double gigabytes = floor(bytes / 1000000000);
81
        double hundredmegabytes = ceil((bytes - gigabytes * 1000000000) / 100000000);
82
        if (hundredmegabytes == 10) {
83
            gigabytes++;
84
            hundredmegabytes = 0;
85
        }
86
        if (hundredmegabytes == 0) {
87
            return [NSString stringWithFormat:@"%d GB", (int)gigabytes];
88
        } else {
89
            return [NSString stringWithFormat:@"%d.%d GB", (int)gigabytes, (int)hundredmegabytes];
90
        }
91
    }
92
}
93

  
50 94
@end

Also available in: Unified diff