VLCHTTPUploaderController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*****************************************************************************
  2. * VLCHTTPUploaderViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Jean-Baptiste Kempf <jb # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Felix Paul Kühne <fkuehne # videolan.org>
  11. * Jean-Romain Prévost <jr # 3on.fr>
  12. *
  13. * Refer to the COPYING file of the official project for license.
  14. *****************************************************************************/
  15. #import "VLCAppDelegate.h"
  16. #import "VLCHTTPUploaderController.h"
  17. #import "VLCHTTPConnection.h"
  18. #import "HTTPServer.h"
  19. #import <ifaddrs.h>
  20. #import <arpa/inet.h>
  21. @implementation VLCHTTPUploaderController
  22. - (id)init
  23. {
  24. if (self = [super init]) {
  25. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  26. [center addObserver:self selector:@selector(applicationDidBecomeActive:)
  27. name:UIApplicationDidBecomeActiveNotification object:nil];
  28. [center addObserver:self selector:@selector(applicationDidEnterBackground:)
  29. name:UIApplicationDidEnterBackgroundNotification object:nil];
  30. }
  31. return self;
  32. }
  33. - (void)applicationDidBecomeActive: (NSNotification *)notification
  34. {
  35. [self changeHTTPServerState:[[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus]];
  36. }
  37. - (void)applicationDidEnterBackground: (NSNotification *)notification
  38. {
  39. [self changeHTTPServerState:NO];
  40. }
  41. - (BOOL)changeHTTPServerState:(BOOL)state
  42. {
  43. if (!state) {
  44. [self.httpServer stop];
  45. return true;
  46. }
  47. // Initialize our http server
  48. _httpServer = [[HTTPServer alloc] init];
  49. [_httpServer setInterface:WifiInterfaceName];
  50. // Tell the server to broadcast its presence via Bonjour.
  51. // This allows browsers such as Safari to automatically discover our service.
  52. [self.httpServer setType:@"_http._tcp."];
  53. // Serve files from the standard Sites folder
  54. NSString *docRoot = [[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] stringByDeletingLastPathComponent];
  55. APLog(@"Setting document root: %@", docRoot);
  56. [self.httpServer setDocumentRoot:docRoot];
  57. [self.httpServer setPort:80];
  58. [self.httpServer setConnectionClass:[VLCHTTPConnection class]];
  59. NSError *error = nil;
  60. if (![self.httpServer start:&error]) {
  61. if (error.code == 13) {
  62. APLog(@"Port forbidden by OS, trying another one");
  63. [self.httpServer setPort:8888];
  64. if(![self.httpServer start:&error])
  65. return true;
  66. }
  67. /* Address already in Use, take a random one */
  68. if (error.code == 48) {
  69. APLog(@"Port already in use, trying another one");
  70. [self.httpServer setPort:0];
  71. if(![self.httpServer start:&error])
  72. return true;
  73. }
  74. if (error.code != 0)
  75. APLog(@"Error starting HTTP Server: %@", error.localizedDescription);
  76. return false;
  77. }
  78. return true;
  79. }
  80. - (NSString *)currentIPAddress
  81. {
  82. NSString *address = @"";
  83. struct ifaddrs *interfaces = NULL;
  84. struct ifaddrs *temp_addr = NULL;
  85. int success = getifaddrs(&interfaces);
  86. if (success != 0) {
  87. freeifaddrs(interfaces);
  88. return address;
  89. }
  90. temp_addr = interfaces;
  91. while (temp_addr != NULL) {
  92. if (temp_addr->ifa_addr->sa_family == AF_INET) {
  93. if([@(temp_addr->ifa_name) isEqualToString:WifiInterfaceName])
  94. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  95. }
  96. temp_addr = temp_addr->ifa_next;
  97. }
  98. freeifaddrs(interfaces);
  99. return address;
  100. }
  101. - (void)moveFileFrom:(NSString *)filepath
  102. {
  103. NSString *fileName = [filepath lastPathComponent];
  104. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  105. NSString *libraryPath = searchPaths[0];
  106. NSString *finalFilePath = [libraryPath stringByAppendingPathComponent:fileName];
  107. NSFileManager *fileManager = [NSFileManager defaultManager];
  108. if ([fileManager fileExistsAtPath:finalFilePath]) {
  109. /* we don't want to over-write existing files, so add an integer to the file name */
  110. NSString *potentialFilename;
  111. NSString *fileExtension = [fileName pathExtension];
  112. NSString *rawFileName = [fileName stringByDeletingPathExtension];
  113. for (NSUInteger x = 1; x < 100; x++) {
  114. potentialFilename = [NSString stringWithFormat:@"%@ %lu.%@", rawFileName, (unsigned long)x, fileExtension];
  115. if (![[NSFileManager defaultManager] fileExistsAtPath:[libraryPath stringByAppendingPathComponent:potentialFilename]])
  116. break;
  117. }
  118. finalFilePath = [libraryPath stringByAppendingPathComponent:potentialFilename];
  119. }
  120. NSError *error;
  121. [fileManager moveItemAtPath:filepath toPath:finalFilePath error:&error];
  122. if (error) {
  123. APLog(@"Moving received media %@ to library folder failed (%li), deleting", fileName, (long)error.code);
  124. [fileManager removeItemAtPath:filepath error:nil];
  125. }
  126. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  127. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  128. /* update media library when file upload was completed */
  129. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  130. [appDelegate performSelectorOnMainThread:@selector(updateMediaList) withObject:nil waitUntilDone:NO];
  131. }
  132. @end