diff --git a/src/handlers/docs/handleDocUpload.go b/src/handlers/docs/handleDocUpload.go index 041d48b..c0cd5cf 100644 --- a/src/handlers/docs/handleDocUpload.go +++ b/src/handlers/docs/handleDocUpload.go @@ -57,7 +57,6 @@ func HandleDocUpload(c *gin.Context) { } else { filename = newName + filepath.Ext(fileHeader.Filename) } - savedFileName, alreadyExists := database.AddDoc(filename, fileHashBuffer[:]) filteredFilename, err := util.FilterFilename(filename) if err != nil { @@ -65,6 +64,8 @@ func HandleDocUpload(c *gin.Context) { return } + savedFileName, alreadyExists := database.AddDoc(filename, fileHashBuffer[:]) + if !alreadyExists { err = c.SaveUploadedFile(fileHeader, util.ExPath+"/uploads/docs/"+filteredFilename) if err != nil { diff --git a/src/handlers/image/handleImageUpload.go b/src/handlers/image/handleImageUpload.go index 6914c49..eeb53c0 100644 --- a/src/handlers/image/handleImageUpload.go +++ b/src/handlers/image/handleImageUpload.go @@ -24,14 +24,17 @@ func HandleImageUpload(c *gin.Context) { c.String(http.StatusBadRequest, "Failed to open file: %s", err.Error()) return } + defer file.Close() fileBuffer := make([]byte, 512) + _, err = file.Read(fileBuffer) if err != nil { c.String(http.StatusInternalServerError, "Failed to read file: %s", err.Error()) return } + fileType := http.DetectContentType(fileBuffer) allowedMimeTypes := map[string]bool{ @@ -58,14 +61,13 @@ func HandleImageUpload(c *gin.Context) { filename = newName + filepath.Ext(fileHeader.Filename) } - savedFilename, alreadyExists := database.AddImage(filename, fileHashBuffer[:]) - filteredFilename, err := util.FilterFilename(filename) if err != nil { c.String(http.StatusBadRequest, err.Error()) return } + savedFilename, alreadyExists := database.AddImage(filename, fileHashBuffer[:]) if !alreadyExists { err = c.SaveUploadedFile(fileHeader, util.ExPath+"/uploads/images/"+filteredFilename) if err != nil {