|
|
@ -1,175 +1,34 @@
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
using ZeroLevel.Models;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZeroLevel.Network.FileTransfer
|
|
|
|
namespace ZeroLevel.Network.FileTransfer
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class FileReceiver
|
|
|
|
public sealed class FileReceiver
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private class FileWriter
|
|
|
|
private readonly string _baseFolder;
|
|
|
|
: IDisposable
|
|
|
|
private readonly ClientFolderNameMapper _nameMapper;
|
|
|
|
{
|
|
|
|
private readonly FileWriter _receiver;
|
|
|
|
private readonly FileStream _stream;
|
|
|
|
|
|
|
|
internal DateTime _writeTime { get; private set; } = DateTime.UtcNow;
|
|
|
|
|
|
|
|
private bool _gotCompleteMessage = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool GotCompleteMessage() => _gotCompleteMessage = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool ReadyToRemove()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_gotCompleteMessage)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return (DateTime.UtcNow - _writeTime).TotalSeconds > 15;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public FileWriter(string path)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Write(long offset, byte[] data)
|
|
|
|
public FileReceiver(IRouter router, string baseFolder, ClientFolderNameMapper nameMapper)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_stream.Position = offset;
|
|
|
|
_baseFolder = baseFolder ?? throw new Exception(nameof(baseFolder));
|
|
|
|
_stream.Write(data, 0, data.Length);
|
|
|
|
_nameMapper = nameMapper ?? throw new Exception(nameof(nameMapper));
|
|
|
|
_writeTime = DateTime.Now;
|
|
|
|
_receiver = new FileWriter(baseFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsTimeoutBy(TimeSpan period)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return (DateTime.Now - _writeTime) > period;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_stream.Flush();
|
|
|
|
|
|
|
|
_stream.Close();
|
|
|
|
|
|
|
|
_stream.Dispose();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private string _basePath;
|
|
|
|
|
|
|
|
private string _disk_prefix;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<long, FileWriter> _incoming = new Dictionary<long, FileWriter>();
|
|
|
|
if (false == router.ContainsRequestorInbox("__file_transfer_start_transfer__"))
|
|
|
|
private readonly object _locker = new object();
|
|
|
|
|
|
|
|
private long _cleanErrorsTaskId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public FileReceiver(string path, string disk_prefix = "DRIVE_")
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_disk_prefix = disk_prefix;
|
|
|
|
router.RegisterInbox<FileStartFrame>("__file_transfer_start_transfer__", (c, f) => _receiver.Incoming(f, nameMapper(c)));
|
|
|
|
_basePath = path;
|
|
|
|
|
|
|
|
_cleanErrorsTaskId = Sheduller.RemindEvery(TimeSpan.FromMinutes(1), CleanBadFiles);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false == router.ContainsRequestorInbox("__file_transfer_frame__"))
|
|
|
|
|
|
|
|
|
|
|
|
private void CleanBadFiles()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lock (_locker)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach (var pair in _incoming)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (pair.Value.IsTimeoutBy(TimeSpan.FromMinutes(3)) || pair.Value.ReadyToRemove())
|
|
|
|
router.RegisterInbox<FileFrame>("__file_transfer_frame__", (_, f) => _receiver.Incoming(f));
|
|
|
|
{
|
|
|
|
|
|
|
|
Remove(pair.Key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false == router.ContainsRequestorInbox("__file_transfer_complete_transfer__"))
|
|
|
|
public InvokeResult Incoming(FileStartFrame info, string clientFolderName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (false == _incoming.ContainsKey(info.UploadFileTaskId))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lock (_locker)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (false == _incoming.ContainsKey(info.UploadFileTaskId))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
string path = BuildFilePath(clientFolderName, info.FilePath);
|
|
|
|
router.RegisterInbox<FileEndFrame>("__file_transfer_complete_transfer__", (_, f) => _receiver.Incoming(f));
|
|
|
|
_incoming.Add(info.UploadFileTaskId, new FileWriter(path));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (false == router.ContainsRequestorInbox("__file_transfer_ping__"))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Log.Error("[FileReceiver]", ex);
|
|
|
|
router.RegisterInbox<bool>("__file_transfer_ping__", (_) => true);
|
|
|
|
return InvokeResult.Fault(ex.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return InvokeResult.Succeeding();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public InvokeResult Incoming(FileFrame chunk)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FileWriter stream;
|
|
|
|
|
|
|
|
if (_incoming.TryGetValue(chunk.UploadFileTaskId, out stream))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
stream.Write(chunk.Offset, chunk.Payload);
|
|
|
|
|
|
|
|
return InvokeResult.Succeeding();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return InvokeResult.Fault("File not expected.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Error("[FileReceiver]", ex);
|
|
|
|
|
|
|
|
return InvokeResult.Fault(ex.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public InvokeResult Incoming(FileEndFrame info)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lock (_locker)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FileWriter stream;
|
|
|
|
|
|
|
|
if (_incoming.TryGetValue(info.UploadFileTaskId, out stream))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
stream.GotCompleteMessage();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Error("[FileReceiver]", ex);
|
|
|
|
|
|
|
|
return InvokeResult.Fault(ex.Message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return InvokeResult.Succeeding();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void Remove(long uploadTaskId)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FileWriter stream;
|
|
|
|
|
|
|
|
if (_incoming.TryGetValue(uploadTaskId, out stream))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_incoming.Remove(uploadTaskId);
|
|
|
|
|
|
|
|
stream?.Dispose();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private string BuildFilePath(string client_folder_name, string clientPath)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(client_folder_name))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (false == Directory.Exists(_basePath))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Directory.CreateDirectory(_basePath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Path.Combine(_basePath, Path.GetFileName(clientPath));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string folder = Path.Combine(Path.Combine(_basePath, client_folder_name), Path.GetDirectoryName(clientPath).Replace(":", "_DRIVE"));
|
|
|
|
|
|
|
|
if (false == System.IO.Directory.Exists(folder))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
System.IO.Directory.CreateDirectory(folder);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Path.Combine(folder, Path.GetFileName(clientPath));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|