forked from owncloud/ios-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOCItem+FileProviderItem.m
415 lines (328 loc) · 11.5 KB
/
OCItem+FileProviderItem.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// OCItem+FileProviderItem.m
// ownCloud File Provider
//
// Created by Felix Schwarz on 08.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 <CoreServices/CoreServices.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#import "OCItem+FileProviderItem.h"
#import "NSError+MessageResolution.h"
#if OC_FEATURE_AVAILABLE_FILEPROVIDER
static NSMutableDictionary<OCLocalID, NSError *> *sOCItemUploadingErrors;
@implementation OCItem (FileProviderItem)
@dynamic filename;
// TODO: implement an initializer to create an item from your extension's backing model
// TODO: implement the accessors to return the values from your extension's backing model
- (NSFileProviderItemIdentifier)itemIdentifier
{
OCVFSItemID vfsItemID = self.vfsItemID;
return ([vfsItemID isEqual:OCVFSItemIDRoot] ? NSFileProviderRootContainerItemIdentifier : vfsItemID);
}
- (NSFileProviderItemIdentifier)parentItemIdentifier
{
OCVFSItemID vfsParentItemID = self.vfsParentItemID;
return ([vfsParentItemID isEqual:OCVFSItemIDRoot] ? NSFileProviderRootContainerItemIdentifier : vfsParentItemID);
}
- (NSString *)filename
{
return (self.vfsItemName);
}
+ (NSDictionary<NSString*, NSString*> *)overriddenUTIByMIMEType
{
static dispatch_once_t onceToken;
static NSDictionary<NSString *, NSString *> *utiByMIMEType;
dispatch_once(&onceToken, ^{
utiByMIMEType = @{
@"application/vnd.oasis.opendocument.text" : @"org.oasis-open.opendocument.text",
@"application/vnd.oasis.opendocument.text-template" : @"org.oasis-open.opendocument.text-template",
@"application/vnd.oasis.opendocument.graphics" : @"org.oasis-open.opendocument.graphics",
@"application/vnd.oasis.opendocument.graphics-template" : @"org.oasis-open.opendocument.graphics-template",
@"application/vnd.oasis.opendocument.presentation" : @"org.oasis-open.opendocument.presentation",
@"application/vnd.oasis.opendocument.presentation-template" : @"org.oasis-open.opendocument.presentation-template",
@"application/vnd.oasis.opendocument.spreadsheet" : @"org.oasis-open.opendocument.spreadsheet",
@"application/vnd.oasis.opendocument.spreadsheet-template" : @"org.oasis-open.opendocument.spreadsheet-template",
@"application/vnd.oasis.opendocument.formula" : @"org.oasis-open.opendocument.formula",
@"application/vnd.oasis.opendocument.formula-template" : @"org.oasis-open.opendocument.formula-template",
/*
These MIME-Types aren't correctly mapped by iOS, so they're hardcoded. Reference:
- OC10 suffix -> MIMEType map: https://github.com/owncloud/core/blob/master/resources/config/mimetypemapping.dist.json
- Apple UTI reference table: https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
*/
@"application/illustrator" : @"com.adobe.illustrator.ai-image",
@"application/x-perl" : @"public.perl-script",
@"application/x-php" : @"public.php-script",
@"text/x-python" : @"public.python-script",
@"text/x-c" : @"public.c-source",
@"text/x-c++src" : @"public.c-plus-plus-source",
@"text/x-h" : @"public.c-header",
@"text/markdown" : @"net.daringfireball.markdown",
@"text/x-shellscript" : @"public.shell-script",
@"text/x-java-source" : @"com.sun.java-source"
// @"audio/ogg" : @"org.xiph.oga",
// @"video/ogg" : @"org.xiph.ogv"
};
});
return (utiByMIMEType);
}
+ (NSDictionary<NSString*, NSString*> *)overriddenUTIBySuffix
{
static dispatch_once_t onceToken;
static NSDictionary<NSString *, NSString *> *utiBySuffix;
dispatch_once(&onceToken, ^{
utiBySuffix = @{
// These suffix -> UTI mappings are currently not needed as the OC server
// already returns correct MIME-Types for these, so the MIMEType -> UTI
// mapping already takes care of it. Decided to let them still stay here
// as a reference and to document the thinking behind not including these
// five file types in this conversion dictionary.
// @"odt" : @"org.oasis-open.opendocument.text",
// @"ott" : @"org.oasis-open.opendocument.text-template",
// @"odg" : @"org.oasis-open.opendocument.graphics",
// @"otg" : @"org.oasis-open.opendocument.graphics-template",
// @"odp" : @"org.oasis-open.opendocument.presentation",
// @"otp" : @"org.oasis-open.opendocument.presentation-template",
// @"ods" : @"org.oasis-open.opendocument.spreadsheet",
// @"ots" : @"org.oasis-open.opendocument.spreadsheet-template",
// @"odf" : @"org.oasis-open.opendocument.formula",
// @"otf" : @"org.oasis-open.opendocument.formula-template",
// OC server does not seem to return MIME Types for these types
// at the time of writing, so these entries take care of correctly
// mapping suffixes to UTIs
@"odc" : @"org.oasis-open.opendocument.chart",
@"otc" : @"org.oasis-open.opendocument.chart-template",
@"odi" : @"org.oasis-open.opendocument.image",
@"oti" : @"org.oasis-open.opendocument.image-template",
@"odm" : @"org.oasis-open.opendocument.text-master",
@"oth" : @"org.oasis-open.opendocument.text-web",
@"m" : @"public.objective-c-source",
@"mindnode" : @"com.mindnode.mindnode.mindmap",
@"itmz" : @"com.toketaware.uti.ithoughts.itmz",
@"pdf" : @"com.adobe.pdf"
};
});
return (utiBySuffix);
}
- (UTType *)contentType
{
UTType *uti = nil;
// Return special UTI type for folders
if (self.type == OCItemTypeCollection)
{
return (UTTypeFolder);
}
// Workaround for broken MIMEType->UTI conversions
if (uti == nil)
{
// Override by MIMEType
if (self.mimeType != nil)
{
NSString *overrideType;
if ((overrideType = OCItem.overriddenUTIByMIMEType[self.mimeType]) != nil)
{
uti = [UTType typeWithIdentifier:overrideType];
OCLogVerbose(@"Mapped %@ MIMEType %@ to UTI %@", self.name, self.mimeType, uti);
}
}
}
if (uti == nil)
{
NSString *suffix;
// Override by suffix
if ((suffix = self.name.pathExtension.lowercaseString) != nil)
{
NSString *overrideType;
if ((overrideType = OCItem.overriddenUTIBySuffix[suffix]) != nil)
{
uti = [UTType typeWithIdentifier:overrideType];
OCLogVerbose(@"Mapped %@ suffix %@ to UTI %@", self.name, suffix, uti);
}
}
}
// Convert MIME type to UTI type identifier
if (uti == nil)
{
if (self.mimeType != nil)
{
uti = [UTType typeWithMIMEType:self.mimeType];
}
if (uti == nil)
{
uti = UTTypeData;
}
OCLogVerbose(@"Converted %@ MIMEType %@ to UTI %@", self.name, self.mimeType, uti);
}
// Reject "dyn.*" types
if (uti.isDynamic)
{
// Use generic data UTI instead
// Rationale: https://github.com/owncloud/ios-app/issues/747#issuecomment-689797261
uti = UTTypeData;
OCLogVerbose(@"Rejected dynamic %@ UTI for %@, using %@ instead", self.name, self.mimeType, uti);
}
return (uti);
}
- (NSFileProviderItemCapabilities)capabilities
{
OCItemPermissions permissions = self.permissions;
BOOL fileReadable = (self.state == OCItemStateNormal) || (self.localRelativePath != nil);
switch (self.type)
{
case OCItemTypeFile:
return (
(fileReadable ? NSFileProviderItemCapabilitiesAllowsReading : 0) |
((permissions & OCItemPermissionWritable) ? NSFileProviderItemCapabilitiesAllowsWriting : 0) |
((permissions & OCItemPermissionMove) ? NSFileProviderItemCapabilitiesAllowsReparenting : 0) |
((permissions & OCItemPermissionRename) ? NSFileProviderItemCapabilitiesAllowsRenaming : 0) |
((permissions & OCItemPermissionDelete) ? NSFileProviderItemCapabilitiesAllowsDeleting : 0)
);
break;
case OCItemTypeCollection:
return (NSFileProviderItemCapabilitiesAllowsContentEnumerating |
((permissions & OCItemPermissionMove) ? NSFileProviderItemCapabilitiesAllowsReparenting : 0) |
((permissions & OCItemPermissionRename) ? NSFileProviderItemCapabilitiesAllowsRenaming : 0) |
((permissions & OCItemPermissionDelete) ? NSFileProviderItemCapabilitiesAllowsDeleting : 0) |
((permissions & (OCItemPermissionCreateFile|OCItemPermissionCreateFolder)) ? NSFileProviderItemCapabilitiesAllowsAddingSubItems : 0)
);
break;
}
return (NSFileProviderItemCapabilitiesAllowsContentEnumerating); // previously NSFileProviderItemCapabilitiesAllowsAll, but since it shouldn't be used anyway…
}
- (NSData *)versionIdentifier
{
return ([[NSString stringWithFormat:@"%@_:_%@", self.eTag, self.fileID] dataUsingEncoding:NSUTF8StringEncoding]);
}
- (NSNumber *)documentSize
{
if (self.type == OCItemTypeFile)
{
return (@(self.size));
}
return (nil);
}
- (BOOL)isDownloading
{
return ((self.syncActivity & OCItemSyncActivityDownloading) == OCItemSyncActivityDownloading);
}
- (BOOL)isDownloaded
{
if (self.localRelativePath != nil)
{
return (YES);
}
if (self.type == OCItemTypeCollection)
{
// Needs to return YES for folders in order to allow browsing while offline
// Otherwise Files.app will bring up an alert "You're not connected to the Internet"
// (big thanks to @palmin who pointed me to this possibility)
return (YES);
}
return (NO);
}
- (BOOL)isUploading
{
return ((self.syncActivity & OCItemSyncActivityUploading) == OCItemSyncActivityUploading);
}
- (BOOL)isUploaded
{
if (![self isUploading])
{
return (!self.locallyModified);
}
return (NO);
}
- (BOOL)isMostRecentVersionDownloaded
{
if (((self.localRelativePath != nil) && (self.remoteItem == nil)) || self.isUploading)
{
return (YES);
}
return (NO);
}
//- (BOOL)respondsToSelector:(SEL)aSelector
//{
// OCLogDebug(@"Probing for %@", NSStringFromSelector(aSelector));
//
// return ([super respondsToSelector:aSelector]);
//}
//
- (NSDate *)contentModificationDate
{
return (self.lastModified);
}
- (NSNumber *)childItemCount
{
if (self.type == OCItemTypeFile)
{
return (@(0));
}
return (nil);
}
- (void)setLocalFavoriteRank:(NSNumber *)localFavoriteRank
{
[self setValue:localFavoriteRank forLocalAttribute:OCLocalAttributeFavoriteRank];
}
- (NSNumber *)favoriteRank
{
NSNumber *favoriteRank = [self valueForLocalAttribute:OCLocalAttributeFavoriteRank];
// NSNumber *favoriteRank = nil;
//
// if (self.isFavorite)
// {
// if ((favoriteRank = [self valueForLocalAttribute:OCLocalAttributeFavoriteRank]) == nil)
// {
// favoriteRank = @(NSFileProviderFavoriteRankUnranked);
// }
// }
return (favoriteRank);
}
- (void)setLocalTagData:(NSData *)localTagData
{
[self setValue:localTagData forLocalAttribute:OCLocalAttributeTagData];
}
- (NSData *)tagData
{
return ([self valueForLocalAttribute:OCLocalAttributeTagData]);
}
- (NSError *)uploadingError
{
if (self.localID != nil)
{
if (self.isPlaceholder)
{
NSLog(@"Request uploadingError for %@", self.localID);
}
@synchronized ([OCItem class])
{
return (sOCItemUploadingErrors[self.localID]);
}
}
return (nil);
}
- (void)setUploadingError:(NSError *)uploadingError
{
NSLog(@"Set uploadingError for %@ to %@", self.localID, uploadingError);
if (self.localID != nil)
{
@synchronized ([OCItem class])
{
if (sOCItemUploadingErrors == nil)
{
sOCItemUploadingErrors = [NSMutableDictionary new];
}
sOCItemUploadingErrors[self.localID] = [uploadingError translatedError];
}
}
}
@end
#endif /* OC_FEATURE_AVAILABLE_FILEPROVIDER */