|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#import "DHImageAtURLPatch.h" |
| 11 | +#import "NSArray+FBAdditions.h" |
| 12 | +#import "NSURL+FBAdditions.h" |
| 13 | +#import "QCPatch+FBAdditions.h" |
| 14 | +#import "NSDocument+FBAdditions.h" |
| 15 | + |
| 16 | +@interface DHImageAtURLPatch (Private) |
| 17 | +- (void)_downloadImageAtURL:(NSString *)URL; |
| 18 | +- (void)_updateOutputPorts; |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation DHImageAtURLPatch |
| 22 | + |
| 23 | +static NSMutableDictionary *_downloadedImages; |
| 24 | + |
| 25 | +- (id)initWithIdentifier:(id)identifier { |
| 26 | + if (self = [super initWithIdentifier:identifier]) { |
| 27 | + inputUseCache.booleanValue = YES; |
| 28 | + _stillDownloading = [[NSMutableSet alloc] init]; |
| 29 | + if (!_downloadedImages) { |
| 30 | + _downloadedImages = [[NSMutableDictionary alloc] init]; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return self; |
| 35 | +} |
| 36 | + |
| 37 | ++ (QCPatchExecutionMode)executionModeWithIdentifier:(id)identifier { |
| 38 | + return kQCPatchExecutionModeProcessor; |
| 39 | +} |
| 40 | + |
| 41 | ++ (BOOL)allowsSubpatchesWithIdentifier:(id)identifier { |
| 42 | + return NO; |
| 43 | +} |
| 44 | + |
| 45 | ++ (QCPatchTimeMode)timeModeWithIdentifier:(id)identifier { |
| 46 | + return kQCPatchTimeModeNone; |
| 47 | +} |
| 48 | + |
| 49 | +- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments { |
| 50 | + if (!(inputURLOrURLs.wasUpdated || inputUseCache.wasUpdated)) { |
| 51 | + return YES; |
| 52 | + } |
| 53 | + |
| 54 | + id inputValue = inputURLOrURLs.rawValue; |
| 55 | + if ([inputValue isKindOfClass:[NSString class]]) { |
| 56 | + _URLs = @{@(0): inputValue}; |
| 57 | + } else if ([inputValue isKindOfClass:[QCStructure class]]) { |
| 58 | + _URLs = [inputValue dictionaryRepresentation]; |
| 59 | + } else { |
| 60 | + _URLs = @{}; |
| 61 | + } |
| 62 | + |
| 63 | + [_stillDownloading removeAllObjects]; |
| 64 | + [self _updateOutputPorts]; |
| 65 | + |
| 66 | + for (id key in _URLs) { |
| 67 | + NSString *URL = _URLs[key]; |
| 68 | + if (URL && (inputUseCache.booleanValue == NO || _downloadedImages[URL] == nil)) { |
| 69 | + [_stillDownloading addObject:URL]; |
| 70 | + [NSThread detachNewThreadSelector:@selector(_downloadImageAtURL:) toTarget:self withObject:URL]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return YES; |
| 75 | +} |
| 76 | + |
| 77 | +@end |
| 78 | + |
| 79 | +@implementation DHImageAtURLPatch (Private) |
| 80 | + |
| 81 | +- (void)_downloadImageAtURL:(NSString *)URL { |
| 82 | + if (!(URL && [URL isKindOfClass:[NSString class]])) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + NSImage *image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithQuartzComposerLocation:URL relativeToDocument:self.fb_document]]; |
| 87 | + _downloadedImages[URL] = image ? image : [NSNull null]; |
| 88 | + [_stillDownloading removeObject:URL]; |
| 89 | + |
| 90 | + [self performSelectorOnMainThread:@selector(_updateOutputPorts) withObject:nil waitUntilDone:NO]; |
| 91 | +} |
| 92 | + |
| 93 | +- (void)_updateOutputPorts { |
| 94 | + if (_URLs.count == 1) { |
| 95 | + id result = _downloadedImages[[_URLs allValues][0]]; |
| 96 | + outputImageOrStructure.rawValue = (result && result != [NSNull null]) ? result : nil; |
| 97 | + outputDone.booleanValue = YES; |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + NSMutableDictionary *images = [[NSMutableDictionary alloc] initWithCapacity:_URLs.count]; |
| 102 | + if (_downloadedImages.count) { |
| 103 | + for (id key in _URLs) { |
| 104 | + NSString *URL = _URLs[key]; |
| 105 | + if (![_stillDownloading containsObject:URL]) { |
| 106 | + NSImage *image = _downloadedImages[URL]; |
| 107 | + if (image) { |
| 108 | + images[key] = image; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + BOOL allKeysAreNumbers = YES; |
| 115 | + for (id key in images) { |
| 116 | + allKeysAreNumbers &= [key isKindOfClass:[NSNumber class]]; |
| 117 | + } |
| 118 | + |
| 119 | + QCStructure *outputStructure = nil; |
| 120 | + if (images.count) { |
| 121 | + if (!allKeysAreNumbers) { |
| 122 | + outputStructure = [[QCStructure alloc] initWithDictionary:images]; |
| 123 | + } else { |
| 124 | + NSMutableArray *sortedImages = [[NSMutableArray alloc] initWithCapacity:images.count]; |
| 125 | + id keysArray = [[images allKeys] sortedArrayUsingAlphabeticalSort]; |
| 126 | + for (NSNumber *key in keysArray) { |
| 127 | + [sortedImages addObject:images[key]]; |
| 128 | + } |
| 129 | + outputStructure = [[QCStructure alloc] initWithArray:sortedImages]; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + outputImageOrStructure.rawValue = outputStructure; |
| 134 | + outputDone.booleanValue = images.count ? (images.count == _URLs.count) : NO; |
| 135 | +} |
| 136 | + |
| 137 | +@end |
0 commit comments