Skip to content

Commit

Permalink
final commit release
Browse files Browse the repository at this point in the history
  • Loading branch information
samudiogo committed Nov 16, 2016
1 parent 0676cbe commit 37f5dc8
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 59 deletions.
71 changes: 71 additions & 0 deletions SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/FtpDestiny.cs
Original file line number Diff line number Diff line change
@@ -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}";
}
}
65 changes: 7 additions & 58 deletions SdTech.FtpUploader/SdTech.FtpUploader.FtpUpload/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Net;
using static System.Text.Encoding;
using System.Reflection;
using System.Text.RegularExpressions;

namespace SdTech.FtpUploader.FtpUpload
{
Expand All @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,31 @@
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SdTech.FtpUploader.FtpUpload</RootNamespace>
<AssemblyName>SdTech.FtpUploader.FtpUpload</AssemblyName>
<AssemblyName>FtpUpload</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>D:\publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>false</MapFileExtensions>
<SupportUrl>http://www.sdtech.com.br</SupportUrl>
<ProductName>FTP Uploader</ProductName>
<PublisherName>SD TECH Consultorias e Serviços</PublisherName>
<AutorunEnabled>true</AutorunEnabled>
<TrustUrlParameters>true</TrustUrlParameters>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -32,6 +53,12 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>true</GenerateManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -43,12 +70,47 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FtpDestiny.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<PublishFile Include="SdTech.FtpUploader.FtpUpload.exe.config">
<Visible>False</Visible>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Auto</PublishState>
<IncludeHash>False</IncludeHash>
<FileType>File</FileType>
</PublishFile>
<PublishFile Include="SdTech.FtpUploader.FtpUpload.exe.manifest">
<Visible>False</Visible>
<Group>
</Group>
<TargetPath>
</TargetPath>
<PublishState>Auto</PublishState>
<IncludeHash>False</IncludeHash>
<FileType>ManifestEntryPoint</FileType>
</PublishFile>
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.5.2">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.5.2 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down

0 comments on commit 37f5dc8

Please sign in to comment.