setup some sftp upload stuff
This commit is contained in:
parent
c8d1427472
commit
ae424eaa5f
@ -25,6 +25,9 @@
|
||||
<Reference Include="Gitea">
|
||||
<HintPath>Resources\Gitea.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="WinSCPnet">
|
||||
<HintPath>Resources\WinSCPnet.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -12,6 +12,7 @@ namespace EftPatchHelper.Interfaces
|
||||
public string ServiceName { get; set; }
|
||||
public string HubEntryText { get; set; }
|
||||
public FileInfo UploadFileInfo { get; }
|
||||
public bool AddHubEntry { get; }
|
||||
public string GetLink();
|
||||
public Task<bool> UploadAsync(IProgress<double>? progress = null);
|
||||
}
|
||||
|
@ -1,7 +1,11 @@
|
||||
namespace EftPatchHelper.Model
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace EftPatchHelper.Model
|
||||
{
|
||||
public class DownloadMirror
|
||||
{
|
||||
[JsonIgnore]
|
||||
public bool AddHubEntry { get; set; }
|
||||
public string Link { get; set; }
|
||||
public string Hash { get; set; }
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ namespace EftPatchHelper.Model
|
||||
public string DisplayName { get; set; }
|
||||
public string ServiceName { get; set; }
|
||||
public string HubEntryText { get; set; }
|
||||
public bool AddHubEntry { get; }
|
||||
|
||||
public GoFileUpload(FileInfo file, string apiToken, string folderId)
|
||||
{
|
||||
@ -31,6 +32,7 @@ namespace EftPatchHelper.Model
|
||||
ServiceName = "GoFile";
|
||||
DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}";
|
||||
HubEntryText = $"Download from {ServiceName}";
|
||||
AddHubEntry = true;
|
||||
}
|
||||
|
||||
public string GetLink()
|
||||
|
@ -17,6 +17,8 @@ namespace EftPatchHelper.Model
|
||||
public string DisplayName { get; set; }
|
||||
public string ServiceName { get; set; }
|
||||
public string HubEntryText { get; set; }
|
||||
public bool AddHubEntry { get; }
|
||||
|
||||
|
||||
public MegaUpload(FileInfo file, string email, string password, string mfaKey = null)
|
||||
{
|
||||
@ -27,6 +29,7 @@ namespace EftPatchHelper.Model
|
||||
ServiceName = "Mega";
|
||||
DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}";
|
||||
HubEntryText = $"Download from {ServiceName}";
|
||||
AddHubEntry = true;
|
||||
}
|
||||
|
||||
private async Task<bool> CheckLoginStatus()
|
||||
|
72
EftPatchHelper/EftPatchHelper/Model/SftpUpload.cs
Normal file
72
EftPatchHelper/EftPatchHelper/Model/SftpUpload.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using EftPatchHelper.Interfaces;
|
||||
using WinSCP;
|
||||
|
||||
namespace EftPatchHelper.Model;
|
||||
|
||||
public class SftpUpload : IFileUpload
|
||||
{
|
||||
private readonly SftpUploadInfo _sftpInfo;
|
||||
private readonly SessionOptions _sessionOptions;
|
||||
public string DisplayName { get; set; }
|
||||
public string ServiceName { get; set; }
|
||||
public string HubEntryText { get; set; }
|
||||
public bool AddHubEntry { get; }
|
||||
|
||||
public FileInfo UploadFileInfo { get; }
|
||||
|
||||
public SftpUpload(FileInfo file, SftpUploadInfo sftpInfo)
|
||||
{
|
||||
UploadFileInfo = file;
|
||||
_sftpInfo = sftpInfo;
|
||||
|
||||
_sessionOptions = new SessionOptions
|
||||
{
|
||||
Protocol = Protocol.Sftp,
|
||||
HostName = _sftpInfo.Hostname,
|
||||
PortNumber = _sftpInfo.Port,
|
||||
SshHostKeyFingerprint = _sftpInfo.HostKey
|
||||
};
|
||||
|
||||
ServiceName = _sftpInfo.Hostname;
|
||||
DisplayName = $"{ServiceName} Upload: {UploadFileInfo.Name}";
|
||||
HubEntryText = $"Download from {ServiceName}";
|
||||
AddHubEntry = false;
|
||||
}
|
||||
|
||||
public string GetLink()
|
||||
{
|
||||
return $"{_sftpInfo.HttpPath}/${UploadFileInfo.Name}";
|
||||
}
|
||||
|
||||
public Task<bool> UploadAsync(IProgress<double>? progress = null)
|
||||
{
|
||||
TransferOptions transferOptions = new TransferOptions()
|
||||
{
|
||||
TransferMode = TransferMode.Binary,
|
||||
};
|
||||
|
||||
using Session session = new Session();
|
||||
using var uploadStream = UploadFileInfo.OpenRead();
|
||||
|
||||
if (progress != null)
|
||||
{
|
||||
session.FileTransferProgress += (_, args) => progress.Report(args.FileProgress);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
session.Open(_sessionOptions);
|
||||
|
||||
session.PutFile(uploadStream, _sftpInfo.uploadPath, transferOptions);
|
||||
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
12
EftPatchHelper/EftPatchHelper/Model/SftpUploadInfo.cs
Normal file
12
EftPatchHelper/EftPatchHelper/Model/SftpUploadInfo.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace EftPatchHelper.Model;
|
||||
|
||||
public class SftpUploadInfo
|
||||
{
|
||||
public string Username { get; set; } = "";
|
||||
public string Password { get; set; } = "";
|
||||
public string HostKey { get; set; } = "";
|
||||
public string Hostname { get; set; } = "";
|
||||
public int Port { get; set; } = 0;
|
||||
public string uploadPath { get; set; } = "";
|
||||
public string HttpPath { get; set; } = "";
|
||||
}
|
BIN
EftPatchHelper/EftPatchHelper/Resources/WinSCPnet.dll
Normal file
BIN
EftPatchHelper/EftPatchHelper/Resources/WinSCPnet.dll
Normal file
Binary file not shown.
@ -58,6 +58,12 @@ namespace EftPatchHelper.Tasks
|
||||
|
||||
foreach (var pair in _options.MirrorList)
|
||||
{
|
||||
if (!pair.Value.AddHubEntry)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
var displayText = pair.Key;
|
||||
var link = pair.Value.Link;
|
||||
|
||||
@ -90,8 +96,12 @@ namespace EftPatchHelper.Tasks
|
||||
static string BytesToString(long byteCount)
|
||||
{
|
||||
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
|
||||
|
||||
if (byteCount == 0)
|
||||
{
|
||||
return "0" + suf[0];
|
||||
}
|
||||
|
||||
long bytes = Math.Abs(byteCount);
|
||||
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
|
||||
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
|
||||
@ -134,8 +144,9 @@ namespace EftPatchHelper.Tasks
|
||||
}
|
||||
else
|
||||
{
|
||||
DownloadMirror mirror = new DownloadMirror()
|
||||
DownloadMirror mirror = new DownloadMirror
|
||||
{
|
||||
AddHubEntry = pair.Key.AddHubEntry,
|
||||
Link = pair.Key.GetLink(),
|
||||
Hash = GetFileHash(pair.Key.UploadFileInfo)
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user