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),
})
}

45
endpoints/upload.go Normal file
View File

@@ -0,0 +1,45 @@
package endpoints
import (
"io"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
func HandleFileUpload(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No file provided"})
return
}
defer file.Close()
uploadsDir := "./uploads"
if err := os.MkdirAll(uploadsDir, 0755); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create uploads directory"})
return
}
dst := filepath.Join(uploadsDir, header.Filename)
out, err := os.Create(dst)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file"})
return
}
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "File uploaded successfully",
"filename": header.Filename,
"size": header.Size,
})
}