示例#1
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>"
            }));
        }
示例#2
0
        public async Task <ActionResult> Restore(int id, string target, int backupId = 0)
        {
            this.EnforceFeaturePermission("FileManager");
            if (backupId == 0)
            {
                return(new JsonHttpStatusResult(new
                {
                    Message = "No backup selected to restore."
                }, HttpStatusCode.InternalServerError));
            }

            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);
            var fileSystem     = TCAdmin.SDK.Objects.Server.GetSelectedServer().FileSystemService;
            var backup         = new Backup(backupId);
            var backupSolution = backup.BackupSolution;

            try
            {
                var targetpath = vdir.CombineWithPhysicalPath(target);
                var saveTo     = TCAdmin.SDK.Misc.FileSystem.CombinePath(targetpath, backup.FileName, server.OperatingSystem);

                if (backupSolution.AllowsDirectDownload)
                {
                    var downloadUrl = await backupSolution.DirectDownloadLink(backup.FileName);

                    fileSystem.DownloadFile(saveTo, downloadUrl);
                }
                else
                {
                    var bytes = await backupSolution.DownloadBytes(backup.FileName);

                    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);
                }

                return(new JsonHttpStatusResult(new
                {
                    Message = $"Restored <strong>{backup.FileName}</strong>"
                }, HttpStatusCode.OK));
            }
            catch (Exception e)
            {
                return(new JsonHttpStatusResult(new
                {
                    Message = "An error occurred: " + e.Message + " | " + e.StackTrace
                }, HttpStatusCode.InternalServerError));
            }
        }