/// <summary> /// FTP 서버의 <paramref name="directory"/>의 하위 디렉토리 및 파일명을 열거합니다. /// </summary> /// <param name="ftpClient"></param> /// <param name="directory"></param> /// <param name="recursive">재귀 호출 여부</param> /// <returns></returns> public static Task <IList <string> > ListDirectoryTask(this FtpClient ftpClient, string directory, bool recursive) { if (IsDebugEnabled) { log.Debug("List all entry in the specified directory. directory=[{0}], recursive=[{1}]", directory, recursive); } var paths = new List <string>(); directory = FtpClient.AdjustDir(directory); paths.Add(directory); if (!directory.EndsWith(FtpClient.FTP_PATH_DELIMITER)) { directory += FtpClient.FTP_PATH_DELIMITER; } var task = ftpClient.ListDirectoryTask(directory); if (recursive) { task.ContinueWith(dt => dt.Result .Select(subDir => FtpClient.PathCombine(directory, subDir)) .RunEach(subPath => paths.AddRange(ftpClient.ListDirectoryTask(subPath, true).Result)), TaskContinuationOptions.ExecuteSynchronously); } else { paths.AddRange(task.Result); } return(Task.Factory.FromResult <IList <string> >(paths)); }
/// <summary> /// FTP 서버에 <paramref name="directory"/> 가 존재하는지 확인합니다. /// </summary> /// <param name="ftpClient"></param> /// <param name="directory"></param> /// <returns></returns> public static Task <bool> DirectoryExistsTask(this FtpClient ftpClient, string directory) { if (IsDebugEnabled) { log.Debug("원격 디렉토리가 존재하는지 확인하는 비동기 작업을 directory=[{0}]", directory); } directory = FtpClient.AdjustDir(directory); if (directory == FtpClient.FTP_PATH_DELIMITER) { return(Task.Factory.FromResult(true)); } var tcs = new TaskCompletionSource <bool>(); var isSuccess = false; try { // 디렉토리에 대해 리스트를 조회해본다. // 조회가 된다면, 서브 디렉토리가 없더라도, 550 예외가 발생하지 않으므로, 디렉토리가 존재한다. // 디렉토리가 없다면 550 예외가 발생하므로, false 이다. // ftpClient.ListDirectoryTask(directory).Wait(); if (IsDebugEnabled) { log.Debug("원격 디렉토리가 존재합니다. directory=[{0}]", directory); } isSuccess = true; } catch (AggregateException age) { age.Handle(error => true); } tcs.TrySetResult(isSuccess); return(tcs.Task); }
/// <summary> /// FTP 서버에 디렉토리를 비동기 방식으로 생성합니다. /// </summary> /// <param name="ftpClient"></param> /// <param name="remoteDir"></param> /// <returns></returns> public static Task <bool> CreateDirectoryTask(this FtpClient ftpClient, string remoteDir) { if (IsDebugEnabled) { log.Debug("Create ftp directory. remoteDir=[{0}]", remoteDir); } string dir = FtpClient.AdjustDir(remoteDir); if (dir.Length < 2) { return(Task.Factory.FromResult(true)); } var uri = ftpClient.Hostname + dir; var tcs = new TaskCompletionSource <bool>(); var isSuccess = false; try { var task = SendRequestOnlyTask(ftpClient, uri, WebRequestMethods.Ftp.MakeDirectory); task.Wait(); isSuccess = task.IsCompleted; if (IsDebugEnabled) { log.Debug("FTP 서버에 Directory를 생성했습니다. remoteDir=[{0}]", remoteDir); } } catch (AggregateException age) { age.Handle(ex => true); } tcs.TrySetResult(isSuccess); return(tcs.Task); }
/// <summary> /// FTP 서버에 디렉토리를 비동기 방식으로 삭제합니다. /// </summary> /// <param name="ftpClient"></param> /// <param name="remoteDir"></param> /// <returns></returns> public static Task <bool> DeleteDirectoryTask(this FtpClient ftpClient, string remoteDir) { if (IsDebugEnabled) { log.Debug("Delete remote directory. remoteDir=[{0}]", remoteDir); } string uri = ftpClient.Hostname + FtpClient.AdjustDir(remoteDir); var tcs = new TaskCompletionSource <bool>(); var isSuccess = false; try { var task = ftpClient.SendRequestOnlyTask(uri, WebRequestMethods.Ftp.RemoveDirectory); task.Wait(); isSuccess = task.IsCompleted; } catch (AggregateException age) { age.Handle(ex => true); } tcs.TrySetResult(isSuccess); return(tcs.Task); }