Signal-iOS/SignalServiceKit/src/Network/API/OWSUploadOperation.m
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

172 lines
6.1 KiB
Objective-C

//
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "OWSUploadOperation.h"
#import "HTTPUtils.h"
#import "MIMETypeUtil.h"
#import "OWSDispatch.h"
#import "OWSError.h"
#import "OWSOperation.h"
#import "OWSUpload.h"
#import "SSKEnvironment.h"
#import "TSAttachmentStream.h"
#import <SignalCoreKit/Cryptography.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
NSString *const kAttachmentUploadProgressNotification = @"kAttachmentUploadProgressNotification";
NSString *const kAttachmentUploadProgressKey = @"kAttachmentUploadProgressKey";
NSString *const kAttachmentUploadAttachmentIDKey = @"kAttachmentUploadAttachmentIDKey";
@interface OWSUploadOperation ()
@property (readonly, nonatomic) NSString *attachmentId;
@property (readonly, nonatomic) BOOL canUseV3;
@property (readonly, nonatomic) NSArray<NSString *> *messageIds;
@property (nonatomic, nullable) TSAttachmentStream *completedUpload;
@end
#pragma mark -
@implementation OWSUploadOperation
+ (NSOperationQueue *)uploadQueue
{
static NSOperationQueue *operationQueue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
operationQueue = [NSOperationQueue new];
operationQueue.name = @"Uploads";
// TODO: Tune this limit.
operationQueue.maxConcurrentOperationCount = CurrentAppContext().isNSE ? 2 : 8;
});
return operationQueue;
}
#pragma mark -
- (instancetype)initWithAttachmentId:(NSString *)attachmentId
messageIds:(NSArray<NSString *> *)messageIds
canUseV3:(BOOL)canUseV3
{
self = [super init];
if (!self) {
return self;
}
self.remainingRetries = 4;
_attachmentId = attachmentId;
_canUseV3 = canUseV3;
_messageIds = messageIds;
return self;
}
- (NetworkManager *)networkManager
{
return SSKEnvironment.shared.networkManager;
}
- (void)run
{
__block TSAttachmentStream *_Nullable attachmentStream;
[self.databaseStorage readWithBlock:^(SDSAnyReadTransaction *transaction) {
attachmentStream =
[TSAttachmentStream anyFetchAttachmentStreamWithUniqueId:self.attachmentId transaction:transaction];
if (attachmentStream == nil) {
// Message may have been removed.
OWSLogWarn(@"Missing attachment.");
return;
}
}];
if (!attachmentStream) {
OWSProdError([OWSAnalyticsEvents messageSenderErrorCouldNotLoadAttachment]);
// Not finding local attachment is a terminal failure.
NSError *error = [OWSUnretryableError asNSError];
[self reportError:error];
return;
}
if (attachmentStream.isUploaded) {
OWSLogDebug(@"Attachment previously uploaded.");
self.completedUpload = attachmentStream;
[self reportSuccess];
return;
}
[self fireNotificationWithProgress:0];
OWSAttachmentUploadV2 *upload = [[OWSAttachmentUploadV2 alloc] initWithAttachmentStream:attachmentStream
canUseV3:self.canUseV3];
[BlurHash ensureBlurHashForAttachmentStream:attachmentStream]
.catchInBackground(^(NSError *error) {
// Swallow these errors; blurHashes are strictly optional.
OWSLogWarn(@"Error generating blurHash.");
})
.thenInBackground(^(id value) {
return [upload uploadWithProgressBlock:^(
NSProgress *uploadProgress) { [self fireNotificationWithProgress:uploadProgress.fractionCompleted]; }];
})
.doneInBackground(^(id value) {
DatabaseStorageWrite(self.databaseStorage, ^(SDSAnyWriteTransaction *transaction) {
[attachmentStream updateAsUploadedWithEncryptionKey:upload.encryptionKey
digest:upload.digest
serverId:upload.serverId
cdnKey:upload.cdnKey
cdnNumber:upload.cdnNumber
uploadTimestamp:upload.uploadTimestamp
transaction:transaction];
for (NSString *messageId in self.messageIds) {
TSInteraction *_Nullable interaction = [TSInteraction anyFetchWithUniqueId:messageId
transaction:transaction];
if (interaction == nil) {
OWSLogWarn(@"Missing interaction.");
continue;
}
[self.databaseStorage touchInteraction:interaction shouldReindex:false transaction:transaction];
}
});
self.completedUpload = attachmentStream;
[self reportSuccess];
})
.catchInBackground(^(NSError *error) {
OWSLogError(@"Failed: %@", error);
if (error.httpStatusCode.intValue == 413) {
OWSFailDebug(@"Request entity too large: %@.", @(attachmentStream.byteCount));
[self reportError:[OWSUnretryableMessageSenderError asNSError]];
} else if (error.isNetworkConnectivityFailure) {
[self reportError:error];
} else {
OWSFailDebug(@"Unexpected error: %@", error);
[self reportError:error];
}
});
}
- (void)fireNotificationWithProgress:(CGFloat)progress
{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter postNotificationNameAsync:kAttachmentUploadProgressNotification
object:nil
userInfo:@{
kAttachmentUploadProgressKey : @(progress),
kAttachmentUploadAttachmentIDKey : self.attachmentId
}];
}
@end
NS_ASSUME_NONNULL_END