Replaced FTP dll with FTPClient class

This commit is contained in:
miku-666
2022-12-23 20:20:29 +01:00
parent 44e92c3507
commit b23aa91a93
6 changed files with 300 additions and 18 deletions

View File

@@ -0,0 +1,283 @@
using System;
using System.IO;
using System.Net;
namespace PckStudio.Classes.Misc
{
public class FTPClient : IDisposable
{
private const int bufferSize = 2048;
private Uri hostUri;
private NetworkCredential credentials;
private FtpWebRequest request = null;
private FtpWebResponse response = null;
private Stream _stream = null;
public FTPClient(string host, string username)
: this(new Uri(host), username)
{
}
public FTPClient(Uri uri, string username)
: this(uri, username, string.Empty)
{
}
public FTPClient(string host, string username, string password)
: this(new Uri(host), username, password)
{
}
public FTPClient(Uri uri, string username, string password)
{
hostUri = uri;
credentials = new NetworkCredential(username, password);
}
/// <summary>
/// Creates a new FTP Request
/// </summary>
/// <param name="uri"></param>
/// <param name="credentials"></param>
/// <param name="method">See <see cref="WebRequestMethods.Ftp"/></param>
/// <returns><see cref="FtpWebRequest"/></returns>
public static FtpWebRequest CreateFTPWebRequest(Uri uri, ICredentials credentials, string method)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = credentials;
request.Method = method;
return request;
}
public void DownloadFile(string remoteFilepath, string localFile)
{
try
{
request = CreateFTPWebRequest(new Uri(hostUri, remoteFilepath), credentials, WebRequestMethods.Ftp.DownloadFile);
//request = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile);
//request.Credentials = credentials;
//request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
response = (FtpWebResponse)request.GetResponse();
_stream = response.GetResponseStream();
FileStream fileStream = new FileStream(localFile, FileMode.OpenOrCreate);
byte[] buffer = new byte[Convert.ToInt32(GetFileSize(remoteFilepath))];
int num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath)));
try
{
while (num > 0)
{
fileStream.Write(buffer, 0, num);
num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath)));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
fileStream.Close();
_stream.Close();
response.Close();
request = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void UploadFile(string localFile, string remoteFile)
{
try
{
request = CreateFTPWebRequest(new Uri(hostUri, remoteFile), credentials, WebRequestMethods.Ftp.UploadFile);
//request = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile);
//request.Credentials = credentials;
//request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
_stream = request.GetRequestStream();
FileStream fileStream = new FileStream(localFile, FileMode.Open);
byte[] buffer = new byte[fileStream.Length];
int num = fileStream.Read(buffer, 0, (int)fileStream.Length);
try
{
while (num != 0)
{
_stream.Write(buffer, 0, num);
num = fileStream.Read(buffer, 0, (int)fileStream.Length);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
fileStream.Close();
_stream.Close();
request = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public string[] ListDirectory(string directory)
{
try
{
request = CreateFTPWebRequest(new Uri(hostUri, directory), credentials, WebRequestMethods.Ftp.ListDirectory);
//request = (FtpWebRequest)WebRequest.Create(host + "/" + directory);
//request.Credentials = credentials;
//request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
response = (FtpWebResponse)request.GetResponse();
_stream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(_stream);
string text = string.Empty;
try
{
while (streamReader.Peek() != -1)
{
text += streamReader.ReadLine() + "|";
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
streamReader.Close();
_stream.Close();
response.Close();
request = null;
try
{
return text.Split("|".ToCharArray());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return Array.Empty<string>();
}
public void Rename(string name, string newName)
{
try
{
request = CreateFTPWebRequest(new Uri(hostUri, name), credentials, WebRequestMethods.Ftp.Rename);
//request = (FtpWebRequest)WebRequest.Create(host + "/" + name);
//request.Credentials = credentials;
//request.Method = WebRequestMethods.Ftp.Rename;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
request.RenameTo = newName;
response = (FtpWebResponse)request.GetResponse();
response.Close();
request = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void DeleteFile(string filename)
{
try
{
request = CreateFTPWebRequest(new Uri(hostUri, filename), credentials, WebRequestMethods.Ftp.DeleteFile);
//request = (FtpWebRequest)WebRequest.Create(host + "/" + filename);
//request.Credentials = credentials;
//request.Method = WebRequestMethods.Ftp.DeleteFile;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
response = (FtpWebResponse)request.GetResponse();
response.Close();
request = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public void CreateDirectory(string name)
{
try
{
request = CreateFTPWebRequest(new Uri(hostUri, name), credentials, WebRequestMethods.Ftp.MakeDirectory);
//request = (FtpWebRequest)WebRequest.Create(host + "/" + name);
//request.Credentials = credentials;
//request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
response = (FtpWebResponse)request.GetResponse();
response.Close();
request = null;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public long GetFileSize(string filepath)
{
FtpWebRequest ftpWebRequest = CreateFTPWebRequest(new Uri(hostUri, filepath), credentials, WebRequestMethods.Ftp.GetFileSize);
//FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(host + "/" + fileName);
//ftpWebRequest.Credentials = credentials;
//ftpWebRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ftpWebRequest.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse();
long contentLength = response.ContentLength;
response.Close();
return contentLength;
}
public void Dispose()
{
_stream.Dispose();
response.Dispose();
request = null;
response = null;
_stream = null;
}
}
}

View File

@@ -1,5 +1,4 @@
using FileTransferProtocolLib;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
@@ -7,6 +6,7 @@ using System.IO;
using System.Net;
using System.Windows.Forms;
using MetroFramework.Forms;
using PckStudio.Classes.Misc;
namespace PckStudio.Forms
{
@@ -292,7 +292,7 @@ namespace PckStudio.Forms
if (openPCK.ShowDialog() == DialogResult.OK)
{
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "");
client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString());
MessageBox.Show("PCK Replaced!");
}
@@ -341,7 +341,7 @@ namespace PckStudio.Forms
if (listViewPCKS.SelectedItems.Count != 0)
{
buttonMode("loading");
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "");
client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString());
MessageBox.Show("PCK Replaced!");
}

View File

@@ -1,5 +1,4 @@
using FileTransferProtocolLib;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
@@ -7,6 +6,7 @@ using System.IO;
using System.Net;
using System.Windows.Forms;
using MetroFramework.Forms;
using PckStudio.Classes.Misc;
namespace PckStudio.Forms
{
@@ -292,7 +292,7 @@ namespace PckStudio.Forms
if (openPCK.ShowDialog() == DialogResult.OK)
{
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "");
client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString());
MessageBox.Show("PCK Replaced!");
}
@@ -341,7 +341,7 @@ namespace PckStudio.Forms
if (listViewPCKS.SelectedItems.Count != 0)
{
buttonMode("loading");
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "");
client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString());
MessageBox.Show("PCK Replaced!");
}

View File

@@ -1,5 +1,4 @@
using FileTransferProtocolLib;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
@@ -7,10 +6,12 @@ using System.IO;
using System.IO.Compression;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using PckStudio.Classes.FileTypes;
using PckStudio.Classes.IO.PCK;
using PckStudio.Classes.IO.ARC;
using System.Diagnostics;
using PckStudio.Classes.Misc;
namespace PckStudio.Forms
{
@@ -346,7 +347,7 @@ namespace PckStudio.Forms
if (openPCK.ShowDialog() == DialogResult.OK)
{
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443");
client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString());
if(TextBoxPackImage.Text != "")
{
@@ -411,7 +412,7 @@ namespace PckStudio.Forms
if (listViewPCKS.SelectedItems.Count != 0)
{
buttonMode("loading");
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443");
client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString());
if (TextBoxPackImage.Text != "")
{
@@ -436,7 +437,7 @@ namespace PckStudio.Forms
private void GetARCFromConsole()
{
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443");
client.DownloadFile(dlcPath + "../../Common/Media/MediaWiiU.arc", Program.AppData + "MediaWiiU.arc");
archive = ARCFileReader.Read(new MemoryStream(File.ReadAllBytes(Program.AppData + "MediaWiiU.arc")));
}
@@ -451,7 +452,7 @@ namespace PckStudio.Forms
private void SendARCToConsole()
{
FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443");
FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443");
MemoryStream ms = new MemoryStream();
ARCFileWriter.Write(ms, archive);
File.WriteAllBytes(Program.AppData + "MediaWiiU.arc", ms.ToArray());

View File

@@ -105,9 +105,6 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="filetransferprotocollib">
<HintPath>..\filetransferprotocollib.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
@@ -161,6 +158,7 @@
<Compile Include="Classes\IO\COL\COLFileWriter.cs" />
<Compile Include="Classes\IO\Sounds\SoundIO.cs" />
<Compile Include="Classes\IO\Sounds\Sounds.cs" />
<Compile Include="Classes\Misc\FTPClient.cs" />
<Compile Include="Classes\Models\DefaultModels\Steve64x64Model.cs" />
<Compile Include="Classes\Utils\3DS\3DSUtil.cs" />
<Compile Include="Classes\Utils\ARC\ARCUtil.cs" />

Binary file not shown.