Files
tbd-station-14/Content.Client/Administration/Commands/UploadFile.cs
Vera Aguilera Puerto eb54f4b224 Adds Network Resource Uploading for admins. (#6904)
Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
2022-03-26 12:46:37 +01:00

63 lines
1.9 KiB
C#

using Content.Shared.Administration;
using Content.Shared.CCVar;
using Robust.Client.UserInterface;
using Robust.Shared.Configuration;
using Robust.Shared.Console;
using Robust.Shared.Network;
using Robust.Shared.Utility;
namespace Content.Client.Administration.Commands;
public sealed class UploadFile : IConsoleCommand
{
public string Command => "uploadfile";
public string Description => "Uploads a resource to the server.";
public string Help => $"{Command} [relative path for the resource]";
public async void Execute(IConsoleShell shell, string argStr, string[] args)
{
var cfgMan = IoCManager.Resolve<IConfigurationManager>();
if (!cfgMan.GetCVar(CCVars.ResourceUploadingEnabled))
{
shell.WriteError("Network Resource Uploading is currently disabled by the server.");
return;
}
if (args.Length != 1)
{
shell.WriteError("Wrong number of arguments!");
return;
}
var dialog = IoCManager.Resolve<IFileDialogManager>();
var filters = new FileDialogFilters(new FileDialogFilters.Group("*"));
await using var file = await dialog.OpenFile(filters);
if (file == null)
{
shell.WriteError("Error picking file!");
return;
}
var sizeLimit = cfgMan.GetCVar(CCVars.ResourceUploadingLimitMb);
if (sizeLimit > 0f && file.Length * SharedNetworkResourceManager.BytesToMegabytes > sizeLimit)
{
shell.WriteError($"File above the current size limit! It must be smaller than {sizeLimit} MB.");
return;
}
var data = file.CopyToArray();
var netManager = IoCManager.Resolve<INetManager>();
var msg = netManager.CreateNetMessage<NetworkResourceUploadMessage>();
msg.RelativePath = new ResourcePath(args[0]).ToRelativePath();
msg.Data = data;
netManager.ClientSendMessage(msg);
}
}