Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. /*
  2. Copyright 2009-2011 Urban Airship Inc. All rights reserved.
  3.  
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6.  
  7. 1. Redistributions of source code must retain the above copyright notice, this
  8. list of conditions and the following disclaimer.
  9.  
  10. 2. Redistributions in binaryform must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided withthe distribution.
  13.  
  14. THIS SOFTWARE IS PROVIDED BY THE URBAN AIRSHIP INC``AS IS'' AND ANY EXPRESS OR
  15. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  17. EVENT SHALL URBAN AIRSHIP INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  19. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  22. OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  23. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25.  
  26. #import "UANewsstandHelper.h"
  27.  
  28. #import "UASubscriptionInventory.h"
  29. #import "UASubscription.h"
  30.  
  31. #import "UASubscriptionManager.h"
  32. #import "UASubscriptionContent.h"
  33.  
  34. #import "UADownloadContent.h"
  35. #import "UASubscriptionDownloadManager.h"
  36.  
  37. #import "UAGlobal.h"
  38. #import "UAUser.h"
  39. #import "UAUtils.h"
  40.  
  41. @implementation UANewsstandHelper
  42.  
  43. @synthesize contentIdentifier;
  44.  
  45. - (id)init {
  46. if ((self = [super init])) {
  47. return self;
  48. }
  49. return nil;
  50. }
  51.  
  52. - (void)dealloc {
  53. [[UASubscriptionManager shared] removeObserver:self];
  54.  
  55. self.contentIdentifier = nil;
  56.  
  57. RELEASE_SAFELY(connection);
  58.  
  59. }
  60.  
  61. #pragma mark -
  62. #pragma mark Push Handler
  63. - (void)handleNewsstandPushInfo:(NSDictionary *)userInfo {
  64.  
  65. NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
  66. if ([[apsInfo objectForKey:@"content-available"] intValue] == 1) {
  67. // It's a Newsstand app
  68.  
  69. if ([apsInfo objectForKey:@"badge"] != nil){
  70. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[apsInfo objectForKey:@"badge"] intValue]];
  71. }
  72. }
  73.  
  74. if ([userInfo objectForKey:@"cid"] != nil) {
  75. self.contentIdentifier = [userInfo objectForKey:@"cid"];
  76.  
  77. // reload inventory if necessary
  78. if ([[UASubscriptionManager shared].inventory hasLoaded]) {
  79. [[UASubscriptionManager shared] loadSubscription];
  80. }
  81. }
  82. }
  83.  
  84. #pragma mark -
  85. #pragma mark Subscription Observer
  86. - (void)userSubscriptionsUpdated:(NSArray *)userSubscriptions {
  87.  
  88. UASubscriptionInventory *inv = [UASubscriptionManager shared].inventory;
  89. UASubscriptionContent *content = [inv contentForKey:self.contentIdentifier];
  90.  
  91. if (content) {
  92.  
  93. NKLibrary *library = [NKLibrary sharedLibrary];
  94. UALOG(@"Check the library for priors...");
  95.  
  96. if(![library issueWithName:content.contentKey]) {
  97.  
  98. UALOG(@"Retrive S3 Bucket for this URL: %@", content.downloadURL);
  99.  
  100. UAHTTPRequest *request = [UAHTTPRequest requestWithURLString:[content.downloadURL absoluteString]];
  101. request.HTTPMethod= @"POST";
  102. request.username = [UAUser defaultUser].username;
  103. request.password = [UAUser defaultUser].password;
  104.  
  105. RELEASE_SAFELY(connection);
  106. connection = [[UAHTTPConnection connectionWithRequest:request] retain];
  107. connection.delegate = self;
  108.  
  109. [connection start];
  110.  
  111. }
  112. }
  113.  
  114. }
  115.  
  116. #pragma mark -
  117. #pragma mark UAHTTPConnectionDelegate delegate
  118.  
  119. - (void)requestDidSucceed:(UAHTTPRequest *)request
  120. response:(NSHTTPURLResponse *)response
  121. responseData:(NSData *)responseData2 {
  122.  
  123. UALOG(@"response code: %d", [response statusCode]);
  124.  
  125. NSString* newStr = [[NSString alloc] initWithData:responseData2
  126. encoding:NSUTF8StringEncoding];
  127.  
  128. UALOG(@"received content download response: %@",newStr);
  129.  
  130. NSDictionary *result = (NSDictionary *)[UAUtils parseJSON:newStr];
  131. NSString *contentURLString = [result objectForKey:@"download_url"];
  132.  
  133. UALOG(@"DOWNLOAD URL: %@", contentURLString);
  134.  
  135. UASubscriptionInventory *inv = [UASubscriptionManager shared].inventory;
  136. UASubscriptionContent *content = [inv contentForKey:self.contentIdentifier];
  137.  
  138. //reset id
  139. self.contentIdentifier = nil;
  140.  
  141. if (content) {
  142.  
  143. NKLibrary *library = [NKLibrary sharedLibrary];
  144. UALOG(@"Check the library one last time... %@", library);
  145.  
  146. if(![library issueWithName:content.contentKey]) {
  147.  
  148. UALOG(@"Downloading content with key: %@", content.contentKey);
  149.  
  150. NKIssue *issue = [library addIssueWithName:content.contentKey date:content.publishDate];
  151.  
  152. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:contentURLString]
  153. cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
  154. timeoutInterval:30.0];
  155.  
  156. NKAssetDownload *asset = [issue addAssetWithRequest:request];
  157. [asset downloadWithDelegate:self];
  158.  
  159. UALOG(@"Staged download.");
  160. }
  161. } else {
  162. UALOG(@"Already have content.");
  163. }
  164.  
  165. }
  166.  
  167. - (void)requestDidFail:(UAHTTPRequest *)request {
  168. UALOG(@"Failed to get S3 Download URL. Fatal.");
  169. }
  170.  
  171.  
  172. #pragma mark -
  173. #pragma mark NSURLConnectionDownloadDelegate
  174. - (void)connectionDidFinishDownloading:(NSURLConnection *)downloadConnection destinationURL:(NSURL *)destinationURL {
  175.  
  176. NKAssetDownload *asset = [downloadConnection newsstandAssetDownload];
  177.  
  178. //save and set downloaded
  179. UASubscriptionInventory *inv = [UASubscriptionManager shared].inventory;
  180. UASubscriptionContent *content = [inv contentForKey:asset.issue.name];
  181.  
  182. if (!content) {
  183. UALOG(@"Content not found.");
  184. }
  185.  
  186. UAZipDownloadContent *zipDownloadContent = [[UAZipDownloadContent alloc] init];
  187. zipDownloadContent.decompressDelegate = self;
  188. zipDownloadContent.userInfo = content;
  189. zipDownloadContent.downloadPath = [destinationURL relativePath];
  190. UALOG(@"Unzipping content at %@", zipDownloadContent.downloadPath);
  191.  
  192. zipDownloadContent.decompressedContentPath =
  193. [NSString stringWithFormat:@"%@/",
  194. [[UASubscriptionManager shared].downloadManager.downloadDirectory stringByAppendingPathComponent:content.subscriptionKey]];
  195.  
  196. if ([UASubscriptionManager shared].downloadManager.createProductIDSubdir) {
  197.  
  198. // Use the content key as the subdirectory unless the
  199. // product ID is available
  200. NSString *subdirectory = content.contentKey;
  201. if ([content.productIdentifier length] > 0) {
  202. subdirectory = content.productIdentifier;
  203. }
  204.  
  205. zipDownloadContent.decompressedContentPath = [NSString stringWithFormat:@"%@/",
  206. [zipDownloadContent.decompressedContentPath stringByAppendingPathComponent:subdirectory]];
  207. }
  208.  
  209. [zipDownloadContent decompress];
  210.  
  211. UALOG(@"Wrote file to %@", [destinationURL absoluteString]);
  212. }
  213.  
  214. - (void)connectionDidResumeDownloading:(NSURLConnection *)connection
  215. totalBytesWritten:(long long)totalBytesWritten
  216. expectedTotalBytes:(long long)expectedTotalBytes {
  217.  
  218. UALOG(@"didResumeDownloading");
  219. }
  220.  
  221. - (void)connection:(NSURLConnection *)connection
  222. didWriteData:(long long)bytesWritten
  223. totalBytesWritten:(long long)totalBytesWritten
  224. expectedTotalBytes:(long long)expectedTotalBytes {
  225.  
  226. UALOG(@"Writing bytes. Total thus far: %lld",totalBytesWritten);
  227. }
  228.  
  229. #pragma mark -
  230. #pragma mark Unzip Delegate
  231.  
  232. - (void)decompressDidSucceed:(UAZipDownloadContent *)zipDownloadContent {
  233. UASubscriptionContent *content = zipDownloadContent.userInfo;
  234. UALOG(@"Decompress succeeded for content key=%@", content.contentKey);
  235.  
  236. //set progress to 100% / done
  237. [content setProgress:1.0];
  238.  
  239. UALOG(@"DONE!!!!! Unzipped to %@", zipDownloadContent.decompressedContentPath);
  240.  
  241. RELEASE_SAFELY(zipDownloadContent);
  242. }
  243. - (void)decompressDidFail:(UAZipDownloadContent *)zipDownloadContent {
  244. UASubscriptionContent *content = zipDownloadContent.userInfo;
  245. UALOG(@"Decompress failed for content key=%@", content.contentKey);
  246.  
  247. RELEASE_SAFELY(zipDownloadContent);
  248. }
  249.  
  250. @end
Add Comment
Please, Sign In to add comment