diff --git a/go.mod b/go.mod index 0273651..880c670 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3ef57ad..64e47df 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/app/blob_routes.go b/pkg/app/blob_routes.go index 5410776..227b2dd 100644 --- a/pkg/app/blob_routes.go +++ b/pkg/app/blob_routes.go @@ -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 }