backup_controller.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package controllers
  2. import (
  3. "net/http"
  4. "ems-backend/models"
  5. "ems-backend/services"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type BackupController struct{}
  9. func (bc *BackupController) GetConfig(c *gin.Context) {
  10. config, err := services.GlobalBackupService.GetConfig()
  11. if err != nil {
  12. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  13. return
  14. }
  15. c.JSON(http.StatusOK, gin.H{"data": config})
  16. }
  17. func (bc *BackupController) SaveConfig(c *gin.Context) {
  18. var config models.BackupConfig
  19. if err := c.ShouldBindJSON(&config); err != nil {
  20. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  21. return
  22. }
  23. if err := services.GlobalBackupService.SaveConfig(&config); err != nil {
  24. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  25. return
  26. }
  27. c.JSON(http.StatusOK, gin.H{"message": "Configuration saved"})
  28. }
  29. func (bc *BackupController) ManualBackup(c *gin.Context) {
  30. go services.GlobalBackupService.PerformBackup()
  31. c.JSON(http.StatusOK, gin.H{"message": "Backup started"})
  32. }
  33. func (bc *BackupController) GetLogs(c *gin.Context) {
  34. logs, err := services.GlobalBackupService.GetLogs()
  35. if err != nil {
  36. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  37. return
  38. }
  39. c.JSON(http.StatusOK, gin.H{"data": logs})
  40. }
  41. func (bc *BackupController) TestConnection(c *gin.Context) {
  42. var config models.BackupConfig
  43. if err := c.ShouldBindJSON(&config); err != nil {
  44. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  45. return
  46. }
  47. if err := services.GlobalBackupService.TestConnection(&config); err != nil {
  48. c.JSON(http.StatusOK, gin.H{"success": false, "message": err.Error()})
  49. return
  50. }
  51. c.JSON(http.StatusOK, gin.H{"success": true, "message": "Connection successful"})
  52. }
  53. func (bc *BackupController) Download(c *gin.Context) {
  54. id := c.Param("id")
  55. if id == "" {
  56. c.JSON(http.StatusBadRequest, gin.H{"error": "ID required"})
  57. return
  58. }
  59. object, fileName, err := services.GlobalBackupService.DownloadFile(id)
  60. if err != nil {
  61. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  62. return
  63. }
  64. defer object.Close()
  65. // Set headers
  66. c.Header("Content-Description", "File Transfer")
  67. c.Header("Content-Transfer-Encoding", "binary")
  68. c.Header("Content-Disposition", "attachment; filename="+fileName)
  69. c.Header("Content-Type", "application/octet-stream")
  70. // Stream to client
  71. // Gin's DataFromReader is perfect for this
  72. // We don't know exact size easily without Stat (already called inside service),
  73. // Service could return info.Size but let's assume it works or use -1 if unknown
  74. // Actually object.Stat() is cached in minio-go object usually.
  75. stat, _ := object.Stat()
  76. c.DataFromReader(http.StatusOK, stat.Size, "application/octet-stream", object, map[string]string{
  77. "Content-Disposition": "attachment; filename="+fileName,
  78. })
  79. }