Skip to content

Commit

Permalink
[Feature] Change the filename from uuid to existing or declared filen…
Browse files Browse the repository at this point in the history
…ame (#11)

* chore: change filename to unique

* feat: change filename to existing or declared filename instead of uuid
  • Loading branch information
kevinanielsen authored Dec 14, 2023
1 parent 51629c8 commit 209f1cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion models/docModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import "gorm.io/gorm"
type Doc struct {
gorm.Model

FileName string `json:"file_name"`
FileName string `json:"file_name" gorm:"unique"`
Checksum []byte `json:"checksum"`
}
2 changes: 1 addition & 1 deletion models/imageModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import "gorm.io/gorm"
type Image struct {
gorm.Model

FileName string `json:"file_name"`
FileName string `json:"file_name" gorm:"unique"`
Checksum []byte `json:"checksum"`
}
9 changes: 7 additions & 2 deletions util/handlers/docs/handleDocsUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-fast-cdn/database"
"github.com/go-fast-cdn/util"
"github.com/google/uuid"
)

func HandleDocsUpload(c *gin.Context) {
fileHeader, err := c.FormFile("doc")
newName := c.PostForm("filename")

if err != nil {
c.String(http.StatusBadRequest, "Failed to read file: %s", err.Error())
Expand Down Expand Up @@ -51,7 +51,12 @@ func HandleDocsUpload(c *gin.Context) {
}

fileHashBuffer := md5.Sum(fileBuffer)
fileName := uuid.NewString() + filepath.Ext(fileHeader.Filename)
var fileName string
if newName == "" {
fileName = fileHeader.Filename
} else {
fileName = newName + filepath.Ext(fileHeader.Filename)
}
savedFileName, alreadyExists := database.AddDoc(fileName, fileHashBuffer[:])

if !alreadyExists {
Expand Down
13 changes: 11 additions & 2 deletions util/handlers/image/handleImageUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-fast-cdn/database"
"github.com/go-fast-cdn/util"
"github.com/google/uuid"
)

func HandleImageUpload(c *gin.Context) {
fileHeader, err := c.FormFile("image")
newName := c.PostForm("filename")

if err != nil {
c.String(http.StatusBadRequest, "Failed to read file: %s", err.Error())
Expand Down Expand Up @@ -49,7 +49,16 @@ func HandleImageUpload(c *gin.Context) {
}

fileHashBuffer := md5.Sum(fileBuffer)
fileName := uuid.NewString() + filepath.Ext(fileHeader.Filename)

var fileName string

if newName == "" {

fileName = fileHeader.Filename
} else {
fileName = newName + filepath.Ext(fileHeader.Filename)
}

savedFileName, alreadyExists := database.AddImage(fileName, fileHashBuffer[:])

if !alreadyExists {
Expand Down

0 comments on commit 209f1cb

Please sign in to comment.