backup.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package system
  2. import (
  3. "bytes"
  4. "net/http"
  5. "time"
  6. "github.com/0xJacky/Nginx-UI/internal/backup"
  7. "github.com/gin-gonic/gin"
  8. "github.com/uozi-tech/cosy"
  9. )
  10. // CreateBackup creates a backup of nginx-ui and nginx configurations
  11. // and sends files directly for download
  12. func CreateBackup(c *gin.Context) {
  13. result, err := backup.Backup()
  14. if err != nil {
  15. cosy.ErrHandler(c, err)
  16. return
  17. }
  18. // Concatenate Key and IV
  19. securityToken := result.AESKey + ":" + result.AESIv
  20. // Prepare response content
  21. reader := bytes.NewReader(result.BackupContent)
  22. modTime := time.Now()
  23. // Set HTTP headers for file download
  24. fileName := result.BackupName
  25. c.Header("Content-Description", "File Transfer")
  26. c.Header("Content-Type", "application/zip")
  27. c.Header("Content-Disposition", "attachment; filename="+fileName)
  28. c.Header("Content-Transfer-Encoding", "binary")
  29. c.Header("X-Backup-Security", securityToken) // Pass security token in header
  30. c.Header("Expires", "0")
  31. c.Header("Cache-Control", "must-revalidate")
  32. c.Header("Pragma", "public")
  33. // Send file content
  34. http.ServeContent(c.Writer, c.Request, fileName, modTime, reader)
  35. }