Signal-iOS/SignalServiceKit/src/Network/API/OWSUpload.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

122 lines
4.3 KiB
Objective-C

//
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "OWSUpload.h"
#import "HTTPUtils.h"
#import "MIMETypeUtil.h"
#import "OWSError.h"
#import "OWSRequestFactory.h"
#import "SSKEnvironment.h"
#import "TSAttachmentStream.h"
#import <SignalCoreKit/Cryptography.h>
#import <SignalCoreKit/NSData+OWS.h>
#import <SignalCoreKit/NSDate+OWS.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
// See: https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-UsingHTTPPOST.html
@implementation OWSUploadFormV2
- (instancetype)initWithAcl:(NSString *)acl
key:(NSString *)key
policy:(NSString *)policy
algorithm:(NSString *)algorithm
credential:(NSString *)credential
date:(NSString *)date
signature:(NSString *)signature
attachmentId:(nullable NSNumber *)attachmentId
attachmentIdString:(nullable NSString *)attachmentIdString
{
self = [super init];
if (self) {
_acl = acl;
_key = key;
_policy = policy;
_algorithm = algorithm;
_credential = credential;
_date = date;
_signature = signature;
_attachmentId = attachmentId;
_attachmentIdString = attachmentIdString;
}
return self;
}
+ (nullable OWSUploadFormV2 *)parseDictionary:(nullable NSDictionary *)formResponseObject
{
if (![formResponseObject isKindOfClass:[NSDictionary class]]) {
OWSFailDebug(@"Invalid upload form.");
return nil;
}
NSDictionary *responseMap = formResponseObject;
NSString *_Nullable formAcl = responseMap[@"acl"];
if (![formAcl isKindOfClass:[NSString class]] || formAcl.length < 1) {
OWSFailDebug(@"Invalid upload form: acl.");
return nil;
}
NSString *_Nullable formKey = responseMap[@"key"];
if (![formKey isKindOfClass:[NSString class]] || formKey.length < 1) {
OWSFailDebug(@"Invalid upload form: key.");
return nil;
}
NSString *_Nullable formPolicy = responseMap[@"policy"];
if (![formPolicy isKindOfClass:[NSString class]] || formPolicy.length < 1) {
OWSFailDebug(@"Invalid upload form: policy.");
return nil;
}
NSString *_Nullable formAlgorithm = responseMap[@"algorithm"];
if (![formAlgorithm isKindOfClass:[NSString class]] || formAlgorithm.length < 1) {
OWSFailDebug(@"Invalid upload form: algorithm.");
return nil;
}
NSString *_Nullable formCredential = responseMap[@"credential"];
if (![formCredential isKindOfClass:[NSString class]] || formCredential.length < 1) {
OWSFailDebug(@"Invalid upload form: credential.");
return nil;
}
NSString *_Nullable formDate = responseMap[@"date"];
if (![formDate isKindOfClass:[NSString class]] || formDate.length < 1) {
OWSFailDebug(@"Invalid upload form: date.");
return nil;
}
NSString *_Nullable formSignature = responseMap[@"signature"];
if (![formSignature isKindOfClass:[NSString class]] || formSignature.length < 1) {
OWSFailDebug(@"Invalid upload form: signature.");
return nil;
}
NSNumber *_Nullable attachmentId = responseMap[@"attachmentId"];
if (attachmentId == nil) {
// This value is optional.
} else if (![attachmentId isKindOfClass:[NSNumber class]]) {
OWSFailDebug(@"Invalid upload form: attachmentId.");
return nil;
}
NSString *_Nullable attachmentIdString = responseMap[@"attachmentIdString"];
if (attachmentIdString == nil) {
// This value is optional.
} else if (![attachmentIdString isKindOfClass:[NSString class]] || attachmentIdString.length < 1) {
OWSFailDebug(@"Invalid upload form: attachmentIdString.");
return nil;
}
return [[OWSUploadFormV2 alloc] initWithAcl:formAcl
key:formKey
policy:formPolicy
algorithm:formAlgorithm
credential:formCredential
date:formDate
signature:formSignature
attachmentId:attachmentId
attachmentIdString:attachmentIdString];
}
@end
NS_ASSUME_NONNULL_END