62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package endpoints
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func HandleFileDownload(c *gin.Context) {
|
|
filename := c.Param("filename")
|
|
if filename == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Filename is required"})
|
|
return
|
|
}
|
|
|
|
uploadsDir := "./uploads"
|
|
filePath := filepath.Join(uploadsDir, 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)
|
|
}
|
|
|
|
func ListFiles(c *gin.Context) {
|
|
uploadsDir := "./uploads"
|
|
|
|
files, err := os.ReadDir(uploadsDir)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read uploads directory"})
|
|
return
|
|
}
|
|
|
|
var fileList []gin.H
|
|
for _, file := range files {
|
|
if !file.IsDir() {
|
|
info, err := file.Info()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
fileList = append(fileList, gin.H{
|
|
"filename": file.Name(),
|
|
"size": info.Size(),
|
|
"modified": info.ModTime(),
|
|
})
|
|
}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"files": fileList,
|
|
"count": len(fileList),
|
|
})
|
|
} |