From c851a9af239c65098deb9750ba6c53db460ab40a Mon Sep 17 00:00:00 2001 From: Zoldorf Date: Wed, 5 Apr 2023 17:37:05 -0600 Subject: [PATCH] Adds uploadfolder command (#15102) --- .../Administration/Commands/UploadFolder.cs | 76 +++++++++++++++++++ .../administration/commands/uploadfolder.ftl | 7 ++ Resources/clientCommandPerms.yml | 1 + 3 files changed, 84 insertions(+) create mode 100644 Content.Client/Administration/Commands/UploadFolder.cs create mode 100644 Resources/Locale/en-US/administration/commands/uploadfolder.ftl diff --git a/Content.Client/Administration/Commands/UploadFolder.cs b/Content.Client/Administration/Commands/UploadFolder.cs new file mode 100644 index 0000000000..02b094e916 --- /dev/null +++ b/Content.Client/Administration/Commands/UploadFolder.cs @@ -0,0 +1,76 @@ +using System.IO; +using Content.Shared.Administration; +using Content.Shared.CCVar; +using Robust.Shared.Configuration; +using Robust.Shared.Console; +using Robust.Shared.ContentPack; +using Robust.Shared.Network; +using Robust.Shared.Utility; + +namespace Content.Client.Administration.Commands; + +public sealed class UploadFolder : IConsoleCommand +{ + public string Command => "uploadfolder"; + public string Description => Loc.GetString("uploadfolder-command-description"); + public string Help => Loc.GetString("uploadfolder-command-help"); + + private static readonly ResourcePath BaseUploadFolderPath = new("/UploadFolder"); + + [Dependency] private IResourceManager _resourceManager = default!; + [Dependency] private IConfigurationManager _configManager = default!; + + public async void Execute(IConsoleShell shell, string argStr, string[] args) + { + var fileCount = 0; + + + if (!_configManager.GetCVar(CCVars.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 = new ResourcePath(BaseUploadFolderPath + $"/{args[0]}"); + + if (!_resourceManager.UserData.Exists(folderPath.ToRootedPath())) + { + 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 + foreach (var filepath in _resourceManager.UserData.Find($"{folderPath.ToRelativePath()}/").files ) + { + + await using var filestream = _resourceManager.UserData.Open(filepath,FileMode.Open); + { + var sizeLimit = _configManager.GetCVar(CCVars.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(); + + var netManager = IoCManager.Resolve(); + var msg = netManager.CreateNetMessage(); + + msg.RelativePath = new ResourcePath($"{filepath.ToString().Remove(0,14)}"); //removes /UploadFolder/ from path + msg.Data = data; + + netManager.ClientSendMessage(msg); + fileCount++; + } + } + + shell.WriteLine( Loc.GetString("uploadfolder-command-success",("fileCount",fileCount))); + } +} diff --git a/Resources/Locale/en-US/administration/commands/uploadfolder.ftl b/Resources/Locale/en-US/administration/commands/uploadfolder.ftl new file mode 100644 index 0000000000..9d2ec1b542 --- /dev/null +++ b/Resources/Locale/en-US/administration/commands/uploadfolder.ftl @@ -0,0 +1,7 @@ +uploadfolder-command-description = Uploads a folder from your UserData folder recursively to the server contentDB. +uploadfolder-command-help = uploadfolder [folder you want to upload in userdata/UploadFolder] +uploadfolder-command-wrong-args = Wrong number of arguments! +uploadfolder-command-folder-not-found = Folder {$folder} not found! +uploadfolder-command-resource-upload-disabled = Network Resource Uploading is currently disabled. check Server CVars. +uploadfolder-command-file-too-big = File {$filename} above the current size limit! It must be smaller than {$sizeLimit} MB. skipping. +uploadfolder-command-success = Uploaded {$fileCount} files diff --git a/Resources/clientCommandPerms.yml b/Resources/clientCommandPerms.yml index 5dccc849ed..9c5a55d403 100644 --- a/Resources/clientCommandPerms.yml +++ b/Resources/clientCommandPerms.yml @@ -71,3 +71,4 @@ Commands: - uploadfile - loadprototype + - uploadfolder