Files
RobustToolbox/Robust.Client/Upload/Commands/UploadFolderCommand.cs
T
Pieter-Jan Briers 83c2a1be11 [Dependency] source generator part 2 (#6550)
* [Dependency] source generator

No more reflection, no more codegen at runtime

Also various changes to Roslyn helpers to make this easier to write.

Requires all types with dependencies to be partial and not have readonly dependency fields. An analyzer enforces this at warning level, the previous injection strategies have remained in the code *for now* as a fallback.

No fallback is available for [field: Dependency] properties, due to a Roslyn bug.

Code Fixes exist. We love Roslyn

* Apply dependencies generator changes to all code

* Release notes

* Preprocessor got hands

* Handle nullable dependencies

These are bad but gotta deal with it.

* Apply suggestions from code review

Co-authored-by: Moony <moony@hellomouse.net>

* Fine, let's not use collection expressions

---------

Co-authored-by: Moony <moony@hellomouse.net>
2026-05-08 12:38:33 +02: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 partial class UploadFolderCommand : IConsoleCommand
{
[Dependency] private IResourceManager _resourceManager = default!;
[Dependency] private IConfigurationManager _configManager = default!;
[Dependency] private 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)));
}
}