Files
RobustToolbox/Robust.Client/Upload/Commands/UploadFolderCommand.cs
Pieter-Jan Briers dc1464b462 High-bandwidth transfer system (#6373)
* WebSocket-based data transfer system

* Move resource downloads/uploads to the new transfer system

Should drastically increase the permitted practical size

* Transfer impl for Lidgren

* Async impl for receive stream

* Use unbounded channel for Lidgren

* Add metrics

* More comments

* Add serverside stream limit to avoid being a DoS vector

* Fix tests

* Oops forgot to actually implement sequence channels in NetMessage

* Doc comment for NetMessage.SequenceChannel

* Release notes
2026-01-19 20:44:44 +01:00

77 lines
2.8 KiB
C#

using System.Collections.Generic;
using System.IO;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.Console;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Upload;
using Robust.Shared.Utility;
namespace Robust.Client.Upload.Commands;
public sealed class UploadFolderCommand : IConsoleCommand
{
[Dependency] private readonly IResourceManager _resourceManager = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly NetworkResourceManager _netRes = default!;
public string Command => "uploadfolder";
public string Description => Loc.GetString("uploadfolder-command-description");
public string Help => Loc.GetString("uploadfolder-command-help");
private static readonly ResPath BaseUploadFolderPath = new("/UploadFolder");
public async void Execute(IConsoleShell shell, string argStr, string[] args)
{
var fileCount = 0;
if (!_configManager.GetCVar(CVars.ResourceUploadingEnabled))
{
shell.WriteError( Loc.GetString("uploadfolder-command-resource-upload-disabled"));
return;
}
if (args.Length != 1)
{
shell.WriteError( Loc.GetString("uploadfolder-command-wrong-args"));
shell.WriteLine( Loc.GetString("uploadfolder-command-help"));
return;
}
var folderPath = BaseUploadFolderPath / args[0];
if (!_resourceManager.UserData.Exists(folderPath))
{
shell.WriteError( Loc.GetString("uploadfolder-command-folder-not-found",("folder", folderPath)));
return; // bomb out if the folder doesnt exist in /UploadFolder
}
//Grab all files in specified folder and upload them
var files = new List<(ResPath Relative, byte[] Data)>();
foreach (var filepath in _resourceManager.UserData.Find($"{folderPath.ToRelativePath()}/").files )
{
await using var filestream = _resourceManager.UserData.Open(filepath, FileMode.Open);
{
var sizeLimit = _configManager.GetCVar(CVars.ResourceUploadingLimitMb);
if (sizeLimit > 0f && filestream.Length * SharedNetworkResourceManager.BytesToMegabytes > sizeLimit)
{
shell.WriteError( Loc.GetString("uploadfolder-command-file-too-big", ("filename",filepath), ("sizeLimit",sizeLimit)));
return;
}
var data = filestream.CopyToArray();
files.Add((filepath.RelativeTo(BaseUploadFolderPath), data));
fileCount++;
}
}
_netRes.UploadResources(files);
shell.WriteLine( Loc.GetString("uploadfolder-command-success",("fileCount",fileCount)));
}
}