Adds uploadfolder command (#15102)
This commit is contained in:
76
Content.Client/Administration/Commands/UploadFolder.cs
Normal file
76
Content.Client/Administration/Commands/UploadFolder.cs
Normal file
@@ -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<INetManager>();
|
||||
var msg = netManager.CreateNetMessage<NetworkResourceUploadMessage>();
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -71,3 +71,4 @@
|
||||
Commands:
|
||||
- uploadfile
|
||||
- loadprototype
|
||||
- uploadfolder
|
||||
|
||||
Reference in New Issue
Block a user