Use SignalAttachment logic in conversation view too

// FREEBIE
pull/1/head
Michael Kirk 7 years ago
parent 7d0acc94ff
commit 031e40d090

@ -62,6 +62,7 @@
#import <JSQSystemSoundPlayer/JSQSystemSoundPlayer.h> #import <JSQSystemSoundPlayer/JSQSystemSoundPlayer.h>
#import <MediaPlayer/MediaPlayer.h> #import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/UTCoreTypes.h> #import <MobileCoreServices/UTCoreTypes.h>
#import <PromiseKit/AnyPromise.h>
#import <SignalMessaging/OWSContactOffersInteraction.h> #import <SignalMessaging/OWSContactOffersInteraction.h>
#import <SignalMessaging/OWSFormat.h> #import <SignalMessaging/OWSFormat.h>
#import <SignalMessaging/OWSUserProfile.h> #import <SignalMessaging/OWSUserProfile.h>
@ -2722,37 +2723,22 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
presentFromViewController:self presentFromViewController:self
canCancel:YES canCancel:YES
backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) { backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) {
AVAsset *video = [AVAsset assetWithURL:movieURL]; DataSource *dataSource = [DataSourcePath dataSourceWithURL:movieURL];
AVAssetExportSession *exportSession = dataSource.sourceFilename = filename;
[AVAssetExportSession exportSessionWithAsset:video VideoCompressionResult *compressionResult =
presetName:AVAssetExportPresetMediumQuality]; [SignalAttachment compressVideoAsMp4WithDataSource:dataSource
exportSession.shouldOptimizeForNetworkUse = YES; dataUTI:(NSString *)kUTTypeMPEG4];
exportSession.outputFileType = AVFileTypeMPEG4; [compressionResult.attachmentPromise retainUntilComplete];
NSURL *compressedVideoUrl = [[self videoTempFolder]
URLByAppendingPathComponent:[[[NSUUID UUID] UUIDString] compressionResult.attachmentPromise.then(^(SignalAttachment *attachment) {
stringByAppendingPathExtension:@"mp4"]];
exportSession.outputURL = compressedVideoUrl;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
OWSAssert([attachment isKindOfClass:[SignalAttachment class]]);
if (modalActivityIndicator.wasCancelled) { if (modalActivityIndicator.wasCancelled) {
return; return;
} }
[modalActivityIndicator dismissWithCompletion:^{ [modalActivityIndicator dismissWithCompletion:^{
NSString *baseFilename = filename.stringByDeletingPathExtension;
NSString *mp4Filename = [baseFilename stringByAppendingPathExtension:@"mp4"];
DataSource *_Nullable dataSource =
[DataSourcePath dataSourceWithURL:compressedVideoUrl];
[dataSource setSourceFilename:mp4Filename];
// Remove temporary file when complete.
[dataSource setShouldDeleteOnDeallocation];
SignalAttachment *attachment =
[SignalAttachment attachmentWithDataSource:dataSource
dataUTI:(NSString *)kUTTypeMPEG4];
if (!attachment || [attachment hasError]) { if (!attachment || [attachment hasError]) {
DDLogError(@"%@ %s Invalid attachment: %@.", DDLogError(@"%@ %s Invalid attachment: %@.",
self.logTag, self.logTag,
@ -2760,13 +2746,11 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
attachment ? [attachment errorName] : @"Missing data"); attachment ? [attachment errorName] : @"Missing data");
[self showErrorAlertForAttachment:attachment]; [self showErrorAlertForAttachment:attachment];
} else { } else {
[self tryToSendAttachmentIfApproved:attachment [self tryToSendAttachmentIfApproved:attachment skipApprovalDialog:skipApprovalDialog];
skipApprovalDialog:skipApprovalDialog];
} }
}]; }];
}); });
}]; }];
}];
} }

@ -866,6 +866,27 @@ public class SignalAttachment: NSObject {
return (promise, exportSession) return (promise, exportSession)
} }
@objc
public class VideoCompressionResult: NSObject {
@objc
public let attachmentPromise: AnyPromise
@objc
public let exportSession: AVAssetExportSession?
fileprivate init(attachmentPromise: Promise<SignalAttachment>, exportSession: AVAssetExportSession?) {
self.attachmentPromise = AnyPromise(attachmentPromise)
self.exportSession = exportSession
super.init()
}
}
@objc
public class func compressVideoAsMp4(dataSource: DataSource, dataUTI: String) -> VideoCompressionResult {
let (attachmentPromise, exportSession) = compressVideoAsMp4(dataSource: dataSource, dataUTI: dataUTI)
return VideoCompressionResult(attachmentPromise: attachmentPromise, exportSession: exportSession)
}
public class func isInvalidVideo(dataSource: DataSource, dataUTI: String) -> Bool { public class func isInvalidVideo(dataSource: DataSource, dataUTI: String) -> Bool {
guard videoUTISet.contains(dataUTI) else { guard videoUTISet.contains(dataUTI) else {
// not a video // not a video

@ -4,6 +4,21 @@
import PromiseKit import PromiseKit
public extension AnyPromise {
/**
* Sometimes there isn't a straight forward candidate to retain a promise, in that case we tell the
* promise to self retain, until it completes to avoid the risk it's GC'd before completion.
*/
func retainUntilComplete() {
// Unfortunately, there is (currently) no way to surpress the
// compiler warning: "Variable 'retainCycle' was written to, but never read"
var retainCycle: AnyPromise? = self
self.always {
retainCycle = nil
}
}
}
public extension Promise { public extension Promise {
/** /**
* Sometimes there isn't a straight forward candidate to retain a promise, in that case we tell the * Sometimes there isn't a straight forward candidate to retain a promise, in that case we tell the

Loading…
Cancel
Save