File tree Expand file tree Collapse file tree 1 file changed +10
-14
lines changed Expand file tree Collapse file tree 1 file changed +10
-14
lines changed Original file line number Diff line number Diff line change @@ -3,30 +3,26 @@ package utils
3
3
import (
4
4
"bytes"
5
5
"crypto/rand"
6
- "encoding/binary "
6
+ "encoding/base64 "
7
7
"log"
8
- math_rand "math/rand"
9
- "strconv"
10
8
)
11
9
12
10
/*
13
- VideoIDGen returns an unique videoID and appends the fileExtension to it,
11
+ FileIDGen returns an unique videoID and appends the fileExtension to it,
14
12
it takes the fileExtensionas parameter
15
13
*/
16
14
func FileIDGen (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 )
21
17
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
+ }
24
22
25
- // Generate a 8 digit random number
26
- randomNumber := math_rand .Intn (99999999 - 10000000 ) + 10000000
23
+ uid := base64 .URLEncoding .EncodeToString (randomBytes )[:length ]
27
24
28
- // VideoID = (8-digit random number) + (file Name)
29
- return strconv .Itoa (randomNumber ) + fileExtension
25
+ return uid + fileExtension
30
26
}
31
27
32
28
// WrapStringInQuotes returns the string wrapped in quotes
You can’t perform that action at this time.
0 commit comments