diff --git a/Riot/Modules/Communities/Home/GroupHomeViewController.m b/Riot/Modules/Communities/Home/GroupHomeViewController.m index 62397db77..f66f9d39f 100644 --- a/Riot/Modules/Communities/Home/GroupHomeViewController.m +++ b/Riot/Modules/Communities/Home/GroupHomeViewController.m @@ -497,39 +497,73 @@ ]; // Do some sanitisation by handling the potential image + MXWeakify(self); sanitisedGroupLongDescription = [MXKTools sanitiseHTML:_group.summary.profile.longDescription withAllowedHTMLTags:allowedHTMLTags imageHandler:^NSString *(NSString *sourceURL, CGFloat width, CGFloat height) { - NSString *imageURL; + NSString *localSourcePath; if (width != -1 && height != -1) { CGSize size = CGSizeMake(width, height); - imageURL = [self.mxSession.matrixRestClient urlOfContentThumbnail:sourceURL toFitViewSize:size withMethod:MXThumbnailingMethodScale]; + // Build the cache path for the a thumbnail of this image. + NSString *cacheFilePath = [MXMediaManager thumbnailCachePathForMatrixContentURI:sourceURL + andType:nil + inFolder:kMXMediaManagerDefaultCacheFolder + toFitViewSize:size + withMethod:MXThumbnailingMethodScale]; + // Check whether the provided URL is a valid Matrix Content URI. + if (cacheFilePath) + { + localSourcePath = [NSString stringWithFormat:@"file://%@", cacheFilePath]; + + // Download the thumbnail if it is not already stored in the cache. + if (![[NSFileManager defaultManager] fileExistsAtPath:cacheFilePath]) + { + MXStrongifyAndReturnValueIfNil(self, nil); + MXWeakify(self); + [self.mxSession.mediaManager downloadThumbnailFromMatrixContentURI:sourceURL + withType:nil + inFolder:kMXMediaManagerDefaultCacheFolder + toFitViewSize:size + withMethod:MXThumbnailingMethodScale + success:^(NSString *outputFilePath) { + MXStrongifyAndReturnIfNil(self); + [self renderGroupLongDescription]; + } + failure:nil]; + } + } } else { - imageURL = [self.mxSession.matrixRestClient urlOfContent:sourceURL]; - } - - NSString *mimeType = nil; - // Check if the extension could not be deduced from url - if (![imageURL pathExtension].length) - { - // Set default mime type if no information is available - mimeType = @"image/jpeg"; - } - - NSString *cacheFilePath = [MXMediaManager cachePathForMediaWithURL:imageURL andType:mimeType inFolder:kMXMediaManagerDefaultCacheFolder]; - if (![[NSFileManager defaultManager] fileExistsAtPath:cacheFilePath]) - { - [MXMediaManager downloadMediaFromURL:imageURL andSaveAtFilePath:cacheFilePath success:^{ + // Build the cache path for this image. + NSString* cacheFilePath = [MXMediaManager cachePathForMatrixContentURI:sourceURL + andType:nil + inFolder:kMXMediaManagerDefaultCacheFolder]; + + // Check whether the provided URL is a valid Matrix Content URI. + if (cacheFilePath) + { + localSourcePath = [NSString stringWithFormat:@"file://%@", cacheFilePath]; - [self renderGroupLongDescription]; - - } failure:nil]; + // Download the image if it is not already stored in the cache. + if (![[NSFileManager defaultManager] fileExistsAtPath:cacheFilePath]) + { + MXStrongifyAndReturnValueIfNil(self, nil); + MXWeakify(self); + [self.mxSession.mediaManager downloadMediaFromMatrixContentURI:sourceURL + withType:nil + inFolder:kMXMediaManagerDefaultCacheFolder + success:^(NSString *outputFilePath) { + MXStrongifyAndReturnIfNil(self); + [self renderGroupLongDescription]; + } + failure:nil]; + } + } } + return localSourcePath; - return [NSString stringWithFormat:@"file://%@", cacheFilePath]; }]; } else diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index ea535c7c4..390979666 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -2333,7 +2333,7 @@ selectedEvent.sentState == MXEventSentStateSending) { // Upload id is stored in attachment url (nasty trick) - NSString *uploadId = roomBubbleTableViewCell.bubbleData.attachment.actualURL; + NSString *uploadId = roomBubbleTableViewCell.bubbleData.attachment.contentURL; if ([MXMediaManager existingUploaderWithId:uploadId]) { [currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"room_event_action_cancel_send", @"Vector", nil) @@ -2357,7 +2357,7 @@ // Remove the outgoing message and its related cached file. [[NSFileManager defaultManager] removeItemAtPath:roomBubbleTableViewCell.bubbleData.attachment.cacheFilePath error:nil]; - [[NSFileManager defaultManager] removeItemAtPath:roomBubbleTableViewCell.bubbleData.attachment.cacheThumbnailPath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:roomBubbleTableViewCell.bubbleData.attachment.thumbnailCachePath error:nil]; // Cancel and remove the outgoing message [self.roomDataSource.room cancelSendingOperation:selectedEvent.eventId]; @@ -2418,8 +2418,8 @@ // Check whether download is in progress if (level == 0 && selectedEvent.isMediaAttachment) { - NSString *cacheFilePath = roomBubbleTableViewCell.bubbleData.attachment.cacheFilePath; - if ([MXMediaManager existingDownloaderWithOutputFilePath:cacheFilePath]) + NSString *downloadId = roomBubbleTableViewCell.bubbleData.attachment.downloadId; + if ([MXMediaManager existingDownloaderWithIdentifier:downloadId]) { [currentAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedStringFromTable(@"room_event_action_cancel_download", @"Vector", nil) style:UIAlertActionStyleDefault @@ -2432,7 +2432,7 @@ [self cancelEventSelection]; // Get again the loader - MXMediaLoader *loader = [MXMediaManager existingDownloaderWithOutputFilePath:cacheFilePath]; + MXMediaLoader *loader = [MXMediaManager existingDownloaderWithIdentifier:downloadId]; if (loader) { [loader cancel]; diff --git a/RiotShareExtension/Managers/ShareExtensionManager.m b/RiotShareExtension/Managers/ShareExtensionManager.m index 7cae6fac4..541b5892d 100644 --- a/RiotShareExtension/Managers/ShareExtensionManager.m +++ b/RiotShareExtension/Managers/ShareExtensionManager.m @@ -58,7 +58,7 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) sharedInstance.pendingImages = [NSMutableArray array]; sharedInstance.imageUploadProgresses = [NSMutableDictionary dictionary]; - [[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(onMediaUploadProgress:) name:kMXMediaUploadProgressNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(onMediaLoaderStateDidChange:) name:kMXMediaLoaderStateDidChangeNotification object:nil]; // Add observer to handle logout [[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(checkUserAccount) name:kMXKAccountManagerDidRemoveAccountNotification object:nil]; @@ -503,21 +503,30 @@ typedef NS_ENUM(NSInteger, ImageCompressionMode) #pragma mark - Notifications -- (void)onMediaUploadProgress:(NSNotification *)notification +- (void)onMediaLoaderStateDidChange:(NSNotification *)notification { - self.imageUploadProgresses[notification.object] = (NSNumber *)notification.userInfo[kMXMediaLoaderProgressValueKey]; - - if ([self.delegate respondsToSelector:@selector(shareExtensionManager:mediaUploadProgress:)]) - { - const NSInteger totalImagesCount = self.pendingImages.count; - CGFloat totalProgress = 0.0; - - for (NSNumber *progress in self.imageUploadProgresses.allValues) + MXMediaLoader *loader = (MXMediaLoader*)notification.object; + // Consider only upload progress + switch (loader.state) { + case MXMediaLoaderStateUploadInProgress: { - totalProgress += progress.floatValue/totalImagesCount; + self.imageUploadProgresses[loader.uploadId] = (NSNumber *)loader.statisticsDict[kMXMediaLoaderProgressValueKey]; + if ([self.delegate respondsToSelector:@selector(shareExtensionManager:mediaUploadProgress:)]) + { + const NSInteger totalImagesCount = self.pendingImages.count; + CGFloat totalProgress = 0.0; + + for (NSNumber *progress in self.imageUploadProgresses.allValues) + { + totalProgress += progress.floatValue/totalImagesCount; + } + + [self.delegate shareExtensionManager:self mediaUploadProgress:totalProgress]; + } + break; } - - [self.delegate shareExtensionManager:self mediaUploadProgress:totalProgress]; + default: + break; } }