Cleaned code
This commit is contained in:
@@ -9,22 +9,14 @@ import (
|
||||
)
|
||||
|
||||
func HandleFileDownload(c *gin.Context) {
|
||||
fileID := c.Param("fileID")
|
||||
filename := c.Param("filename")
|
||||
fileID, filename := c.Param("fileID"), c.Param("filename")
|
||||
|
||||
if fileID == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "File ID is required"})
|
||||
if fileID == "" || filename == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "File ID and filename are required"})
|
||||
return
|
||||
}
|
||||
|
||||
if filename == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Filename is required"})
|
||||
return
|
||||
}
|
||||
|
||||
uploadsDir := "./uploads"
|
||||
fileDir := filepath.Join(uploadsDir, fileID)
|
||||
filePath := filepath.Join(fileDir, filename)
|
||||
filePath := filepath.Join("./uploads", fileID, filename)
|
||||
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
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-Disposition", "attachment; filename="+filename)
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
|
||||
c.File(filePath)
|
||||
}
|
||||
|
||||
@@ -12,8 +12,5 @@ func HandleIndex(c *gin.Context) {
|
||||
if baseURL == "" {
|
||||
baseURL = "http://localhost:8080"
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||
"BaseURL": baseURL,
|
||||
})
|
||||
c.HTML(http.StatusOK, "index.html", gin.H{"BaseURL": baseURL})
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -14,10 +13,8 @@ import (
|
||||
|
||||
func generateID() (string, error) {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(bytes), nil
|
||||
_, err := rand.Read(bytes)
|
||||
return hex.EncodeToString(bytes), err
|
||||
}
|
||||
|
||||
func HandleFileUpload(c *gin.Context) {
|
||||
@@ -26,12 +23,7 @@ func HandleFileUpload(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No file provided"})
|
||||
return
|
||||
}
|
||||
defer func(file multipart.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(file)
|
||||
defer file.Close()
|
||||
|
||||
fileID, err := generateID()
|
||||
if err != nil {
|
||||
@@ -39,28 +31,20 @@ func HandleFileUpload(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
uploadsDir := "./uploads"
|
||||
fileDir := filepath.Join(uploadsDir, fileID)
|
||||
fileDir := filepath.Join("./uploads", 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)
|
||||
out, err := os.Create(filepath.Join(fileDir, header.Filename))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file"})
|
||||
return
|
||||
}
|
||||
defer func(out *os.File) {
|
||||
err := out.Close()
|
||||
if err != nil {
|
||||
defer out.Close()
|
||||
|
||||
}
|
||||
}(out)
|
||||
|
||||
_, err = io.Copy(out, file)
|
||||
if err != nil {
|
||||
if _, err = io.Copy(out, file); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"})
|
||||
return
|
||||
}
|
||||
@@ -70,13 +54,11 @@ func HandleFileUpload(c *gin.Context) {
|
||||
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,
|
||||
"url": baseURL + "/files/" + fileID + "/" + header.Filename,
|
||||
})
|
||||
}
|
||||
|
||||
15
main.go
15
main.go
@@ -15,16 +15,13 @@ func main() {
|
||||
r.GET("/", endpoints.HandleIndex)
|
||||
r.POST("/upload", endpoints.HandleFileUpload)
|
||||
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) {
|
||||
c.JSON(200, gin.H{"status": "ok"})
|
||||
})
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
if port := os.Getenv("PORT"); port != "" {
|
||||
fmt.Printf("FITRA Backend starting on port %s\n", port)
|
||||
r.Run(":" + port)
|
||||
} else {
|
||||
fmt.Println("FITRA Backend starting on port 8080")
|
||||
r.Run(":8080")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,21 +26,21 @@
|
||||
<div class="endpoint">
|
||||
<h3><span class="method post">POST</span>/upload</h3>
|
||||
<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>
|
||||
</div>
|
||||
|
||||
<div class="endpoint">
|
||||
<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>cURL Example:</strong></p>
|
||||
<p><strong>cURL example:</strong></p>
|
||||
<pre><code>curl -O {{.BaseURL}}/uploads/{fileID}/{filename}</code></pre>
|
||||
</div>
|
||||
|
||||
<div class="endpoint">
|
||||
<h3><span class="method get">GET</span>/health</h3>
|
||||
<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>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user