overte-HifiExperiments/launchers/darwin/src/DownloadDomainContent.m
Matt Hardcastle c987106d96 Add User-Agent string to macOS Launcher
Before this change, the mac HQ launcher didn't set a User-Agent string.
This change adds a User-Agent header – HQLauncher/$version – to each
HTTP request the mac launcher makes.

The approach I took was to add the User-Agent header to each HTTP
request. There is room for improvement. Most of the session
initialization could be rolled up into a helper class that sets the
User-Agent headers and other configuration parameters. Making that
change would be too much overhead for this quick task.

Jira: https://highfidelity.atlassian.net/browse/DEV-417
2019-08-21 13:29:56 -07:00

104 lines
4.7 KiB
Objective-C

#import "DownloadDomainContent.h"
#import "Launcher.h"
@implementation DownloadDomainContent
- (double) getProgressPercentage
{
return (self.progressPercentage * 0.70) + (self.taskProgressPercentage * 0.30);
}
- (void) downloadDomainContent:(NSString *)domainContentUrl
{
self.progressPercentage = 0.0;
self.taskProgressPercentage = 0.0;
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:domainContentUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setValue:@USER_AGENT_STRING forHTTPHeaderField:@"User-Agent"];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *downloadTask = [defaultSession downloadTaskWithRequest:request];
[downloadTask resume];
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
CGFloat prog = (float)totalBytesWritten/totalBytesExpectedToWrite;
NSLog(@"domain content downloaded %f", (100.0*prog));
self.progressPercentage = (int)(100.0 * prog);
[[Launcher sharedLauncher] updateProgressIndicator];
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
// unused in this example
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSLog(@"Did finish downloading to url");
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
target: self
selector: @selector(updatePercentage:)
userInfo:nil
repeats: YES];
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destinationFileName = downloadTask.originalRequest.URL.lastPathComponent;
NSString* finalFilePath = [[[Launcher sharedLauncher] getDownloadPathForContentAndScripts] stringByAppendingPathComponent:destinationFileName];
NSURL *destinationURL = [NSURL URLWithString: [finalFilePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]] relativeToURL: [NSURL URLWithString:@"file://"]];
if([fileManager fileExistsAtPath:[destinationURL path]])
{
[fileManager removeItemAtURL:destinationURL error:nil];
}
NSLog(@"location: %@", location.path);
NSLog(@"destination: %@", destinationURL);
BOOL success = [fileManager moveItemAtURL:location toURL:destinationURL error:&error];
NSLog(success ? @"TRUE" : @"FALSE");
Launcher* sharedLauncher = [Launcher sharedLauncher];
if (error) {
NSLog(@"DownlodDomainContent: failed to move file to destintation -> error: %@", error);
[timer invalidate];
[sharedLauncher displayErrorPage];
return;
}
[sharedLauncher setDownloadContextFilename:destinationFileName];
NSLog(@"extracting domain content file");
BOOL extractionSuccessful = [sharedLauncher extractZipFileAtDestination:[[sharedLauncher getDownloadPathForContentAndScripts] stringByAppendingString:@"content"] :[[sharedLauncher getDownloadPathForContentAndScripts] stringByAppendingString:[sharedLauncher getDownloadContentFilename]]];
if (!extractionSuccessful) {
[timer invalidate];
[sharedLauncher displayErrorPage];
return;
}
NSLog(@"finished extracting content file");
[timer invalidate];
self.taskProgressPercentage = 100.0;
[sharedLauncher updateProgressIndicator];
[sharedLauncher domainContentDownloadFinished];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSLog(@"completed; error: %@", error);
if (error) {
[[Launcher sharedLauncher] displayErrorPage];
}
}
- (void) updatePercentage:(NSTimer*) timer {
if (self.taskProgressPercentage < 100.0) {
self.taskProgressPercentage += 1.5;
if (self.taskProgressPercentage > 100.0) {
self.taskProgressPercentage = 100.0;
}
}
[[Launcher sharedLauncher] updateProgressIndicator];
}
@end