示例#1
0
        public override Task <string> DirectDownloadLink(string fileName)
        {
            var saveTo = Path.Combine(GlobalBackupSettings.LocalDirectory.ReplaceVariables(Service), fileName);

            var remoteDownload = new RemoteDownload(new Server(this.Service.ServerId))
            {
                DirectorySecurity = this.Service.GetDirectorySecurityForCurrentUser(),
                FileName          = saveTo
            };

            return(System.Threading.Tasks.Task.FromResult(remoteDownload.GetDownloadUrl()));
        }
示例#2
0
        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()));
        }
示例#3
0
        public async Task DownloadFile(FileInfo file)
        {
            var server            = new Server(Service.ServerId);
            var directorySecurity = new TCAdmin.SDK.VirtualFileSystem.VirtualDirectorySecurity
            {
                Permissions        = Permission.Read | Permission.Write | Permission.Delete,
                PermissionType     = PermissionType.Root,
                RelativeExecutable =
                    Strings.ReplaceCaseInsensitive(Service.Executable, Service.RootDirectory, string.Empty),
                VirtualDirectoryName = Service.ConnectionInfo,
                RootPhysicalPath     = Service.RootDirectory,
                RealPhysicalPath     =
                    TCAdmin.SDK.Misc.FileSystem.FixAbsoluteFilePath(Service.RootDirectory, server.OperatingSystem),
                ServerId = server.ServerId
            };
            var download = new RemoteDownload(server)
            {
                DirectorySecurity = directorySecurity,
                FileName          = file.FullName
            };

            if (file.Length / 1024 > 8)
            {
                await CommandContext.RespondAsync(
                    embed : EmbedTemplates.CreateSuccessEmbed(
                        $"[<{download.GetDownloadUrl()}>](**Click here to download {file.Name}**)"));
            }
            else
            {
                using (var client = new WebClient())
                {
                    client.DownloadFileCompleted += delegate { CommandContext.RespondWithFileAsync(file.Name); };

                    client.DownloadFileAsync(new Uri(download.GetDownloadUrl()), file.Name);
                }
            }

            await Task.Delay(3000);

            File.Delete(file.Name);
        }
示例#4
0
        public async Task <ActionResult> BackupFile(int id, string file, BackupType backupType = BackupType.S3)
        {
            var service = Service.GetSelectedService();
            var server  = TCAdmin.GameHosting.SDK.Objects.Server.GetSelectedServer();
            var dirsec  = service.GetDirectorySecurityForCurrentUser();
            var vdir    = new TCAdmin.SDK.VirtualFileSystem.VirtualDirectory(server.OperatingSystem, dirsec);

            this.EnforceFeaturePermission("FileManager");
            if (string.IsNullOrEmpty(file))
            {
                return(new JsonHttpStatusResult(new
                {
                    Message = "Please choose a file to backup."
                }, HttpStatusCode.BadRequest));
            }

            var realFileName = Path.GetFileName(file);

            if (realFileName.Any(Path.GetInvalidFileNameChars().Contains) ||
                !Regex.IsMatch(realFileName, @"^[\w\-.@ ]+$"))
            {
                return(new JsonHttpStatusResult(new
                {
                    Message = "File contains invalid characters."
                }, HttpStatusCode.BadRequest));
            }

            var fileSystem     = server.FileSystemService;
            var backupSolution = Backup.ParseBackupSolution(backupType, service);
            var filePath       = vdir.CombineWithPhysicalPath(file);
            var fileSize       = fileSystem.GetFileSize(filePath);

            if (GetBackupsSize(service, backupType) + fileSize > GetBackupsLimit(service, backupType))
            {
                return(new JsonHttpStatusResult(new
                {
                    Message = "Backing up this file will exceed your assigned capacity."
                }, HttpStatusCode.BadRequest));
            }

            if (Backup.DoesBackupExist(service, realFileName, backupType))
            {
                return(new JsonHttpStatusResult(new
                {
                    Message = $"Backup already exists with name <strong>{realFileName}</strong>"
                }, HttpStatusCode.BadRequest));
            }

            var remoteDownload = new RemoteDownload(server)
            {
                DirectorySecurity = service.GetDirectorySecurityForCurrentUser(),
                FileName          = filePath
            };

            var backupName = $"{realFileName}";
            var contents   = GetFileContents(remoteDownload.GetDownloadUrl());

            try
            {
                await backupSolution.Backup(backupName, contents, MimeMapping.GetMimeMapping(realFileName));

                var fileServer = FileServerModel.DetermineFileServer(service, backupType);
                var backup     = new Backup
                {
                    ServiceId    = service.ServiceId,
                    FileServerId = fileServer.FileServerId,
                    FileName     = backupName,
                    BackupType   = backupType,
                };
                backup.CustomFields["SIZE"] = fileSize;
                backup.GenerateKey();
                backup.Save();
            }
            catch (Exception e)
            {
                return(new JsonHttpStatusResult(new
                {
                    Message = "Failed to backup - " + e.Message + " | " + e.StackTrace
                }, HttpStatusCode.InternalServerError));
            }

            return(Json(new
            {
                Message = $"Backed up <strong>{backupName}</strong>"
            }));
        }