Added index, improved logic
This commit is contained in:
@@ -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),
|
||||
})
|
||||
}
|
||||
19
endpoints/index.go
Normal file
19
endpoints/index.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func HandleIndex(c *gin.Context) {
|
||||
baseURL := os.Getenv("BASE_URL")
|
||||
if baseURL == "" {
|
||||
baseURL = "http://localhost:8080"
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||
"BaseURL": baseURL,
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
package endpoints
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -9,27 +12,52 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func generateID() (string, error) {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
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()
|
||||
defer func(file multipart.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
|
||||
uploadsDir := "./uploads"
|
||||
if err := os.MkdirAll(uploadsDir, 0755); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create uploads directory"})
|
||||
}
|
||||
}(file)
|
||||
|
||||
fileID, err := generateID()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate file ID"})
|
||||
return
|
||||
}
|
||||
|
||||
dst := filepath.Join(uploadsDir, header.Filename)
|
||||
uploadsDir := "./uploads"
|
||||
fileDir := filepath.Join(uploadsDir, fileID)
|
||||
if err := os.MkdirAll(fileDir, 0755); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file directory"})
|
||||
return
|
||||
}
|
||||
|
||||
dst := filepath.Join(fileDir, 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()
|
||||
defer func(out *os.File) {
|
||||
err := out.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(out)
|
||||
|
||||
_, err = io.Copy(out, file)
|
||||
if err != nil {
|
||||
@@ -37,9 +65,18 @@ func HandleFileUpload(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
baseURL := os.Getenv("BASE_URL")
|
||||
if baseURL == "" {
|
||||
baseURL = "http://localhost:8080"
|
||||
}
|
||||
|
||||
fileURL := baseURL + "/files/" + fileID + "/" + header.Filename
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "File uploaded successfully",
|
||||
"id": fileID,
|
||||
"filename": header.Filename,
|
||||
"size": header.Size,
|
||||
"url": fileURL,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user