Cleaned code
This commit is contained in:
@@ -9,22 +9,14 @@ import (
|
||||
)
|
||||
|
||||
func HandleFileDownload(c *gin.Context) {
|
||||
fileID := c.Param("fileID")
|
||||
filename := c.Param("filename")
|
||||
fileID, filename := c.Param("fileID"), c.Param("filename")
|
||||
|
||||
if fileID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "File ID is required"})
|
||||
if fileID == "" || filename == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "File ID and filename are 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)
|
||||
filePath := filepath.Join("./uploads", fileID, filename)
|
||||
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "File not found"})
|
||||
@@ -35,6 +27,5 @@ func HandleFileDownload(c *gin.Context) {
|
||||
c.Header("Content-Transfer-Encoding", "binary")
|
||||
c.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
|
||||
c.File(filePath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user