public List <string> getDownloadFiles(IHutFTPClient client, string srcpath, ref List <string> exceptions) { List <string> files = client.GetFiles(srcpath).ToList(); if (files.Count == 0) { exceptions.Add(@"No File(s) in target Directory"); } return(files); }
private void TransferFiles(IHutFTPClient client, List <string> exceptions) { int transferfiles = 1; try { if (serverinfo.Direction == HutFTPTransferDirection.Upload) { // check local path if (!Directory.Exists(Path.GetDirectoryName(srcpath))) { TransferStatus = @"source path is not exists"; end = DateTime.Now; result = HutTaskActionResult.Fail; ActionStatus = HutTaskActionStatus.Complete; return; } if (client is HutFTPClient && ServerInfo.Address.StartsWith(@"s")) { transferstatus = @"can't open server sftp:// without SSL: " + serverinfo.Address; return; } List <string> uploaded = new List <string>(); do { // upload foreach (string srcfile in getUploadFiles(srcpath, ref exceptions).Where(w => !uploaded.Contains(Path.GetFileName(w)))) { string dstfile = string.Format(@"{0}/{1}", dstpath, Option.rename(Path.GetFileName(srcfile))).Replace(@"//", @"/"); TransferStatus = @"(" + ((uploaded.Count + 1).ToString()) + @")Upload Start : " + srcfile; client.upload(srcfile, dstfile); TransferStatus = @"(" + ((uploaded.Count + 1).ToString()) + @")Upload Complete: " + client.Addr + dstfile; uploaded.Add(Path.GetFileName(srcfile)); } // check one more if (getUploadFiles(srcpath, ref exceptions).Count <= uploaded.Count) { break; } Thread.Sleep(300); } while (true); TransferStatus = @"Total Sent Files: " + (uploaded.Count).ToString(); // cannot sent. if (uploaded.Count == 0) { exceptions.Add(Name + @": , No Matched File in Option = " + Option.SearchRule.ToString()); } // options if ((option.StorageMethod & HutStorageMethod.DeleteFile) == HutStorageMethod.DeleteFile) { foreach (var src in uploaded.Select((s, i) => new { File = Path.Combine(srcpath, s), Index = (i + 1) })) { File.Delete(src.File); TransferStatus = @"(" + (src.Index) + @")Delete Uploaded File: " + src.File; } } } // DOWNLOAD else { foreach (string srcfile in getDownloadFiles(client, srcpath, ref exceptions)) { string dstfile = Path.Combine(dstpath, Option.rename(Path.GetFileName(srcfile))); TransferStatus = @"(" + (transferfiles.ToString()) + @")Download Start : " + client.Addr + srcpath + srcfile; client.download(string.Format(@"{0}/{1}", srcpath, srcfile), dstfile); TransferStatus = @"(" + (transferfiles.ToString()) + @")Download Complete: " + dstfile; client.dele(string.Format(@"{0}/{1}", srcpath, srcfile)); TransferStatus = @"(" + (transferfiles.ToString()) + @")Delete Complete: " + srcfile; transferfiles++; } TransferStatus = @"Total Received Files: " + (transferfiles - 1).ToString(); } TransferStatus = @"FTP Disconnected"; } catch (Exception e) { //string dstfile = string.Format(@"{0}\{1}", dstpath, Option.rename(Path.GetFileName(srcfile))); TransferStatus = @"File Transfer Error Raised: "; exceptions.Add(TransferStatus); TransferStatus = @" Source Path: " + srcpath; exceptions.Add(TransferStatus); TransferStatus = @" Target Path: " + serverinfo.Address + @"/" + dstpath; exceptions.Add(TransferStatus); TransferStatus = @" FTP Error(s): " + e.Message; TransferStatus = @" " + e.StackTrace; exceptions.Add(TransferStatus); } finally { TransferStatus = @"End Transfer Action: " + Name; updateStatus(exceptions); } }