feat: make sure similar posts get marked before saving anything

This commit is contained in:
Damillora 2025-02-05 18:10:30 +00:00
parent 50bf4efc75
commit 0b50cede44
3 changed files with 31 additions and 28 deletions

1
go.mod
View File

@ -101,6 +101,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.9.5 // indirect
github.com/h2non/bimg v1.1.9 // indirect
github.com/hashicorp/consul/api v1.3.0 // indirect
github.com/hashicorp/consul/sdk v0.3.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect

2
go.sum
View File

@ -184,6 +184,8 @@ github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgf
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/h2non/bimg v1.1.5 h1:o3xsUBxM8s7+e7PmpiWIkEYdeYayJ94eh4cJLx67m1k=
github.com/h2non/bimg v1.1.5/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8=
github.com/h2non/bimg v1.1.9 h1:WH20Nxko9l/HFm4kZCA3Phbgu2cbHvYzxwxn9YROEGg=
github.com/h2non/bimg v1.1.9/go.mod h1:R3+UiYwkK4rQl6KVFTOFJHitgLbZXBZNFh2cv3AEbp8=
github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE=
github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=

View File

@ -107,6 +107,28 @@ func uploadBlob(c *gin.Context) {
hashSlice := make([]byte, 8)
binary.LittleEndian.PutUint64(hashSlice, hashInt)
if len(similarPosts) > 0 {
c.JSON(http.StatusOK,
models.BlobSimilarResponse{
ID: id,
Width: width,
Height: height,
Similar: similarPosts,
})
return
}
filename := id + filepath.Ext(file.Filename)
filePath := filepath.Join(dataDir, folder1, folder2, filename)
err = c.SaveUploadedFile(file, filePath)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse{
Code: http.StatusBadRequest,
Message: err.Error(),
})
return
}
previewImage := imaging.Resize(originalImage, 1000, 0, imaging.Lanczos)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse{
@ -158,17 +180,6 @@ func uploadBlob(c *gin.Context) {
return
}
filename := id + filepath.Ext(file.Filename)
filePath := filepath.Join(dataDir, folder1, folder2, filename)
err = c.SaveUploadedFile(file, filePath)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse{
Code: http.StatusBadRequest,
Message: err.Error(),
})
return
}
blob := database.Blob{
ID: id,
FilePath: filepath.Join(folder1, folder2, filename),
@ -184,21 +195,10 @@ func uploadBlob(c *gin.Context) {
database.DB.Create(&blob)
if len(similarPosts) > 0 {
c.JSON(http.StatusOK,
models.BlobSimilarResponse{
ID: id,
Width: width,
Height: height,
Similar: similarPosts,
})
return
} else {
c.JSON(http.StatusOK, models.BlobResponse{
ID: id,
Width: width,
Height: height,
})
return
}
c.JSON(http.StatusOK, models.BlobResponse{
ID: id,
Width: width,
Height: height,
})
return
}