Init commit

This commit is contained in:
2025-08-07 21:45:04 +02:00
commit 84cdd52322
8 changed files with 455 additions and 0 deletions

62
endpoints/download.go Normal file
View File

@@ -0,0 +1,62 @@
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),
})
}