public static long GetBackupsQuotaUsed(Service service) { var backups = Models.Objects.Backup.GetBackupsForService(service); var value = backups.Sum(backup => backup.FileSize); return(value); }
private static int GetUserServicesCount() { var user = TCAdmin.SDK.Session.GetCurrentUser(); var services = Service.GetServices(user, false); return(services.Count); }
public async Task <ActionResult> Restore(int id, int backupId = 0) { this.EnforceFeaturePermission("FileManager"); if (backupId == 0) { return(this.SendError("No backup selected to restore.")); } var service = Service.GetSelectedService(); var server = TCAdmin.SDK.Objects.Server.GetSelectedServer(); var directorySecurity = service.GetDirectorySecurityForCurrentUser(); var fileSystem = TCAdmin.SDK.Objects.Server.GetSelectedServer().FileSystemService; var backup = new Backup(backupId); var backupSolution = backup.Provider.Create <BackupSolution>(); try { var randomFileName = TCAdmin.SDK.Misc.Random.RandomString(8, true, true) + ".zip"; var saveTo = FileSystem.CombinePath(server.OperatingSystem, service.RootDirectory, backup.Path, randomFileName); if (backupSolution.AllowsDirectDownload) { var downloadUrl = await backupSolution.DirectDownloadLink(backup); fileSystem.DownloadFile(saveTo, downloadUrl); } else { var bytes = await backupSolution.DownloadBytes(backup); var memoryStream = new MemoryStream(bytes); var byteBuffer = new byte[1024 * 1024 * 2]; int bytesRead; memoryStream.Position = 0; if (fileSystem.FileExists(saveTo)) { fileSystem.DeleteFile(saveTo); } while ((bytesRead = memoryStream.Read(byteBuffer, 0, byteBuffer.Length)) > 0) { fileSystem.AppendFile(saveTo, byteBuffer.Take(bytesRead).ToArray()); } fileSystem.SetOwnerAutomatically(saveTo); } fileSystem.Extract(saveTo, FileSystem.CombinePath(server.OperatingSystem, service.RootDirectory, backup.Path), ObjectXml.ObjectToXml(directorySecurity)); fileSystem.DeleteFile(saveTo); return(this.SendSuccess($"Restored <strong>{backup.Name}</strong>")); } catch (Exception e) { return(this.SendException(e, "Unable to restore backup - " + e.Message)); } }
public void RemoveInstalledPlugin(Service service, string slug) { var installedPlugins = GetInstalledPlugins(service); installedPlugins.Remove(slug); SetInstalledPlugins(service, installedPlugins); }
public override async Task <BackupResponse> Backup(Backup backup, BackupRequest request) { var service = new Service(backup.ServiceId); var server = new Server(service.ServerId); var fileSystemService = server.FileSystemService; var backupLocation = FileSystem.CombinePath(server.OperatingSystem, Configuration.LocalDirectory.ReplaceVariables(service)); var originalPath = FileSystem.FixAbsoluteFilePath( FileSystem.CombinePath(server.OperatingSystem, service.RootDirectory, request.Path, Compress(backup, request)), server.OperatingSystem); var newPath = FileSystem.CombinePath(server.OperatingSystem, backupLocation, backup.ZipFullName); var fileSize = fileSystemService.GetFileSize(originalPath); BackupManagerController.ThrowExceedQuota(backup, request, fileSize); backup.FileSize = fileSize; if (!fileSystemService.DirectoryExists(Path.GetDirectoryName(backupLocation))) { fileSystemService.CreateDirectory(Path.GetDirectoryName(backupLocation)); } fileSystemService.CopyFile(originalPath, newPath); fileSystemService.DeleteFile(originalPath); return(new BackupResponse(true)); }
public void AddInstalledPlugin(Service service, string slug) { var installedPlugins = GetInstalledPlugins(service); installedPlugins.Add(slug); SetInstalledPlugins(service, installedPlugins); }
public static async Task <Service> GetService(CommandContext ctx) { var guildServices = new List <Service>(); var servicesFresh = Service.GetServices(); foreach (Service service in servicesFresh) { if (service.Variables["__Nexus::DiscordGuild"] != null && service.Variables["__Nexus::DiscordGuild"].ToString() == ctx.Guild.Id.ToString() /*&& ShowServiceVar(service)*/) { guildServices.Add(service); } } if (guildServices.Count == 0) { throw new CustomMessageException(EmbedTemplates.CreateErrorEmbed("Service Link", "**No Services could be found. Add one by doing the `;Link` command!**")); } if (guildServices.Count == 1) { return(guildServices[0]); } if (guildServices.Count > 1) { return(await ChooseServiceFromList(ctx, guildServices)); } return(null); }
public static async Task <Service> LinkService(CommandContext ctx, User user) { if (user.IsSubUser) { throw new CustomMessageException( "**Sorry!** Sub Users cannot yet link services to Discord. Please get the owner of the services to link them."); } var userServices = Service.GetServices(user, true); var services = new List <Service>(); foreach (Service sv in userServices) { if (sv.UserId == user.UserId) { services.Add(sv); } } var service = await ChooseServiceFromList(ctx, services.AsReadOnly()); UpdateService(service, ctx.Guild.Id); return(service); }
public override async Task <BackupResponse> Backup(Backup backup, BackupRequest request) { var bucket = $"backups-service-{backup.ServiceId}"; var service = new Service(backup.ServiceId); var server = new Server(service.ServerId); var fileSystemService = server.FileSystemService; var combinePath = FileSystem.FixAbsoluteFilePath( FileSystem.CombinePath(server.OperatingSystem, service.RootDirectory, request.Path, Compress(backup, request)), server.OperatingSystem); var fileSize = fileSystemService.GetFileSize(combinePath); BackupManagerController.ThrowExceedQuota(backup, request, fileSize); backup.FileSize = fileSize; if (!await MinioClient.BucketExistsAsync(bucket)) { await MinioClient.MakeBucketAsync(bucket, _region); } var contents = fileSystemService.ReadBinary(combinePath); Stream stream = new MemoryStream(contents); await MinioClient.PutObjectAsync(bucket, backup.ZipFullName, stream, stream.Length, "application/zip"); return(new BackupResponse(true)); }
public FileSystemUtilities(VirtualDirectorySecurity vds, Server server, Service service, CommandContext ctx) { FileSystem = server.FileSystemService; VirtualDirectorySecurity = vds; Server = server; Service = service; CommandContext = ctx; }
private void SetInstalledPlugins(Service service, IEnumerable <string> plugins) { using (var securityBypass = new SecurityBypass(service)) { service.Variables["CM:InstalledPlugins"] = string.Join(",", plugins); service.Save(); } }
private static long GetBackupsLimit(Service service, BackupType backupType) { var limit = service.Variables[$"{backupType.ToString()}:LIMIT"] != null ? long.Parse(service.Variables[$"{backupType.ToString().ToUpper()}:LIMIT"].ToString()) : GlobalBackupSettings.GetDefaultCapacity(backupType); return(limit); }
public override Task <byte[]> DownloadBytes(Backup backup) { var service = new Service(backup.ServiceId); var fileLocation = $"/TCAdminBackupManager/{service.UserId}/{backup.ZipFullName}"; FtpClient.Download(out var content, fileLocation); return(System.Threading.Tasks.Task.FromResult(content)); }
public ActionResult Index() { var model = new MaintenanceModel { Service = new Service(Service.GetSelectedService().ServiceId), ServiceId = Service.GetSelectedService().ServiceId }; return(View("Maintenance", model)); }
public override Task <bool> Delete(Backup backup) { var service = new Service(backup.ServiceId); var fileLocation = $"/TCAdminBackupManager/{service.UserId}/{backup.ZipFullName}"; FtpClient.DeleteFile(fileLocation); return(System.Threading.Tasks.Task.FromResult(true)); }
public static long GetBackupsQuotaUsed(Service service, BackupProvider provider) { var backups = Models.Objects.Backup.GetBackupsForService(service); backups.RemoveAll(x => x.Provider.Id != provider.Id); var value = backups.Sum(backup => backup.FileSize); return(value); }
public static List <BackupProvider> AccessibleProviders(Service service) { var backupProviders = new BackupProvider().GetAll <BackupProvider>(); return((from backupProvider in backupProviders let config = backupProvider.Configuration.Parse <BackupProviderConfiguration>() where config.Enabled && backupProvider.GetQuota(service) > 0 select backupProvider).ToList()); }
private static long GetBackupsSize(Service service, BackupType backupType) { var backups = Backup.GetBackupsForService(service); backups.RemoveAll(x => x.BackupType != backupType); var value = backups.Sum(backup => backup.FileSize); return(value); }
public override Task <bool> Delete(Backup backup) { var service = new Service(backup.ServiceId); var server = new Server(service.ServerId); var fileSystemService = server.FileSystemService; var saveTo = Path.Combine(Configuration.LocalDirectory.ReplaceVariables(service), backup.ZipFullName); fileSystemService.DeleteFile(saveTo); return(System.Threading.Tasks.Task.FromResult(true)); }
public static bool LinkService(ulong guildId, int serviceId) { var service = new Service(serviceId); if (service.Find()) { UpdateService(service, guildId); return(true); } return(false); }
public override bool UnInstallMod(Service service, GenericMod gameMod) { var server = Server.GetSelectedServer(); var fileSystem = server.FileSystemService; var detailedModData = DetailedModData.GetDetailedModData(gameMod.Id); var combinePath = TCAdmin.SDK.Misc.FileSystem.CombinePath(server.OperatingSystem, service.RootDirectory, "oxide", "plugins", detailedModData.Name + ".cs"); fileSystem.DeleteFile(combinePath); return(true); }
public ActionResult Index(int id) { var service = Service.GetSelectedService(); var model = new BackupManagerIndexModel { ServiceId = service.ServiceId, UsedQuota = GetBackupsQuotaUsed(service), MaxQuota = GetBackupsQuota(service) }; return(View(model)); }
public List <string> GetInstalledPlugins(Service service) { var hasValueAndSet = service.Variables.HasValueAndSet("CM:InstalledPlugins"); if (!hasValueAndSet) { return(new List <string>()); } var strings = service.Variables["CM:InstalledPlugins"].ToString().Split(','); return(strings.ToList()); }
public FileSystemUtilities(VirtualDirectorySecurity vds, Server server, CommandContext ctx) { FileSystem = server.FileSystemService; VirtualDirectorySecurity = vds; Server = server; Service = new Service { WorkingDirectory = "/", RootDirectory = "/", Executable = "/" }; CommandContext = ctx; }
public ActionResult List(int id, BackupType backupType) { this.EnforceFeaturePermission("FileManager"); var service = Service.GetSelectedService(); var backups = Backup.GetBackupsForService(service, backupType).ToList(); return(Json(backups.Select(x => new { name = x.FileName, value = x.BackupId }), JsonRequestBehavior.AllowGet)); }
public static string ReplaceVariables(this string query, Service service = null, User user = null, Server server = null, Datacenter datacenter = null) { var input = new TCAdmin.SDK.Scripting.InputParser(query); service?.ReplacePropertyValues(input); service?.ReplaceCustomVariables(new TCAdmin.GameHosting.SDK.Objects.Game(service.GameId).CustomVariables, service.Variables, input); user?.ReplacePropertyValues(input); server?.ReplacePropertyValues(input); datacenter?.ReplacePropertyValues(input); var output = input.GetOutput(); return(output); }
public override string Compress(Backup backup, BackupRequest request) { var service = new Service(backup.ServiceId); var server = new Server(service.ServerId); var directorySecurity = GenerateVirtualDirectorySecurity(service); var baseDirectory = FileSystem.CombinePath(server.OperatingSystem, service.RootDirectory, request.Path); var fileSystem = server.FileSystemService; var toCompress = request.Directories.Select(x => x.Name).ToList(); toCompress.AddRange(request.Files.Select(x => x.Name + x.Extension)); return(fileSystem.CompressFiles(baseDirectory, toCompress.ToArray(), ObjectXml.ObjectToXml(directorySecurity), 500000000)); }
public override Task <string> DirectDownloadLink(Backup backup) { var service = new Service(backup.ServiceId); var server = new Server(service.ServerId); var saveTo = FileSystem.CombinePath(server.OperatingSystem, Configuration.LocalDirectory.ReplaceVariables(service), backup.ZipFullName); var remoteDownload = new RemoteDownload(server) { DirectorySecurity = service.GetDirectorySecurityForCurrentUser(), FileName = saveTo }; return(System.Threading.Tasks.Task.FromResult(remoteDownload.GetDownloadUrl())); }
private static List <SelectListItem> GetDbResetUsernames() { var user = TCAdmin.SDK.Session.GetCurrentUser(); var services = Service.GetServices(user, false) .Cast <Service>().ToList(); return((from service in services let usernameExists = service.Variables["_MySQLPlugin::Username"] != null where usernameExists let text = service.Variables["_MySQLPlugin::Database"].ToString() select new SelectListItem { Text = text, Value = service.ServiceId.ToString() }).ToList()); }
private static List <SelectListItem> GetUserServices() { var user = TCAdmin.SDK.Session.GetCurrentUser(); var services = Service.GetServices(user, false) .Cast <Service>().ToList(); return((from service in services let usernameExists = service.Variables["_MySQLPlugin::Username"] != null where !usernameExists select new SelectListItem { Text = service.ConnectionInfo + " - " + service.Name, Value = service.ServiceId.ToString() }) .ToList()); }