VLCNetworkServerBrowserFTP.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. @property (nonatomic, readwrite) NSArray<id<VLCNetworkServerBrowserItem>> *items;
  18. @end
  19. @implementation VLCNetworkServerBrowserFTP
  20. @synthesize delegate = _delegate, items = _items;
  21. #pragma mark - Protocol conformance
  22. - (NSString *)title {
  23. if ([_url.path isEqualToString:@"/"])
  24. return _url.host;
  25. else
  26. return [_url.path lastPathComponent];
  27. }
  28. - (void)update {
  29. if (_FTPListDirRequest)
  30. return;
  31. _FTPListDirRequest = [[WRRequestListDirectory alloc] init];
  32. _FTPListDirRequest.delegate = self;
  33. _FTPListDirRequest.hostname = _url.host;
  34. _FTPListDirRequest.username = _url.user;
  35. _FTPListDirRequest.password = _url.password;
  36. _FTPListDirRequest.path = _url.path;
  37. _FTPListDirRequest.passive = YES;
  38. [_FTPListDirRequest start];
  39. }
  40. #pragma mark -
  41. - (instancetype)initWithFTPServer:(NSString *)serverAddress userName:(NSString *)username andPassword:(NSString *)password atPath:(NSString *)path
  42. {
  43. NSURLComponents *components = [[NSURLComponents alloc] init];
  44. components.scheme = @"ftp";
  45. components.host = serverAddress;
  46. components.user = username.length ? username : @"anonymous";
  47. components.password = password.length ? password : nil;
  48. components.path = path;
  49. return [self initWithURL:components.URL];
  50. }
  51. - (instancetype)initWithURL:(NSURL *)url
  52. {
  53. self = [super init];
  54. if (self) {
  55. _url = url;
  56. }
  57. return self;
  58. }
  59. #pragma mark - white raccoon delegation
  60. - (void)requestCompleted:(WRRequest *)request
  61. {
  62. if (request == _FTPListDirRequest) {
  63. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  64. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  65. NSUInteger count = rawList.count;
  66. for (NSUInteger x = 0; x < count; x++) {
  67. NSDictionary *dict = rawList[x];
  68. if (![[dict objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  69. [filteredList addObject:[[VLCNetworkServerBrowserItemFTP alloc] initWithDictionary:dict baseURL:self.url]];
  70. }
  71. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  72. self.items = [NSArray arrayWithArray:filteredList];
  73. [self.delegate networkServerBrowserDidUpdate:self];
  74. }];
  75. } else
  76. APLog(@"unknown request %@ completed", request);
  77. }
  78. - (void)requestFailed:(WRRequest *)request
  79. {
  80. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  81. NSDictionary *userInfo = nil;
  82. if (request.error.message) {
  83. userInfo = @{ NSLocalizedDescriptionKey : request.error.message };
  84. }
  85. NSError *error = [NSError errorWithDomain:@"org.videolan.whiteracoon" code:request.error.errorCode userInfo:userInfo];
  86. [self.delegate networkServerBrowser:self requestDidFailWithError:error];
  87. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  88. }];
  89. }
  90. @end
  91. @implementation VLCNetworkServerBrowserItemFTP
  92. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL;
  93. - (instancetype)initWithDictionary:(NSDictionary *)dict baseURL:(NSURL *)baseURL
  94. {
  95. self = [super init];
  96. if (self) {
  97. NSString *rawFileName = [dict objectForKey:(id)kCFFTPResourceName];
  98. NSData *flippedData = [rawFileName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  99. if (flippedData != nil) {
  100. _name = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  101. if (_name == nil) {
  102. /* this can happen if our string conversation failed */
  103. _name = rawFileName;
  104. }
  105. _container = [dict[(id)kCFFTPResourceType] intValue] == 4;
  106. _fileSizeBytes = dict[(id)kCFFTPResourceSize];
  107. _URL = [baseURL URLByAppendingPathComponent:_name];
  108. }
  109. }
  110. return self;
  111. }
  112. - (id<VLCNetworkServerBrowser>)containerBrowser {
  113. return [[VLCNetworkServerBrowserFTP alloc] initWithURL:self.URL];
  114. }
  115. @end