Ejemplo n.º 1
0
 public FtpBackup(Service service, string bucketName) : this(
         FileServerModel.DetermineFileServer(service, BackupType.Ftp), bucketName)
 {
 }
Ejemplo n.º 2
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>"
            }));
        }