public async Task <IEnumerable <FTPFile> > ListFilesAsync() { var ftpUrl = Utils.FormatFTPUrl(Host, Path, null, Port); var request = CreateFTPRequest(ftpUrl, WebRequestMethods.Ftp.ListDirectoryDetails); FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string line = null; var ret = new List <FTPFile>(); while ((line = await reader.ReadLineAsync()) != null) { var file = FTPFile.CreateFTPFile(line); if (file != null) { ret.Add(file); } } reader.Close(); response.Close(); return(ret); }
public void DeleteFTPFile(FTPFile ftpFile) { if (ftpFile == null) { throw new ArgumentNullException(nameof(ftpFile)); } _context.FTPFiles.Remove(ftpFile); }
/// <summary> /// Function to start a download /// </summary> /// <param name="orgremoteFile"></param> /// <param name="remotefile"></param> /// <param name="localfile"></param> public void Download(string orgremoteFile, string remotefile, string localfile) { string name = System.IO.Path.GetFileName(remotefile); string folder = orgremoteFile.Substring("remote:".Length); folder = folder.Substring(0, folder.Length - (name.Length + 1)); string[] subitems = folder.Split(new char[] { '?' }); if (subitems[4] == string.Empty) { subitems[4] = "/"; } bool fileExists = false; string tmpDir = string.Empty; FTPFile[] files; try { tmpDir = subitems[4]; Connection.ChDir(tmpDir); files = Connection.DirDetails(); //(tmpDir); for (int i = 0; i < files.Length; ++i) { FTPFile file = files[i]; if (file.Dir) { continue; } if (String.Compare(file.Name, name, true) == 0) { fileExists = true; break; } } if (!fileExists) { if (Connection.Connected) { Connection.QuitImmediately(); } Busy = false; return; } } catch (Exception ex) { Log.Error("FtpConnectionCache:Download {0}", ex.Message); return; } Log.Debug("FTPConnection: Download {0} from {1} to {2}", remotefile, tmpDir, localfile); LocalFileName = localfile; RemoteFileName = remotefile; OriginalRemoteFileName = orgremoteFile; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(StartDownLoad); worker.RunWorkerAsync(); }
public void CreateFTPFile(FTPFile ftpFile) { if (ftpFile == null) { throw new ArgumentNullException(nameof(ftpFile)); } _context.FTPFiles.Add(ftpFile); }
public Task <TaskResult> FTPDownloadFile(FTPFile file, string targetPath, Action <int> ProgressChange) { Task <TaskResult> res = null; res = ServiceRunTask(service => { return(ftp_service.FTPDownloadFile(targetPath, file, ProgressChange)); }); return(res); }
private FsPermission GetPermissions(FTPFile ftpFile) { FsAction user; FsAction group; FsAction others; user = GetFsAction(FTPFile.UserAccess, ftpFile); group = GetFsAction(FTPFile.GroupAccess, ftpFile); others = GetFsAction(FTPFile.WorldAccess, ftpFile); return(new FsPermission(user, group, others)); }
public Boolean FTPDownloadFile(string targetFilePath, FTPFile file, Action <int> ProgressChange) { bool result = false; FileStream outputStream = new FileStream(targetFilePath + "\\" + file.file_name, FileMode.Create); Stream ftpStream = null; FtpWebResponse response = null; try { long filesize = FtpFileSize(file.file_folder + "/" + file.file_name); FtpWebRequest reqFTP = GetFtpWebRequest(file.file_folder + "/" + file.file_name); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; response = (FtpWebResponse)reqFTP.GetResponse(); ftpStream = response.GetResponseStream(); int bufferSize = 16384; int readCount; byte[] buffer = new byte[bufferSize]; long countWriteBites = 0; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); countWriteBites = countWriteBites + readCount; ProgressChange((int)(countWriteBites * 100 / filesize)); readCount = ftpStream.Read(buffer, 0, bufferSize); } result = true; } catch (Exception) { throw; } finally { if (ftpStream != null) { ftpStream.Close(); } if (outputStream != null) { outputStream.Close(); } if (response != null) { response.Close(); } } return(result); }
/// <summary> /// Convert the file information in FTPFile to a /// <see cref="Org.Apache.Hadoop.FS.FileStatus"/> /// object. /// </summary> /// <param name="ftpFile"/> /// <param name="parentPath"/> /// <returns>FileStatus</returns> private FileStatus GetFileStatus(FTPFile ftpFile, Path parentPath) { long length = ftpFile.GetSize(); bool isDir = ftpFile.IsDirectory(); int blockReplication = 1; // Using default block size since there is no way in FTP client to know of // block sizes on server. The assumption could be less than ideal. long blockSize = DefaultBlockSize; long modTime = ftpFile.GetTimestamp().GetTimeInMillis(); long accessTime = 0; FsPermission permission = GetPermissions(ftpFile); string user = ftpFile.GetUser(); string group = ftpFile.GetGroup(); Path filePath = new Path(parentPath, ftpFile.GetName()); return(new FileStatus(length, isDir, blockReplication, blockSize, modTime, accessTime , permission, user, group, filePath.MakeQualified(this))); }
private FsAction GetFsAction(int accessGroup, FTPFile ftpFile) { FsAction action = FsAction.None; if (ftpFile.HasPermission(accessGroup, FTPFile.ReadPermission)) { action.Or(FsAction.Read); } if (ftpFile.HasPermission(accessGroup, FTPFile.WritePermission)) { action.Or(FsAction.Write); } if (ftpFile.HasPermission(accessGroup, FTPFile.ExecutePermission)) { action.Or(FsAction.Execute); } return(action); }
public void Download(FTPFile file, string destination) { connection.DownloadFile(destination, file.Name); }
/// <summary> /// Helper method for dumping a listing /// </summary> /// <param name="list"> directory listing to print /// </param> private void Print(FTPFile[] list) { log.Debug("Directory listing:"); for (int i = 0; i < list.Length; i++) { log.Debug(list[i].ToString()); } log.Debug("Listing complete"); }