From 37f5dc84947f483dd748d225c2651aa95c797c84 Mon Sep 17 00:00:00 2001 From: Samuel Diogo Date: Tue, 15 Nov 2016 22:13:20 -0200 Subject: [PATCH] final commit release --- .../FtpDestiny.cs | 71 +++++++++++++++++++ .../SdTech.FtpUploader.FtpUpload/Program.cs | 65 ++--------------- .../SdTech.FtpUploader.FtpUpload.csproj | 64 ++++++++++++++++- 3 files changed, 141 insertions(+), 59 deletions(-) create mode 100644 SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/FtpDestiny.cs diff --git a/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/FtpDestiny.cs b/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/FtpDestiny.cs new file mode 100644 index 0000000..9d3e532 --- /dev/null +++ b/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/FtpDestiny.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace SdTech.FtpUploader.FtpUpload +{ + public class FtpDestiny + { + + private string pEndereco; + public string PEndereco + { + get { return pEndereco; } + set + { + // TODO Test if already have a ftp + if (string.IsNullOrWhiteSpace(value)) throw new Exception("empty uri"); + value = SanitizeEndereco(value); + if (!Uri.IsWellFormedUriString(value, UriKind.RelativeOrAbsolute)) throw new UriFormatException(); + pEndereco = value; + } + } + + private static string SanitizeEndereco(string end) + { + end = end.TrimEnd('/'); + end = Regex.Replace(end, "^(https|http)", "ftp"); + if (!Regex.Match(end, "^(ftp)://.*$").Success) return $"ftp://{end}"; + return end; + + } + + private string pUsuario; + public string PUsuario + { + get { return pUsuario; } + set + { + if (!string.IsNullOrWhiteSpace(value)) pUsuario = value; + else throw new Exception("empty user"); + + } + } + private string pSenha; + public string PSenha + { + get { return pSenha; } + set + { + if (!string.IsNullOrWhiteSpace(value)) pSenha = value; + else throw new Exception("empty password"); + } + } + + private string pEnviar; + public string PEnviar + { + get { return pEnviar; } + set + { + if (!string.IsNullOrWhiteSpace(value)) pEnviar = value; + else throw new Exception("empty pEnviar"); + } + } + + public override string ToString() => $"End: {PEndereco}\nUsuario: {PUsuario}\nSenha: {PSenha}\nEnviar: {PEnviar}"; + } +} diff --git a/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/Program.cs b/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/Program.cs index 833fbc1..ef538e8 100644 --- a/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/Program.cs +++ b/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/Program.cs @@ -3,7 +3,6 @@ using System.Net; using static System.Text.Encoding; using System.Reflection; -using System.Text.RegularExpressions; namespace SdTech.FtpUploader.FtpUpload { @@ -23,27 +22,28 @@ static void Main(string[] args) Console.WriteLine(ftpParams); - var request = (FtpWebRequest)WebRequest.Create(ftpParams.PEndereco); + var remoteFile = $@"{ftpParams.PEndereco}/{ftpParams.PEnviar.Substring(ftpParams.PEnviar.LastIndexOf(@"\") + 1)}"; + + var request = (FtpWebRequest)WebRequest.Create(remoteFile); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(ftpParams.PUsuario, ftpParams.PSenha); + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; byte[] fileContent = null; using (var strReader = new StreamReader(ftpParams.PEnviar)) { fileContent = UTF8.GetBytes(strReader.ReadToEnd()); } - request.ContentLength = fileContent.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(fileContent, 0, fileContent.Length); } - var response = (FtpWebResponse)request.GetResponse(); - - Console.WriteLine($"Upload file complete, status {response.StatusDescription}"); - + } catch (UriFormatException uriExcepion) @@ -57,59 +57,8 @@ static void Main(string[] args) LogError.Log(ex, args); } - } - } - - public class FtpDestiny - { - private string pEndereco; - public string PEndereco - { - get { return pEndereco; } - set - { - if (string.IsNullOrWhiteSpace(value)) throw new Exception("empty uri"); - if (!Regex.Match(value, "^(https?|ftp)://.*$").Success) value = $"ftp://{value.TrimEnd('/')}"; - if (!Uri.IsWellFormedUriString(value, UriKind.RelativeOrAbsolute)) throw new UriFormatException(); - pEndereco = value; - - } - } - - private string pUsuario; - public string PUsuario - { - get { return pUsuario; } - set - { - if (!string.IsNullOrWhiteSpace(value)) pUsuario = value; - else throw new Exception("empty user"); - - } - } - private string pSenha; - public string PSenha - { - get { return pSenha; } - set - { - if (!string.IsNullOrWhiteSpace(value)) pSenha = value; - else throw new Exception("empty password"); - } - } - private string pEnviar; - public string PEnviar - { - get { return pEnviar; } - set - { - if (!string.IsNullOrWhiteSpace(value)) pEnviar = value; - else throw new Exception("empty pEnviar"); - } } - - public override string ToString() => $"End: {PEndereco}\nUsuario: {PUsuario}\nSenha: {PSenha}\nEnviar: {PEnviar}"; } public class LogError diff --git a/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/SdTech.FtpUploader.FtpUpload.csproj b/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/SdTech.FtpUploader.FtpUpload.csproj index 8598183..9e18f57 100644 --- a/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/SdTech.FtpUploader.FtpUpload.csproj +++ b/SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/SdTech.FtpUploader.FtpUpload.csproj @@ -8,10 +8,31 @@ Exe Properties SdTech.FtpUploader.FtpUpload - SdTech.FtpUploader.FtpUpload + FtpUpload v4.5.2 512 true + false + D:\publish\ + true + Disk + false + Foreground + 7 + Days + false + false + false + http://www.sdtech.com.br + FTP Uploader + SD TECH Consultorias e Serviços + true + true + 0 + 1.0.0.0 + false + true + true AnyCPU @@ -32,6 +53,12 @@ prompt 4 + + true + + + true + @@ -43,12 +70,47 @@ + + + + False + + + + + Auto + False + File + + + False + + + + + Auto + False + ManifestEntryPoint + + + + + False + Microsoft .NET Framework 4.5.2 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + +