Added index, improved logic

This commit is contained in:
2025-08-07 22:08:48 +02:00
parent 84cdd52322
commit 6bcedf84f6
8 changed files with 159 additions and 43 deletions

View File

@@ -9,14 +9,22 @@ import (
)
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"
filePath := filepath.Join(uploadsDir, filename)
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"})
@@ -27,36 +35,6 @@ 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)
}
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),
})
}