Skip to content

Commit 8b08eb9

Browse files
authored
fix: lint issue and unused var (#77)
* fix: lint issue and unused var * fix: crypto rand package import * fix: deepsource issue
1 parent a680fcd commit 8b08eb9

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

controllers/upload.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ import (
1010

1111
func GetPresignedURL(c *gin.Context) {
1212
fileName := c.Query("fileName")
13-
fileType := c.Query("fileType")
1413
basePath := c.Query("basePath")
1514
extension := filepath.Ext(fileName)
1615

17-
if fileName == "" || extension == "" || fileType == "" {
18-
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing required query parameter 'fileName' or 'fileType'"})
16+
if fileName == "" || extension == "" {
17+
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing required query parameter 'fileName' or it has no extension"})
1918
return
2019
}
2120

2221
videoID := utils.VideoIDGen(extension)
2322

24-
url := utils.GetSignedURL(videoID, fileType, basePath)
23+
url := utils.GetSignedURL(videoID, basePath)
2524
if url == "" {
2625
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error generating presigned URL", "details": ""})
2726
return

utils/helper.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,26 @@ package utils
33
import (
44
"bytes"
55
"crypto/rand"
6-
"encoding/binary"
6+
"encoding/base64"
77
"log"
8-
math_rand "math/rand"
9-
"strconv"
108
)
119

1210
/*
1311
VideoIDGen returns an unique videoID and appends the fileExtension to it,
1412
it takes the fileExtensionas parameter
1513
*/
1614
func VideoIDGen(fileExtension string) string {
17-
var b [8]byte
18-
if _, err := rand.Read(b[:]); err != nil {
19-
return err.Error()
20-
}
15+
length := 8
16+
randomBytes := make([]byte, length)
2117

22-
var i int64 = int64(binary.LittleEndian.Uint64(b[:]))
23-
math_rand.Seed(i)
18+
_, err := rand.Read(randomBytes)
19+
if err != nil {
20+
return ""
21+
}
2422

25-
// Generate a 8 digit random number
26-
randomNumber := math_rand.Intn(99999999-10000000) + 10000000
23+
uid := base64.URLEncoding.EncodeToString(randomBytes)[:length]
2724

28-
// VideoID = (8-digit random number) + (file Name)
29-
return strconv.Itoa(randomNumber) + fileExtension
25+
return uid + fileExtension
3026
}
3127

3228
// WrapStringInQuotes returns the string wrapped in quotes

utils/upload.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ func GetWhichCloudIsEnabled() (bool, bool, bool) {
245245
return false, false, false
246246
}
247247

248-
func GetSignedURL(videoId string, fileType string, basePath string) string {
248+
func GetSignedURL(videoId string, basePath string) string {
249249
isAWS, isGCP, isAzure := GetWhichCloudIsEnabled()
250250

251-
filePath, err := GetCloudStoragePath(basePath, videoId, fileType)
251+
filePath, err := GetCloudStoragePath(basePath, videoId)
252252
LogErr(err)
253253

254254
if isAWS {
@@ -352,7 +352,7 @@ func generateAzureSignedURL(filePath string) string {
352352
return sasURL
353353
}
354354

355-
func GetCloudStoragePath(basePath, fileName string, _ string) (string, error) {
355+
func GetCloudStoragePath(basePath, fileName string) (string, error) {
356356
url, err := url.JoinPath(basePath, fileName)
357357
LogErr(err)
358358

0 commit comments

Comments
 (0)