forked from owncloud/ios-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileProviderExtensionThumbnailRequest.m
180 lines (142 loc) · 4.92 KB
/
FileProviderExtensionThumbnailRequest.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// FileProviderExtensionThumbnailRequest.m
// ownCloud File Provider
//
// Created by Felix Schwarz on 09.06.18.
// Copyright © 2018 ownCloud GmbH. All rights reserved.
//
/*
* Copyright (C) 2018, ownCloud GmbH.
*
* This code is covered by the GNU Public License Version 3.
*
* For distribution utilizing Apple mechanisms please see https://owncloud.org/contribute/iOS-license-exception/
* You should have received a copy of this license along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*/
#import "FileProviderExtensionThumbnailRequest.h"
#import "NSError+MessageResolution.h"
@interface FileProviderExtensionThumbnailRequest ()
{
BOOL _isDone;
__weak OCResourceRequestItemThumbnail *_thumbnailRequest;
}
@end
@implementation FileProviderExtensionThumbnailRequest
- (void)dealloc
{
OCLogDebug(@"Dealloc %@", self);
}
- (void)requestNextThumbnail
{
if (_isDone)
{
return;
}
if (self.progress.cancelled)
{
OCLogDebug(@"Thumbnail request cancelled by the system");
[self completedRequestWithError:nil];
return;
}
if (self.cursorPosition < self.itemIdentifiers.count)
{
NSFileProviderItemIdentifier itemIdentifier = self.itemIdentifiers[self.cursorPosition];
OCLogDebug(@"Retrieving %ld / %ld:", self.cursorPosition, self.itemIdentifiers.count);
self.cursorPosition += 1;
OCCore *core;
if ((core = self.extension.core) != nil)
{
OCLocalID localID = (OCLocalID)itemIdentifier;
// Translate item identifiers
OCVaultLocation *location;
if ((location = [[OCVaultLocation alloc] initWithVFSItemID:itemIdentifier]) != nil)
{
if (location.localID != nil)
{
localID = location.localID;
}
}
[core retrieveItemFromDatabaseForLocalID:localID completionHandler:^(NSError *error, OCSyncAnchor syncAnchor, OCItem *itemFromDatabase) {
OCLogDebug(@"Retrieving %ld: %@", self.cursorPosition-1, itemFromDatabase.name);
OCResourceManager *resourceManager = core.vault.resourceManager;
if ((itemFromDatabase.type == OCItemTypeCollection) || // No previews for folders
(itemFromDatabase.thumbnailAvailability == OCItemThumbnailAvailabilityNone) || // No thumbnails available for this type
(resourceManager == nil)) // No ResourceManager available for this core
{
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
self.perThumbnailCompletionHandler(itemIdentifier, nil, nil);
OCLogDebug(@"Replied %ld: %@ -> none available", self.cursorPosition-1, itemFromDatabase.name);
[self requestNextThumbnail];
});
}
else
{
OCResourceRequestItemThumbnail *thumbnailRequest = [OCResourceRequestItemThumbnail requestThumbnailFor:itemFromDatabase maximumSize:self.sizeInPixels scale:1.0 waitForConnectivity:NO changeHandler:^(OCResourceRequest * _Nonnull request, NSError * _Nullable error, BOOL isOngoing, OCResource * _Nullable previousResource, OCResource * _Nullable newResource) {
OCLogDebug(@"Retrieved %ld: %@ -> %d", self.cursorPosition-1, itemFromDatabase.name, isOngoing);
if (!isOngoing)
{
OCResourceImage *thumbnailResource = OCTypedCast(newResource, OCResourceImage);
OCItemThumbnail *thumbnail = thumbnailResource.thumbnail;
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
NSError *returnError = (thumbnail==nil) ?
((error != nil) ? error.translatedError : nil) :
nil;
self.perThumbnailCompletionHandler(itemIdentifier, thumbnail.data, returnError);
OCLogDebug(@"Replied %ld: %@ -> thumbnailData=%d, error=%@", self.cursorPosition-1, itemFromDatabase.name, (thumbnail.data != nil), returnError);
[self requestNextThumbnail];
});
}
}];
[resourceManager startRequest:thumbnailRequest];
self->_thumbnailRequest = thumbnailRequest;
}
}];
}
else
{
OCLogDebug(@"Stopping thumbnail retrieval due to lack of core");
[self completedRequestWithError:OCError(OCErrorInternal)];
}
}
else
{
OCLogDebug(@"Done retrieving %ld / %ld", self.cursorPosition, self.itemIdentifiers.count);
[self completedRequestWithError:nil];
}
}
- (void)completedRequestWithError:(NSError *)error
{
if (!_isDone)
{
_isDone = YES;
dispatch_async(dispatch_get_main_queue(), ^{
self.completionHandler(nil);
});
}
}
- (void)setProgress:(NSProgress *)progress
{
__weak FileProviderExtensionThumbnailRequest *weakSelf = self;
progress.cancellationHandler = ^{
FileProviderExtensionThumbnailRequest *strongSelf;
if ((strongSelf = weakSelf) != nil)
{
OCResourceRequestItemThumbnail *thumbnailRequest;
if ((thumbnailRequest = strongSelf->_thumbnailRequest) != nil)
{
[strongSelf.extension.core.vault.resourceManager stopRequest:thumbnailRequest];
}
strongSelf->_thumbnailRequest = nil;
}
};
}
- (nonnull NSArray<OCLogTagName> *)logTags
{
return (@[@"FPThumbs"]);
}
+ (nonnull NSArray<OCLogTagName> *)logTags
{
return (@[@"FPThumbs"]);
}
@end