public void Enqueue(FTPDownloadJobInfo item) { Queue.Enqueue(item); if (Queue.Count == 1) { //restart the handler HandleNext(); } }
public bool Contains(FTPDownloadJobInfo item) { foreach (var queueItem in Queue) { if (queueItem.DestinationDirectory == item.DestinationDirectory && queueItem.ListItem == item.ListItem && queueItem.Remove == item.Remove && queueItem.TopMostDirectory == item.TopMostDirectory) { return true; } } return false; }
private void DeleteFile(FTPDownloadJobInfo jobToDelete) { if (jobToDelete.ListItem.Type == FTPListItemType.File) { MakeRequestStringResponse(WebRequestMethods.Ftp.DeleteFile, jobToDelete.ListItem.FullPath); } else if (jobToDelete.ListItem.Type == FTPListItemType.Directory) { MakeRequestStringResponse(WebRequestMethods.Ftp.RemoveDirectory, jobToDelete.ListItem.FullPath); } var deleteParent = true; foreach (var downloadJob in _downloadQueue.Queue) { if (downloadJob.DestinationDirectory == jobToDelete.DestinationDirectory) { deleteParent = false; break; } } var parentDirectoryPath = GetParentDirectory(jobToDelete.ListItem.FullPath); if (deleteParent && parentDirectoryPath != jobToDelete.TopMostDirectory) { DeleteFile(new FTPDownloadJobInfo { DestinationDirectory = Directory.GetParent(jobToDelete.DestinationDirectory).FullName, ListItem = new FTPListItem { FullPath = parentDirectoryPath, Type = FTPListItemType.Directory }, TopMostDirectory = jobToDelete.TopMostDirectory }); } }
public void DownloadAllReadyFiles(string ftpDirectory, string destinationDirectory, bool removeAfterDownload) { var files = GetFileListRecursive(ftpDirectory); foreach (var file in files) { //double check the file size to ensure the file is not being copied on the ftp server var size = GetFileSize(file.FullPath); if (size == file.Size) { //get the relative path the file will go into var relativePath = file.FullPath.Substring(ftpDirectory.Length + 1); //remove the filename so we just have its destination directory relativePath = relativePath.Remove(relativePath.Length - file.Name.Length); //construct the local path where the file should be downloaded to var fileDestination = Path.Combine(destinationDirectory, relativePath); var newDownloadJob = new FTPDownloadJobInfo { DestinationDirectory = fileDestination, ListItem = file, Remove = removeAfterDownload, TopMostDirectory = String.Format("{0}", ftpDirectory) }; if (!_downloadQueue.Contains(newDownloadJob)) { _downloadQueue.Enqueue(newDownloadJob); } } } }
private void DownloadFile(FTPDownloadJobInfo job) { try { if (!Directory.Exists(job.DestinationDirectory)) { Directory.CreateDirectory(job.DestinationDirectory); } var destinationFilePath = Path.Combine(job.DestinationDirectory, job.ListItem.Name); if (!File.Exists(destinationFilePath)) { var bytesRead = 0; var buffer = new byte[2048]; var response = MakeRequest(WebRequestMethods.Ftp.DownloadFile, job.ListItem.FullPath); var reader = response.GetResponseStream(); var fileStream = new FileStream(destinationFilePath, FileMode.Create); while (true) { bytesRead = reader.Read(buffer, 0, buffer.Length); if (bytesRead == 0) break; fileStream.Write(buffer, 0, bytesRead); } fileStream.Close(); response.Close(); //TODO verify the file if (job.Remove) { DeleteFile(job); } } } catch (Exception e) { LogError.LogMessage(e); } }