From 2a97155f44cc543169aa00782c34c00deae4adc6 Mon Sep 17 00:00:00 2001 From: ylecollen Date: Fri, 9 Jan 2015 17:14:09 +0100 Subject: [PATCH] The cache size did not include the media one (it was only the Matrix SDK one) --- .../matrixConsole/API/MediaManager.h | 1 + .../matrixConsole/API/MediaManager.m | 39 +++++++++++++++++++ matrixConsole/matrixConsole/MatrixHandler.h | 2 + matrixConsole/matrixConsole/MatrixHandler.m | 11 +++++- .../ViewController/SettingsViewController.m | 3 +- 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/matrixConsole/matrixConsole/API/MediaManager.h b/matrixConsole/matrixConsole/API/MediaManager.h index 91de7c5b4..8c47797f5 100644 --- a/matrixConsole/matrixConsole/API/MediaManager.h +++ b/matrixConsole/matrixConsole/API/MediaManager.h @@ -50,6 +50,7 @@ extern NSString *const kMediaDownloadDidFailNotification; + (NSString *)cacheMediaData:(NSData *)mediaData forURL:(NSString *)mediaURL mimeType:(NSString *)mimeType; ++ (NSUInteger)cacheSize; + (void)clearCache; @end diff --git a/matrixConsole/matrixConsole/API/MediaManager.m b/matrixConsole/matrixConsole/API/MediaManager.m index 60adf8fe5..5284eaf09 100644 --- a/matrixConsole/matrixConsole/API/MediaManager.m +++ b/matrixConsole/matrixConsole/API/MediaManager.m @@ -207,6 +207,45 @@ static NSMutableDictionary* pendingMediaLoadersByURL = nil; mediaCachePath = nil; } +// recursive method to compute the folder content size ++ (long long)folderSize:(NSString *)folderPath +{ + NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil]; + NSEnumerator *contentsEnumurator = [contents objectEnumerator]; + + NSString *file; + unsigned long long int folderSize = 0; + + while (file = [contentsEnumurator nextObject]) + { + NSString* itemPath = [folderPath stringByAppendingPathComponent:file]; + + NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:itemPath error:nil]; + + // is directory + if ([[fileAttributes objectForKey:NSFileType] isEqual:NSFileTypeDirectory]) + { + folderSize += [MediaManager folderSize:itemPath]; + } + else + { + folderSize += [[fileAttributes objectForKey:NSFileSize] intValue]; + } + } + + return folderSize; +} + ++ (NSUInteger)cacheSize { + + if (!mediaCachePath) { + // compute the path + mediaCachePath = [MediaManager getCachePath]; + } + + return (NSUInteger)[MediaManager folderSize:mediaCachePath]; +} + #pragma mark - Cache handling + (UIImage*)loadCachePicture:(NSString*)pictureURL { diff --git a/matrixConsole/matrixConsole/MatrixHandler.h b/matrixConsole/matrixConsole/MatrixHandler.h index 0f00843bc..c5e35c0ef 100644 --- a/matrixConsole/matrixConsole/MatrixHandler.h +++ b/matrixConsole/matrixConsole/MatrixHandler.h @@ -49,6 +49,8 @@ typedef enum : NSUInteger { @property (nonatomic,readonly) BOOL isResumeDone; // return the MX cache size in bytes @property (nonatomic,readonly) NSUInteger MXCacheSize; +// return the sum of the caches (MX cache + media cache ...) +@property (nonatomic,readonly) NSUInteger cachesSize; + (MatrixHandler *)sharedHandler; diff --git a/matrixConsole/matrixConsole/MatrixHandler.m b/matrixConsole/matrixConsole/MatrixHandler.m index 9051df44d..d118abd7d 100644 --- a/matrixConsole/matrixConsole/MatrixHandler.m +++ b/matrixConsole/matrixConsole/MatrixHandler.m @@ -22,6 +22,8 @@ #import "MXFileStore.h" #import "MXTools.h" +#import "MediaManager.h" + NSString *const kMatrixHandlerUnsupportedMessagePrefix = @"UNSUPPORTED MSG: "; static MatrixHandler *sharedHandler = nil; @@ -280,6 +282,9 @@ static MatrixHandler *sharedHandler = nil; [[AppDelegate theDelegate].masterTabBarController popRoomViewControllerAnimated:NO]; if (clearCache) { + // clear the media cache + [MediaManager clearCache]; + [_mxFileStore deleteAllData]; } @@ -787,7 +792,7 @@ static MatrixHandler *sharedHandler = nil; return nil; } --(NSUInteger) MXCacheSize { +- (NSUInteger) MXCacheSize { if (self.mxFileStore) { return self.mxFileStore.diskUsage; @@ -796,6 +801,10 @@ static MatrixHandler *sharedHandler = nil; return 0; } +- (NSUInteger) cachesSize { + return self.MXCacheSize + [MediaManager cacheSize]; +} + - (CGFloat)getPowerLevel:(MXRoomMember *)roomMember inRoom:(MXRoom *)room { CGFloat powerLevel = 0; diff --git a/matrixConsole/matrixConsole/ViewController/SettingsViewController.m b/matrixConsole/matrixConsole/ViewController/SettingsViewController.m index cd347d6e8..0f97e0844 100644 --- a/matrixConsole/matrixConsole/ViewController/SettingsViewController.m +++ b/matrixConsole/matrixConsole/ViewController/SettingsViewController.m @@ -658,6 +658,7 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl if (self.tableView == aTableView) { // tap on clear application cache if ((indexPath.section == SETTINGS_SECTION_ROOMS_INDEX) && (indexPath.row == SETTINGS_SECTION_ROOMS_CLEAR_CACHE_INDEX)) { + // clear caches [[MatrixHandler sharedHandler] forceInitialSync:YES]; } @@ -807,7 +808,7 @@ NSString* const kCommandsDescriptionText = @"The following commands are availabl cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ClearCacheCell"]; } - cell.textLabel.text = [NSString stringWithFormat:@"Clear cache (%@)", [NSByteCountFormatter stringFromByteCount:[MatrixHandler sharedHandler].MXCacheSize countStyle:NSByteCountFormatterCountStyleFile]]; + cell.textLabel.text = [NSString stringWithFormat:@"Clear cache (%@)", [NSByteCountFormatter stringFromByteCount:[MatrixHandler sharedHandler].cachesSize countStyle:NSByteCountFormatterCountStyleFile]]; ; cell.textLabel.textAlignment = NSTextAlignmentCenter; cell.textLabel.textColor = [AppDelegate theDelegate].masterTabBarController.tabBar.tintColor;