-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTKSServerManager.m
executable file
·128 lines (105 loc) · 5.29 KB
/
STKSServerManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// STKSServerManager.m
// Stacks
//
// Created by Ben Rosen on 10/9/16.
// Copyright © 2016 Juan Carlos Perez & Ben Rosen. All rights reserved.
//
#import "STKSServerManager.h"
#import "UIImage+Optimizations.h"
#import "RXPromise.h"
#import "STKSUtilities.h"
@implementation STKSServerManager
#pragma mark - Shared Instance
+ (instancetype)sharedInstance {
static STKSServerManager *staticInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
staticInstance = [[self alloc] init];
});
return staticInstance;
}
#pragma mark - Initialization
- (instancetype)init {
if (self = [super init]) {
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@"your_identity_pool_key" unauthRoleArn:@"your_unath_role_key" authRoleArn:nil identityProviderManager:nil];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
[AWSLogger defaultLogger].logLevel = AWSLogLevelNone;
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"upload"] withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"Error creating upload folder: %@", error);
}
[[NSFileManager defaultManager] createDirectoryAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"download"] withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"Error creating download folder: %@", error);
}
}
return self;
}
#pragma mark - Class Methods
- (void)downloadImagesWithImageKeys:(NSArray<NSString *>*)imageKeys promise:(RXPromise *)promise {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *imageFileURLs = [NSMutableArray array];
for (int i = 0; i < imageKeys.count; i++) {
NSString *imageName = imageKeys[i];
NSString *imagePath = [NSString stringWithFormat:@"%@/%@.jpg", self.class.temporaryDownloadFilePath, imageName];
AWSS3TransferManagerDownloadRequest *downloadRequest = [[AWSS3TransferManagerDownloadRequest alloc] init];
downloadRequest.bucket = @"picchoosebackend";
downloadRequest.key = imageName;
downloadRequest.downloadingFileURL = [NSURL fileURLWithPath:imagePath];
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[transferManager download:downloadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id _Nullable(AWSTask * _Nonnull task) {
if (task.result) {
[imageFileURLs addObject:imagePath];
if (imageFileURLs.count == imageKeys.count) {
[promise fulfillWithValue:imageFileURLs];
}
} else if (task.error) {
[promise rejectWithReason:task.error];
}
return nil;
}];
}
});
}
- (void)uploadRawImages:(NSArray<UIImage *>*)images promise:(RXPromise *)promise {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *imageURLs = [NSMutableArray array];
for (int i = 0; i < images.count; i++) {
UIImage *image = images[i];
NSString *imageName = [[NSProcessInfo processInfo] globallyUniqueString];
NSString *imagePath = [NSString stringWithFormat:@"%@/%@.jpg", self.class.temporaryUploadFilePath, imageName];
NSData *imageData = UIImageJPEGRepresentation(image, 0.2);
NSError *error = nil;
[imageData writeToFile:imagePath options:NSDataWritingAtomic error:&error];
if (error) {
[promise rejectWithReason:error];
return;
}
AWSS3TransferManagerUploadRequest *uploadRequest = [[AWSS3TransferManagerUploadRequest alloc] init];
uploadRequest.body = [NSURL fileURLWithPath:imagePath];
uploadRequest.key = imageName;
uploadRequest.bucket = @"picchoosebackend";
[[[AWSS3TransferManager defaultS3TransferManager] upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id _Nullable(AWSTask * _Nonnull task) {
if (task.result) {
[imageURLs addObject:imageName];
if (imageURLs.count == images.count) {
[promise fulfillWithValue:imageURLs];
}
} else if (task.error) {
[promise rejectWithReason:task.error];
}
return nil;
}];
}
});
}
+ (NSString *)temporaryDownloadFilePath {
return [NSTemporaryDirectory() stringByAppendingPathComponent:@"download"];
}
+ (NSString *)temporaryUploadFilePath {
return [NSTemporaryDirectory() stringByAppendingPathComponent:@"upload"];
}
@end