|
| 1 | +// |
| 2 | +// NKFTPManager.h |
| 3 | +// FTPManager |
| 4 | +// |
| 5 | +// Created by Nico Kreipke on 11.08.11. |
| 6 | +// Copyright (c) 2014 nkreipke. All rights reserved. |
| 7 | +// http://nkreipke.de |
| 8 | +// |
| 9 | + |
| 10 | +// Version 1.6.5 |
| 11 | +// SEE LICENSE FILE FOR LICENSE INFORMATION |
| 12 | + |
| 13 | +// Information: |
| 14 | +// Parts of this class are based on the SimpleFTPSample sample code by Apple. |
| 15 | + |
| 16 | +// This class requires the following frameworks: |
| 17 | +// - CoreServices.framework (OS X) |
| 18 | +// - CFNetwork.framework (iOS) |
| 19 | + |
| 20 | +// *********** |
| 21 | +// CHANGELOG** |
| 22 | +// *********** |
| 23 | +// |
| 24 | +// ** 1.2 (2012-05-28) by nkreipke |
| 25 | +// - Methods added: |
| 26 | +// - (float) progress |
| 27 | +// - (void) abort |
| 28 | +// - downloadFile:toDirectory:fromServer: now sends an FTP STAT command to |
| 29 | +// determine the size of the file to download. |
| 30 | +// |
| 31 | +// ** 1.3 (2012-05-28) by jweinert (cs&m GmbH) |
| 32 | +// - Protocol declaration added: |
| 33 | +// FTPManagerDelegate |
| 34 | +// - Delegate Methods added (both optional): |
| 35 | +// - (void)FTPManagerUploadProgressDidChange:(NSDictionary *)upInfo |
| 36 | +// - (void)FTPManagerDownloadProgressDidChange:(NSDictionary *)downInfo |
| 37 | +// Both methods return an NSDictionary providing the following information (keys): |
| 38 | +// * progress: progress (0.0f to 1.0f, -1.0 at fail) |
| 39 | +// * fileSize: size of the currently processing file |
| 40 | +// * bytesProcessed: number of Bytes processed from the current session |
| 41 | +// * fileSizeProcessed: number of Bytes processed of the currently processing file |
| 42 | +// |
| 43 | +// ** 1.4 (2012-06-07) by jweinert (cs&m GmbH) |
| 44 | +// - Methods added: |
| 45 | +// - uploadData:withFileName:toServer: |
| 46 | +// - deleteFile:fromServer: |
| 47 | +// |
| 48 | +// ** 1.4.1 (2012-06-08) by nkreipke |
| 49 | +// - replaced string keys with kFMProcessInfo constants. |
| 50 | +// - changed method declarations containing an info dictionary to name it processInfo. |
| 51 | +// - fixed broken createNewFolder method in ARC version |
| 52 | +// |
| 53 | +// ** 1.5 (2012-10-25) by nkreipke |
| 54 | +// - FMServer: |
| 55 | +// + anonymousServerWithDestination:(NSURL*) |
| 56 | +// - added NSURL category FTPManagerNSURLAdditions |
| 57 | +// - the destination URL does NOT need the "ftp://" prefix anymore |
| 58 | +// - deleteFile:fromServer: is deprecated |
| 59 | +// - Methods added: |
| 60 | +// - deleteFileNamed:fromServer: |
| 61 | +// - chmodFileNamed:to:atServer: |
| 62 | +// - checkLogin: |
| 63 | +// - cleaned up the code a little |
| 64 | +// |
| 65 | +// ** 1.6 (2013-01-13) by nkreipke |
| 66 | +// - FMServer: |
| 67 | +// - fixed a bug where variables were not retained properly |
| 68 | +// - FMServer.destination is now NSString! You will have to change that in your code. |
| 69 | +// - In FMSever.port the port can be specified. This is 21 by default. |
| 70 | +// - fixed a bug where an empty file was created if downloadFile was not successful |
| 71 | +// |
| 72 | +// ** (1.6.1 -> release for CocoaPods) |
| 73 | +// |
| 74 | +// ** 1.6.2 (2013-04-24) by nkreipke |
| 75 | +// - fixed a bug that occured in iOS 6 (https://github.com/nkreipke/FTPManager/issues/5) |
| 76 | +// - fixed garbage value bug |
| 77 | +// |
| 78 | +// ** 1.6.3 (2014-01-25) by nkreipke |
| 79 | +// - fixed a memory leak |
| 80 | +// |
| 81 | +// ** 1.6.4 (2014-06-13) by nkreipke |
| 82 | +// - fixed crash that can occur when scheduling the stream (thanks to Kevin Paunovic) |
| 83 | +// - fixed race condition bug while aborting |
| 84 | +// - a separate NSThread is now used instead of using whatever thread FTPManager was called on |
| 85 | +// - fixed bug that prevented subdirectories from being accessed when port was not 21 |
| 86 | +// - fixed bug that prevented subdirectories with names containing spaces from being accessed |
| 87 | +// |
| 88 | +// ** 1.6.5 (2014-08-12) by nkreipke |
| 89 | +// - kCFFTPResourceName entry is now converted into UTF8 encoding to cope with Non-ASCII characters |
| 90 | +// |
| 91 | + |
| 92 | +// SCROLL DOWN TO SEE THE WELL COMMENTED PUBLIC METHODS. ***************** |
| 93 | + |
| 94 | +// LOOK AT FTPMSample1.m FOR AN EXAMPLE HOW TO USE THIS CLASS. *********** |
| 95 | + |
| 96 | + |
| 97 | +#import <Foundation/Foundation.h> |
| 98 | +#import <sys/socket.h> |
| 99 | +#import <sys/types.h> |
| 100 | +#import <netinet/in.h> |
| 101 | +#import <netdb.h> |
| 102 | + |
| 103 | +//FTPManager can log Socket answers if you got problems with |
| 104 | +//deletion and chmod: |
| 105 | +//#define FMSOCKET_VERBOSE |
| 106 | + |
| 107 | +//these are used internally: |
| 108 | +#define FTPANONYMOUS @"anonymous" |
| 109 | +/*enum { |
| 110 | + kFTPAnswerSuccess = 200, |
| 111 | + kFTPAnswerLoggedIn = 230, |
| 112 | + kFTPAnswerFileActionOkay = 250, |
| 113 | + kFTPAnswerNeedsPassword = 331, |
| 114 | + kFTPAnswerNotAvailable = 421, |
| 115 | + kFTPAnswerNotLoggedIn = 530 |
| 116 | + };*/ |
| 117 | + |
| 118 | +@interface FMServer : NSObject { |
| 119 | +@private |
| 120 | + NSString* destination; |
| 121 | + NSString* password; |
| 122 | + NSString* username; |
| 123 | + int port; |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * The URL of the FMServer. |
| 128 | + */ |
| 129 | +@property (strong) NSString* destination; |
| 130 | + |
| 131 | +/** |
| 132 | + * The password for the FMServer login. |
| 133 | + */ |
| 134 | +@property (strong) NSString* password; |
| 135 | + |
| 136 | +/** |
| 137 | + * The username for the FMServer login. |
| 138 | + */ |
| 139 | +@property (strong) NSString* username; |
| 140 | + |
| 141 | +/** |
| 142 | + * The port which is used for the connection. |
| 143 | + */ |
| 144 | +@property (unsafe_unretained) int port; |
| 145 | + |
| 146 | +/** |
| 147 | + * Returns a FMServer initialized with the given URL and credentials. |
| 148 | + * |
| 149 | + * @param dest The URL of the FTP server. |
| 150 | + * @param user The username of the account which will be used to log in. |
| 151 | + * @param pass The password which will be used to log in. |
| 152 | + * |
| 153 | + * @return A FMServer object with the given URL, username and password. |
| 154 | + */ |
| 155 | ++ (FMServer*) serverWithDestination:(NSString*)dest username:(NSString*)user password:(NSString*)pass; |
| 156 | + |
| 157 | +/** |
| 158 | + * Returns a FMServer initialized with the given URL and anonymous login. |
| 159 | + * |
| 160 | + * @param dest The URL of the FTP server. |
| 161 | + * |
| 162 | + * @return A FMServer object with the given URL and anonymous login. |
| 163 | + */ |
| 164 | ++ (FMServer*) anonymousServerWithDestination:(NSString*)dest; |
| 165 | + |
| 166 | +@end |
| 167 | + |
| 168 | +@interface NSString (FTPManagerNSStringAdditions) |
| 169 | +-(NSString*)stringWithoutProtocol; |
| 170 | +-(NSURL*)ftpURLForPort:(int)port; |
| 171 | +-(NSString*)fmhost; |
| 172 | +-(NSString*)fmdir; |
| 173 | +@end |
| 174 | + |
| 175 | +enum { |
| 176 | + kSendBufferSize = 32768 |
| 177 | +}; |
| 178 | + |
| 179 | +typedef enum { |
| 180 | + FMStreamFailureReasonNone, |
| 181 | + FMStreamFailureReasonReadError, |
| 182 | + FMStreamFailureReasonWriteError, |
| 183 | + FMStreamFailureReasonGeneralError, |
| 184 | + FMStreamFailureReasonAborted |
| 185 | +} FMStreamFailureReason; |
| 186 | + |
| 187 | +typedef enum { |
| 188 | + _FMCurrentActionUploadFile, |
| 189 | + _FMCurrentActionCreateNewFolder, |
| 190 | + _FMCurrentActionContentsOfServer, |
| 191 | + _FMCurrentActionDownloadFile, |
| 192 | + _FMCurrentActionSOCKET, |
| 193 | + _FMCurrentActionNone |
| 194 | +} _FMCurrentAction; |
| 195 | + |
| 196 | +/* I do not recommend to use this delegate, because the methods will slow down |
| 197 | + * the process. On top of this they may have some threading issues that could |
| 198 | + * be pretty confusing. Use an NSTimer and [manager progress] instead. */ |
| 199 | +@protocol NKFTPManagerDelegate <NSObject> |
| 200 | +@optional |
| 201 | +- (void)ftpManagerUploadProgressDidChange:(NSDictionary *)processInfo; |
| 202 | +// Returns information about the current upload. |
| 203 | +// See "Process Info Dictionary Constants" below for detailed info. |
| 204 | +- (void)ftpManagerDownloadProgressDidChange:(NSDictionary *)processInfo; |
| 205 | +// Returns information about the current download. |
| 206 | +// See "Process Info Dictionary Constants" below for detailed info. |
| 207 | +@end |
| 208 | + |
| 209 | +#pragma mark - Process Info Dictionary Constants |
| 210 | + |
| 211 | +// Process Info Dictionary Constants (for [manager progress]): ****************** |
| 212 | +#define kFMProcessInfoProgress @"progress" // 0.0 to 1.0 |
| 213 | +#define kFMProcessInfoFileSize @"fileSize" |
| 214 | +#define kFMProcessInfoBytesProcessed @"bytesProcessed" |
| 215 | +#define kFMProcessInfoFileSizeProcessed @"fileSizeProcessed" |
| 216 | +// ---------------------------------(returns NSNumber values)-------------------- |
| 217 | + |
| 218 | +#pragma mark - |
| 219 | + |
| 220 | +@interface NKFTPManager : NSObject <NSStreamDelegate> { |
| 221 | + CFRunLoopRef currentRunLoop; |
| 222 | + |
| 223 | + _FMCurrentAction action; |
| 224 | + |
| 225 | + uint8_t _buffer[kSendBufferSize]; |
| 226 | + size_t _bufferOffset; |
| 227 | + size_t _bufferLimit; |
| 228 | + |
| 229 | + unsigned long long fileSize; |
| 230 | + unsigned long long bytesProcessed; |
| 231 | + unsigned long long fileSizeProcessed; |
| 232 | + |
| 233 | + BOOL streamSuccess; |
| 234 | +} |
| 235 | + |
| 236 | +/** |
| 237 | + * Input steam for reading from a local file |
| 238 | + */ |
| 239 | +@property (strong) NSInputStream *fileReader; |
| 240 | + |
| 241 | +/** |
| 242 | + * Output stream for writing to a local file |
| 243 | + */ |
| 244 | +@property (strong) NSOutputStream *fileWriter; |
| 245 | + |
| 246 | +/** |
| 247 | + * Input stream for reading from the server (remote file) |
| 248 | + */ |
| 249 | +@property (strong) NSInputStream *serverReadStream; |
| 250 | + |
| 251 | +/** |
| 252 | + * Output stream for writing to the server (remote file) |
| 253 | + */ |
| 254 | +@property (strong) NSOutputStream *serverStream; |
| 255 | + |
| 256 | +@property (strong) NSMutableData *directoryListingData; |
| 257 | + |
| 258 | + |
| 259 | +@property (assign) id<NKFTPManagerDelegate>delegate; |
| 260 | + |
| 261 | +#pragma mark - Public Methods |
| 262 | + |
| 263 | +// *** Information |
| 264 | +// These methods hold the current thread. You will get an answer with a success information. |
| 265 | + |
| 266 | +/** |
| 267 | + * Uploads a file to a server. Existing remote files of the same name will be overwritten. |
| 268 | + * |
| 269 | + * @param fileURL The local file which will be uploaded to the FTP server. |
| 270 | + * @param server The FTP server which the file will be uploaded to. |
| 271 | + * |
| 272 | + * @return YES if the upload was successful, NO otherwise. |
| 273 | + */ |
| 274 | +- (BOOL) uploadFile:(NSURL*)fileURL toServer:(FMServer*)server; |
| 275 | + |
| 276 | +/** |
| 277 | + * Uploads NSData to a server. Existing remote files of the same name will be overwritten. |
| 278 | + * |
| 279 | + * @param data The data which will be written to the FTP server. |
| 280 | + * @param fileName The name with which the new file will be created on the FTP server. |
| 281 | + * @param server The FTP server on which the file with the given data will be created. |
| 282 | + * |
| 283 | + * @return YES if the upload was successful, NO otherwise. |
| 284 | + */ |
| 285 | +- (BOOL) uploadData:(NSData*)data withFileName:(NSString *)fileName toServer:(FMServer*)server; |
| 286 | + |
| 287 | +/** |
| 288 | + * Creates a new folder on the specified FTP server. |
| 289 | + * |
| 290 | + * @param folderName The name of the folder to create. |
| 291 | + * @param server The FTP server on which the new folder should be created. |
| 292 | + * |
| 293 | + * @return YES if the folder creation was successful, NO otherwise. |
| 294 | + */ |
| 295 | +- (BOOL) createNewFolder:(NSString*)folderName atServer:(FMServer*)server; |
| 296 | + |
| 297 | +/** |
| 298 | + * Returns a list of files and folders at the specified FTP server. as an NSArray containing instances of NSDictionary. |
| 299 | + * The dictionaries contain objects declared in CFStream FTP Resource Constants. To get the name of the entry, get the object for the (id)kCFFTPResourceName key. |
| 300 | + * |
| 301 | + * @param server The FTP server whose contents will be listed. |
| 302 | + * |
| 303 | + * @return The NSArray containing instances of NSDictionary. An empty array if the server has no contents. nil if there was an error during the process. |
| 304 | + */ |
| 305 | +- (NSArray*) contentsOfServer:(FMServer*)server; |
| 306 | + |
| 307 | +/** |
| 308 | + * Downloads a file from the specified FTP server. Existing local files of the same name will be overwritten. |
| 309 | + * |
| 310 | + * @param fileName The file which will be downloaded from the specified FTP server. |
| 311 | + * @param directoryURL The local directory the file will be downloaded to. |
| 312 | + * @param server The server the file will be downloaded from. |
| 313 | + * |
| 314 | + * @return YES if the download was successful, NO otherwise |
| 315 | + */ |
| 316 | +- (BOOL) downloadFile:(NSString*)fileName toDirectory:(NSURL*)directoryURL fromServer:(FMServer*)server; |
| 317 | + |
| 318 | +/** |
| 319 | + * Delete a file from the specified FTP server and delete directories if they are empty. |
| 320 | + * |
| 321 | + * @param fileName The file which will be deleted from the FTP server. |
| 322 | + * @param server The FTP server from which the file or directory will be deleted. |
| 323 | + * |
| 324 | + * @return YES if the file was successfully deleted from the server, NO otherwise. |
| 325 | + */ |
| 326 | +- (BOOL) deleteFileNamed:(NSString*)fileName fromServer:(FMServer*)server; |
| 327 | + |
| 328 | +/** |
| 329 | + * Changes the mode of a file on a server. Works only on UNIX servers. |
| 330 | + * |
| 331 | + * @param fileName The file whose permissions will be modified. |
| 332 | + * @param mode The mode which will be applied to the remote file in octal notation. |
| 333 | + * @param server The server on which the mode change operation will take place. |
| 334 | + * |
| 335 | + * @return YES if the chmod command was successful, NO otherwise. |
| 336 | + */ |
| 337 | +- (BOOL) chmodFileNamed:(NSString*)fileName to:(int)mode atServer:(FMServer*)server; |
| 338 | + |
| 339 | +/** |
| 340 | + * Logs into the FTP server and logs out again. This can be used to check whether the credentials are correct before trying to do a file operation. |
| 341 | + * |
| 342 | + * @param server The FMServer FTP object to log into. |
| 343 | + * |
| 344 | + * @return YES if the login was successful, NO otherwise. |
| 345 | + */ |
| 346 | +- (BOOL) checkLogin:(FMServer*)server; |
| 347 | + |
| 348 | +/** |
| 349 | + * Returns information about the current process. As the FTP methods hold the thread, you may want to call this method from a different thread that updates the UI. |
| 350 | + * See 'Process Info Dictionary Constants' above for information about the contents of the dictionary. |
| 351 | + * |
| 352 | + * @return nil if no process is currently running or information could not be determined. This method only works when downloading or uploading a file. |
| 353 | + */ |
| 354 | +- (NSMutableDictionary *) progress; |
| 355 | + |
| 356 | +/** |
| 357 | + * Aborts the current process. As the FTP methods hold the thread, you may want to call this method from a different thread. |
| 358 | + */ |
| 359 | +- (void) abort; |
| 360 | + |
| 361 | + |
| 362 | + |
| 363 | + |
| 364 | + |
| 365 | +//deprecated: |
| 366 | +/** |
| 367 | + * Deletes a file or directory from the specified FTP server. Uses absolute path on server as path parameter. |
| 368 | + * DEPRECATED: Use deleteFileNamed:fromServer: instead. |
| 369 | + * |
| 370 | + * @param absolutePath The absolute path to the file which will be deleted on the server. The URL must end with a slash ("/"). |
| 371 | + * @param server The server from which the file will be deleted. |
| 372 | + * |
| 373 | + * @return YES if the file was successfully deleted, NO otherwise. |
| 374 | + */ |
| 375 | +- (BOOL) deleteFile:(NSString *)absolutePath fromServer:(FMServer *)server DEPRECATED_ATTRIBUTE; |
| 376 | + |
| 377 | +@end |
| 378 | + |
0 commit comments