Cleaned code

This commit is contained in:
2025-08-07 22:13:39 +02:00
parent 6bcedf84f6
commit afccf236fc
5 changed files with 23 additions and 56 deletions

View File

@@ -9,22 +9,14 @@ import (
) )
func HandleFileDownload(c *gin.Context) { func HandleFileDownload(c *gin.Context) {
fileID := c.Param("fileID") fileID, filename := c.Param("fileID"), c.Param("filename")
filename := c.Param("filename")
if fileID == "" { if fileID == "" || filename == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "File ID is required"}) c.JSON(http.StatusBadRequest, gin.H{"error": "File ID and filename are required"})
return return
} }
if filename == "" { filePath := filepath.Join("./uploads", fileID, filename)
c.JSON(http.StatusBadRequest, gin.H{"error": "Filename is required"})
return
}
uploadsDir := "./uploads"
fileDir := filepath.Join(uploadsDir, fileID)
filePath := filepath.Join(fileDir, filename)
if _, err := os.Stat(filePath); os.IsNotExist(err) { if _, err := os.Stat(filePath); os.IsNotExist(err) {
c.JSON(http.StatusNotFound, gin.H{"error": "File not found"}) c.JSON(http.StatusNotFound, gin.H{"error": "File not found"})
@@ -35,6 +27,5 @@ func HandleFileDownload(c *gin.Context) {
c.Header("Content-Transfer-Encoding", "binary") c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename) c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Type", "application/octet-stream") c.Header("Content-Type", "application/octet-stream")
c.File(filePath) c.File(filePath)
} }

View File

@@ -12,8 +12,5 @@ func HandleIndex(c *gin.Context) {
if baseURL == "" { if baseURL == "" {
baseURL = "http://localhost:8080" baseURL = "http://localhost:8080"
} }
c.HTML(http.StatusOK, "index.html", gin.H{"BaseURL": baseURL})
c.HTML(http.StatusOK, "index.html", gin.H{
"BaseURL": baseURL,
})
} }

View File

@@ -4,7 +4,6 @@ import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"io" "io"
"mime/multipart"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@@ -14,10 +13,8 @@ import (
func generateID() (string, error) { func generateID() (string, error) {
bytes := make([]byte, 16) bytes := make([]byte, 16)
if _, err := rand.Read(bytes); err != nil { _, err := rand.Read(bytes)
return "", err return hex.EncodeToString(bytes), err
}
return hex.EncodeToString(bytes), nil
} }
func HandleFileUpload(c *gin.Context) { func HandleFileUpload(c *gin.Context) {
@@ -26,12 +23,7 @@ func HandleFileUpload(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "No file provided"}) c.JSON(http.StatusBadRequest, gin.H{"error": "No file provided"})
return return
} }
defer func(file multipart.File) { defer file.Close()
err := file.Close()
if err != nil {
}
}(file)
fileID, err := generateID() fileID, err := generateID()
if err != nil { if err != nil {
@@ -39,28 +31,20 @@ func HandleFileUpload(c *gin.Context) {
return return
} }
uploadsDir := "./uploads" fileDir := filepath.Join("./uploads", fileID)
fileDir := filepath.Join(uploadsDir, fileID)
if err := os.MkdirAll(fileDir, 0755); err != nil { if err := os.MkdirAll(fileDir, 0755); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file directory"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file directory"})
return return
} }
dst := filepath.Join(fileDir, header.Filename) out, err := os.Create(filepath.Join(fileDir, header.Filename))
out, err := os.Create(dst)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file"})
return return
} }
defer func(out *os.File) { defer out.Close()
err := out.Close()
if err != nil {
} if _, err = io.Copy(out, file); err != nil {
}(out)
_, err = io.Copy(out, file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"})
return return
} }
@@ -70,13 +54,11 @@ func HandleFileUpload(c *gin.Context) {
baseURL = "http://localhost:8080" baseURL = "http://localhost:8080"
} }
fileURL := baseURL + "/files/" + fileID + "/" + header.Filename
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"message": "File uploaded successfully", "message": "File uploaded successfully",
"id": fileID, "id": fileID,
"filename": header.Filename, "filename": header.Filename,
"size": header.Size, "size": header.Size,
"url": fileURL, "url": baseURL + "/files/" + fileID + "/" + header.Filename,
}) })
} }

17
main.go
View File

@@ -15,16 +15,13 @@ func main() {
r.GET("/", endpoints.HandleIndex) r.GET("/", endpoints.HandleIndex)
r.POST("/upload", endpoints.HandleFileUpload) r.POST("/upload", endpoints.HandleFileUpload)
r.GET("/uploads/:fileID/:filename", endpoints.HandleFileDownload) r.GET("/uploads/:fileID/:filename", endpoints.HandleFileDownload)
r.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ok"}) })
r.GET("/health", func(c *gin.Context) { if port := os.Getenv("PORT"); port != "" {
c.JSON(200, gin.H{"status": "ok"}) fmt.Printf("FITRA Backend starting on port %s\n", port)
}) r.Run(":" + port)
} else {
port := os.Getenv("PORT") fmt.Println("FITRA Backend starting on port 8080")
if port == "" { r.Run(":8080")
port = "8080"
} }
fmt.Printf("FITRA Backend starting on port %s\n", port)
r.Run(":" + port)
} }

View File

@@ -26,21 +26,21 @@
<div class="endpoint"> <div class="endpoint">
<h3><span class="method post">POST</span>/upload</h3> <h3><span class="method post">POST</span>/upload</h3>
<p><strong>Description:</strong> Upload a file</p> <p><strong>Description:</strong> Upload a file</p>
<p><strong>cURL Example:</strong></p> <p><strong>cURL example:</strong></p>
<pre><code>curl -X POST -F "file=@/path/to/your/file.txt" {{.BaseURL}}/upload</code></pre> <pre><code>curl -X POST -F "file=@/path/to/your/file.txt" {{.BaseURL}}/upload</code></pre>
</div> </div>
<div class="endpoint"> <div class="endpoint">
<h3><span class="method get">GET</span>/uploads/{fileID}/{filename}</h3> <h3><span class="method get">GET</span>/uploads/{fileID}/{filename}</h3>
<p><strong>Description:</strong> Download a file using the ID and filename from upload response</p> <p><strong>Description:</strong> Download a file using the ID and filename from upload response</p>
<p><strong>cURL Example:</strong></p> <p><strong>cURL example:</strong></p>
<pre><code>curl -O {{.BaseURL}}/uploads/{fileID}/{filename}</code></pre> <pre><code>curl -O {{.BaseURL}}/uploads/{fileID}/{filename}</code></pre>
</div> </div>
<div class="endpoint"> <div class="endpoint">
<h3><span class="method get">GET</span>/health</h3> <h3><span class="method get">GET</span>/health</h3>
<p><strong>Description:</strong> Check service health</p> <p><strong>Description:</strong> Check service health</p>
<p><strong>cURL Example:</strong></p> <p><strong>cURL example:</strong></p>
<pre><code>curl {{.BaseURL}}/health</code></pre> <pre><code>curl {{.BaseURL}}/health</code></pre>
</div> </div>