Skip to content

Commit ab43dfa

Browse files
committed
feat: add validation if file exists
1 parent 0479069 commit ab43dfa

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/handlers/docs/handleDocUpload.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/gin-gonic/gin"
99
"github.com/kevinanielsen/go-fast-cdn/src/database"
10+
"github.com/kevinanielsen/go-fast-cdn/src/models"
1011
"github.com/kevinanielsen/go-fast-cdn/src/util"
1112
)
1213

@@ -65,14 +66,23 @@ func HandleDocUpload(c *gin.Context) {
6566
return
6667
}
6768

68-
savedFileName, alreadyExists := database.AddDoc(filename, fileHashBuffer[:])
69+
doc := models.Doc{
70+
FileName: filename,
71+
Checksum: fileHashBuffer[:],
72+
}
73+
74+
docInDatabase := database.GetDocByCheckSum(fileHashBuffer[:])
75+
if len(docInDatabase.Checksum) > 0 {
76+
c.JSON(http.StatusConflict, "File already exists")
77+
return
78+
}
6979

70-
if !alreadyExists {
71-
err = c.SaveUploadedFile(fileHeader, util.ExPath+"/uploads/docs/"+filteredFilename)
72-
if err != nil {
73-
c.String(http.StatusInternalServerError, "Failed to save file: %s", err.Error())
74-
return
75-
}
80+
savedFileName := database.AddDoc(doc)
81+
82+
err = c.SaveUploadedFile(fileHeader, util.ExPath+"/uploads/docs/"+filteredFilename)
83+
if err != nil {
84+
c.String(http.StatusInternalServerError, "Failed to save file: %s", err.Error())
85+
return
7686
}
7787

7888
body := gin.H{

0 commit comments

Comments
 (0)