Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lint issue and unused var #77

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions controllers/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@ package utils
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/base64"
"log"
math_rand "math/rand"
"strconv"
)

/*
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)

var i int64 = int64(binary.LittleEndian.Uint64(b[:]))
math_rand.Seed(i)
_, err := rand.Read(randomBytes)
if err != nil {
return ""
}

// Generate a 8 digit random number
randomNumber := math_rand.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
Expand Down
6 changes: 3 additions & 3 deletions utils/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down