VLCNetworkServerBrowserFTP.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserFTP.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCNetworkServerBrowserFTP.h"
  13. #import "WhiteRaccoon.h"
  14. @interface VLCNetworkServerBrowserFTP () <WRRequestDelegate>
  15. @property (nonatomic) NSURL *url;
  16. @property (nonatomic) WRRequestListDirectory *FTPListDirRequest;
  17. @end
  18. @implementation VLCNetworkServerBrowserFTP
  19. @synthesize delegate = _delegate, items = _items;
  20. #pragma mark - Protocol conformance
  21. - (NSString *)title {
  22. if ([_url.path isEqualToString:@"/"])
  23. return _url.host;
  24. else
  25. return [_url.path lastPathComponent];
  26. }
  27. - (void)update {
  28. if (_FTPListDirRequest)
  29. return;
  30. _FTPListDirRequest = [[WRRequestListDirectory alloc] init];
  31. _FTPListDirRequest.delegate = self;
  32. _FTPListDirRequest.hostname = _url.host;
  33. _FTPListDirRequest.username = _url.user;
  34. _FTPListDirRequest.password = _url.password;
  35. _FTPListDirRequest.path = _url.path;
  36. _FTPListDirRequest.passive = YES;
  37. [_FTPListDirRequest start];
  38. }
  39. #pragma mark -
  40. - (instancetype)initWithFTPServer:(NSString *)serverAddress userName:(NSString *)username andPassword:(NSString *)password atPath:(NSString *)path
  41. {
  42. NSURLComponents *components = [[NSURLComponents alloc] init];
  43. components.scheme = @"ftp";
  44. components.host = serverAddress;
  45. components.user = username.length ? username : @"anonymous";
  46. components.password = password.length ? password : nil;
  47. components.path = path;
  48. return [self initWithURL:components.URL];
  49. }
  50. - (instancetype)initWithURL:(NSURL *)url
  51. {
  52. self = [super init];
  53. if (self) {
  54. _url = url;
  55. }
  56. return self;
  57. }
  58. - (VLCMediaList *)mediaList
  59. {
  60. NSMutableArray *mediaArray = [NSMutableArray array];
  61. @synchronized(_items) {
  62. NSUInteger count = _items.count;
  63. for (NSUInteger i = 0; i < count; i++) {
  64. VLCMedia *media = [_items[i] media];
  65. if (media)
  66. [mediaArray addObject:media];
  67. }
  68. }
  69. return [[VLCMediaList alloc] initWithArray:mediaArray];
  70. }
  71. #pragma mark - white raccoon delegation
  72. - (void)requestCompleted:(WRRequest *)request
  73. {
  74. if (request == _FTPListDirRequest) {
  75. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  76. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  77. NSUInteger count = rawList.count;
  78. for (NSUInteger x = 0; x < count; x++) {
  79. NSDictionary *dict = rawList[x];
  80. if (![[dict objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  81. [filteredList addObject:[[VLCNetworkServerBrowserItemFTP alloc] initWithDictionary:dict baseURL:self.url]];
  82. }
  83. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  84. @synchronized(_items) {
  85. _items = [NSArray arrayWithArray:filteredList];
  86. }
  87. [self.delegate networkServerBrowserDidUpdate:self];
  88. }];
  89. } else
  90. APLog(@"unknown request %@ completed", request);
  91. }
  92. - (void)requestFailed:(WRRequest *)request
  93. {
  94. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  95. NSDictionary *userInfo = nil;
  96. if (request.error.message) {
  97. userInfo = @{ NSLocalizedDescriptionKey : request.error.message };
  98. }
  99. NSError *error = [NSError errorWithDomain:@"org.videolan.whiteracoon" code:request.error.errorCode userInfo:userInfo];
  100. [self.delegate networkServerBrowser:self requestDidFailWithError:error];
  101. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  102. }];
  103. }
  104. @end
  105. @implementation VLCNetworkServerBrowserItemFTP
  106. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL;
  107. - (instancetype)initWithDictionary:(NSDictionary *)dict baseURL:(NSURL *)baseURL
  108. {
  109. self = [super init];
  110. if (self) {
  111. NSString *rawFileName = [dict objectForKey:(id)kCFFTPResourceName];
  112. NSData *flippedData = [rawFileName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  113. if (flippedData != nil) {
  114. _name = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  115. if (_name == nil) {
  116. /* this can happen if our string conversation failed */
  117. _name = rawFileName;
  118. }
  119. _container = [dict[(id)kCFFTPResourceType] intValue] == 4;
  120. _fileSizeBytes = dict[(id)kCFFTPResourceSize];
  121. _URL = [baseURL URLByAppendingPathComponent:_name];
  122. }
  123. }
  124. return self;
  125. }
  126. - (id<VLCNetworkServerBrowser>)containerBrowser {
  127. return [[VLCNetworkServerBrowserFTP alloc] initWithURL:self.URL];
  128. }
  129. - (VLCMedia *)media
  130. {
  131. if (_URL)
  132. return [VLCMedia mediaWithURL:_URL];
  133. return nil;
  134. }
  135. @end