38 lines
985 B
Go
38 lines
985 B
Go
package server
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid" // To generate random file names
|
|
"net/http"
|
|
"path/filepath"
|
|
)
|
|
|
|
func saveFileHandler(c *gin.Context) {
|
|
file, err := c.FormFile("file")
|
|
|
|
// The file cannot be received.
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
|
"message": "No file is received",
|
|
})
|
|
return
|
|
}
|
|
|
|
// Retrieve file information
|
|
extension := filepath.Ext(file.Filename)
|
|
// Generate random file name for the new uploaded file so it doesn't override the old file with same name
|
|
newFileName := uuid.New().String() + extension
|
|
|
|
// The file is received, so let's save it
|
|
if err := c.SaveUploadedFile(file, "/tmp/" + newFileName); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
|
"message": "Unable to save the file",
|
|
})
|
|
return
|
|
}
|
|
|
|
// File saved successfully. Return proper result
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Your file has been successfully uploaded.",
|
|
})
|
|
} |