You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Network/FileTransfer/FileReceiver.cs

177 lines
5.5 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.IO;
6 years ago
using ZeroLevel.Models;
6 years ago
6 years ago
namespace ZeroLevel.Network.FileTransfer
6 years ago
{
public class FileReceiver
{
private class FileWriter
: IDisposable
{
private readonly FileStream _stream;
internal DateTime _writeTime { get; private set; } = DateTime.UtcNow;
6 years ago
private bool _gotCompleteMessage = false;
public bool GotCompleteMessage() => _gotCompleteMessage = true;
public bool ReadyToRemove()
{
if (_gotCompleteMessage)
{
return (DateTime.UtcNow - _writeTime).TotalSeconds > 15;
}
return false;
}
6 years ago
public FileWriter(string path)
{
_stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
}
public void Write(long offset, byte[] data)
{
_stream.Position = offset;
_stream.Write(data, 0, data.Length);
_writeTime = DateTime.Now;
}
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;
6 years ago
private readonly Dictionary<long, FileWriter> _incoming = new Dictionary<long, FileWriter>();
6 years ago
private readonly object _locker = new object();
private long _cleanErrorsTaskId;
public FileReceiver(string path, string disk_prefix = "DRIVE_")
{
_disk_prefix = disk_prefix;
_basePath = path;
_cleanErrorsTaskId = Sheduller.RemindEvery(TimeSpan.FromMinutes(1), CleanBadFiles);
}
private void CleanBadFiles()
{
lock (_locker)
{
foreach (var pair in _incoming)
{
6 years ago
if (pair.Value.IsTimeoutBy(TimeSpan.FromMinutes(3)) || pair.Value.ReadyToRemove())
6 years ago
{
Remove(pair.Key);
}
}
}
}
public InvokeResult Incoming(FileStartFrame info, string clientFolderName)
6 years ago
{
6 years ago
try
6 years ago
{
6 years ago
if (false == _incoming.ContainsKey(info.UploadFileTaskId))
6 years ago
{
6 years ago
lock (_locker)
6 years ago
{
6 years ago
if (false == _incoming.ContainsKey(info.UploadFileTaskId))
{
string path = BuildFilePath(clientFolderName, info.FilePath);
_incoming.Add(info.UploadFileTaskId, new FileWriter(path));
6 years ago
}
6 years ago
}
}
}
6 years ago
catch (Exception ex)
{
Log.Error("[FileReceiver]", ex);
return InvokeResult.Fault(ex.Message);
6 years ago
}
return InvokeResult.Succeeding();
6 years ago
}
public InvokeResult Incoming(FileFrame chunk)
6 years ago
{
6 years ago
try
6 years ago
{
6 years ago
FileWriter stream;
if (_incoming.TryGetValue(chunk.UploadFileTaskId, out stream))
{
stream.Write(chunk.Offset, chunk.Payload);
return InvokeResult.Succeeding();
6 years ago
}
return InvokeResult.Fault("File not expected.");
6 years ago
}
catch (Exception ex)
{
Log.Error("[FileReceiver]", ex);
return InvokeResult.Fault(ex.Message);
6 years ago
}
}
public InvokeResult Incoming(FileEndFrame info)
6 years ago
{
6 years ago
try
{
lock (_locker)
{
6 years ago
FileWriter stream;
if (_incoming.TryGetValue(info.UploadFileTaskId, out stream))
{
stream.GotCompleteMessage();
}
6 years ago
}
}
catch (Exception ex)
6 years ago
{
6 years ago
Log.Error("[FileReceiver]", ex);
return InvokeResult.Fault(ex.Message);
6 years ago
}
return InvokeResult.Succeeding();
6 years ago
}
6 years ago
private void Remove(long uploadTaskId)
6 years ago
{
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));
}
}
}
}

Powered by TurnKey Linux.