NCAvatarSessionManager.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "NCAppBranding.h"
  6. #import "NCAvatarSessionManager.h"
  7. #import "AFImageDownloader.h"
  8. #import "CCCertificate.h"
  9. @implementation NCAvatarSessionManager
  10. + (NCAvatarSessionManager *)sharedInstance
  11. {
  12. static dispatch_once_t once;
  13. static NCAvatarSessionManager *sharedInstance;
  14. dispatch_once(&once, ^{
  15. sharedInstance = [[self alloc] init];
  16. });
  17. return sharedInstance;
  18. }
  19. - (id)init
  20. {
  21. NSURLSessionConfiguration *configuration = [AFImageDownloader defaultURLSessionConfiguration];
  22. // In case of avatars we want to use the cache and store it on disk
  23. // As we use the memory cache from AFImageDownloader, we only want disk cache here
  24. NSURL *avatarCacheURL = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupIdentifier] URLByAppendingPathComponent:@"AvatarCache"];
  25. self.cache = [[NSURLCache alloc] initWithMemoryCapacity:0
  26. diskCapacity:100 * 1024 * 1024
  27. directoryURL:avatarCacheURL];
  28. configuration.URLCache = self.cache;
  29. self = [super initWithSessionConfiguration:configuration];
  30. if (self) {
  31. _userAgent = [NSString stringWithFormat:@"Mozilla/5.0 (iOS) Nextcloud-Talk v%@",
  32. [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
  33. self.responseSerializer = [[AFImageResponseSerializer alloc] init];
  34. self.requestSerializer = [[AFHTTPRequestSerializer alloc] init];
  35. AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
  36. self.securityPolicy = policy;
  37. self.responseSerializer.acceptableContentTypes = [self.responseSerializer.acceptableContentTypes setByAddingObject:@"image/jpg"];
  38. [self.requestSerializer setValue:_userAgent forHTTPHeaderField:@"User-Agent"];
  39. }
  40. return self;
  41. }
  42. -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
  43. {
  44. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  45. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  46. } else {
  47. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  48. }
  49. }
  50. @end