From 084b12af799ec0bfe762d4262a99b0ccbff8b9a4 Mon Sep 17 00:00:00 2001 From: Abhishek Raj Date: Sat, 28 Oct 2023 15:06:28 +0530 Subject: [PATCH 1/3] fix: lint issue and unused var --- controllers/upload.go | 7 +++---- utils/helper.go | 9 ++++----- utils/upload.go | 6 +++--- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/controllers/upload.go b/controllers/upload.go index b764c5d..879aeed 100644 --- a/controllers/upload.go +++ b/controllers/upload.go @@ -10,18 +10,17 @@ import ( func GetPresignedURL(c *gin.Context) { fileName := c.Query("fileName") - fileType := c.Query("fileType") basePath := c.Query("basePath") extension := filepath.Ext(fileName) - if fileName == "" || extension == "" || fileType == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "Missing required query parameter 'fileName' or 'fileType'"}) + if fileName == "" || extension == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "Missing required query parameter 'fileName' or it has no extension"}) return } videoID := utils.VideoIDGen(extension) - url := utils.GetSignedURL(videoID, fileType, basePath) + url := utils.GetSignedURL(videoID, basePath) if url == "" { c.JSON(http.StatusInternalServerError, gin.H{"error": "Error generating presigned URL", "details": ""}) return diff --git a/utils/helper.go b/utils/helper.go index d7f05ff..9386e7d 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -2,10 +2,9 @@ package utils import ( "bytes" - "crypto/rand" "encoding/binary" "log" - math_rand "math/rand" + "math/rand" "strconv" ) @@ -19,11 +18,11 @@ func VideoIDGen(fileExtension string) string { return err.Error() } - var i int64 = int64(binary.LittleEndian.Uint64(b[:])) - math_rand.Seed(i) + seed := int64(binary.LittleEndian.Uint64(b[:])) + r := rand.New(rand.NewSource(seed)) // Generate a 8 digit random number - randomNumber := math_rand.Intn(99999999-10000000) + 10000000 + randomNumber := r.Intn(99999999-10000000) + 10000000 // VideoID = (8-digit random number) + (file Name) return strconv.Itoa(randomNumber) + fileExtension diff --git a/utils/upload.go b/utils/upload.go index e924781..2809884 100644 --- a/utils/upload.go +++ b/utils/upload.go @@ -245,10 +245,10 @@ func GetWhichCloudIsEnabled() (bool, bool, bool) { return false, false, false } -func GetSignedURL(videoId string, fileType string, basePath string) string { +func GetSignedURL(videoId string, basePath string) string { isAWS, isGCP, isAzure := GetWhichCloudIsEnabled() - filePath, err := GetCloudStoragePath(basePath, videoId, fileType) + filePath, err := GetCloudStoragePath(basePath, videoId) LogErr(err) if isAWS { @@ -352,7 +352,7 @@ func generateAzureSignedURL(filePath string) string { return sasURL } -func GetCloudStoragePath(basePath, fileName string, _ string) (string, error) { +func GetCloudStoragePath(basePath, fileName string) (string, error) { url, err := url.JoinPath(basePath, fileName) LogErr(err) From dfb9d514d7ae07b6ec7dac8592acd7899f33ddb2 Mon Sep 17 00:00:00 2001 From: Abhishek Raj Date: Sat, 28 Oct 2023 15:57:09 +0530 Subject: [PATCH 2/3] fix: crypto rand package import --- utils/helper.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/helper.go b/utils/helper.go index 9386e7d..db1e5db 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -2,9 +2,10 @@ package utils import ( "bytes" + "crypto/rand" "encoding/binary" "log" - "math/rand" + mathRand "math/rand" "strconv" ) @@ -19,7 +20,7 @@ func VideoIDGen(fileExtension string) string { } seed := int64(binary.LittleEndian.Uint64(b[:])) - r := rand.New(rand.NewSource(seed)) + r := mathRand.New(mathRand.NewSource(seed)) // Generate a 8 digit random number randomNumber := r.Intn(99999999-10000000) + 10000000 From a7b1dd030abe3b53379d3e1ccba62b2cf5988cf1 Mon Sep 17 00:00:00 2001 From: Abhishek Raj Date: Mon, 6 Nov 2023 14:27:15 +0530 Subject: [PATCH 3/3] fix: deepsource issue --- utils/helper.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/utils/helper.go b/utils/helper.go index db1e5db..a3c86c2 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -3,10 +3,8 @@ package utils import ( "bytes" "crypto/rand" - "encoding/binary" + "encoding/base64" "log" - mathRand "math/rand" - "strconv" ) /* @@ -14,19 +12,17 @@ VideoIDGen returns an unique videoID and appends the fileExtension to it, it takes the fileExtensionas parameter */ func VideoIDGen(fileExtension string) string { - var b [8]byte - if _, err := rand.Read(b[:]); err != nil { - return err.Error() - } + length := 8 + randomBytes := make([]byte, length) - seed := int64(binary.LittleEndian.Uint64(b[:])) - r := mathRand.New(mathRand.NewSource(seed)) + _, err := rand.Read(randomBytes) + if err != nil { + return "" + } - // Generate a 8 digit random number - randomNumber := r.Intn(99999999-10000000) + 10000000 + uid := base64.URLEncoding.EncodeToString(randomBytes)[:length] - // VideoID = (8-digit random number) + (file Name) - return strconv.Itoa(randomNumber) + fileExtension + return uid + fileExtension } // WrapStringInQuotes returns the string wrapped in quotes