Files
fitra-backend/endpoints/download.go
2025-08-07 22:08:48 +02:00

41 lines
901 B
Go

package endpoints
import (
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
func HandleFileDownload(c *gin.Context) {
fileID := c.Param("fileID")
filename := c.Param("filename")
if fileID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "File ID is required"})
return
}
if filename == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Filename is required"})
return
}
uploadsDir := "./uploads"
fileDir := filepath.Join(uploadsDir, fileID)
filePath := filepath.Join(fileDir, filename)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
c.JSON(http.StatusNotFound, gin.H{"error": "File not found"})
return
}
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Type", "application/octet-stream")
c.File(filePath)
}