From a96c6ed3b2345bb07155f9a23b74b9d55d7dcc58 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 12 Dec 2018 10:37:52 -0500 Subject: [PATCH] Persist the media validity cache. --- .../Messages/Attachments/TSAttachmentStream.m | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m index e009b0894..64ee5b761 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m @@ -355,25 +355,55 @@ typedef void (^OWSLoadedThumbnailSuccess)(OWSLoadedThumbnail *loadedThumbnail); { OWSAssertDebug(self.isImage || self.isAnimated); + BOOL result; + BOOL didUpdateCache = NO; @synchronized(self) { if (!self.isValidImageCached) { - self.isValidImageCached = - @([NSData ows_isValidImageAtPath:self.originalFilePath mimeType:self.contentType]); + OWSLogVerbose(@"Updating isValidImageCached."); + self.isValidImageCached = @([NSData ows_isValidImageAtPath:self.originalFilePath + mimeType:self.contentType]); + didUpdateCache = YES; } - return self.isValidImageCached.boolValue; + result = self.isValidImageCached.boolValue; } + + if (didUpdateCache) { + [self.dbReadWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [self applyChangeToSelfAndLatestCopy:transaction + changeBlock:^(TSAttachmentStream *attachmentStream) { + attachmentStream.isValidImageCached = @(result); + }]; + }]; + } + + return result; } - (BOOL)isValidVideo { - OWSAssertDebug(self.isVideo); + OWSAssertDebug(self.isImage || self.isAnimated); + BOOL result; + BOOL didUpdateCache = NO; @synchronized(self) { if (!self.isValidVideoCached) { + OWSLogVerbose(@"Updating isValidVideoCached."); self.isValidVideoCached = @([OWSMediaUtils isValidVideoWithPath:self.originalFilePath]); + didUpdateCache = YES; } - return self.isValidVideoCached.boolValue; + result = self.isValidVideoCached.boolValue; } + + if (didUpdateCache) { + [self.dbReadWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [self applyChangeToSelfAndLatestCopy:transaction + changeBlock:^(TSAttachmentStream *attachmentStream) { + attachmentStream.isValidVideoCached = @(result); + }]; + }]; + } + + return result; } #pragma mark -