-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Enc.go
More file actions
49 lines (42 loc) · 1.39 KB
/
Copy pathBase64Enc.go
File metadata and controls
49 lines (42 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package Base64Enc
import (
"bufio"
"encoding/base64"
"io/ioutil"
"os"
"strings"
"github.com/h2non/filetype"
"github.com/h2non/filetype/types"
)
//Returns file type object given an array of bytes of a file.
func GetFileType(ByteArray []byte) types.Type {
FileType, _ := filetype.Match(ByteArray)
return FileType
}
//Loading Byte Slices into a Buffer and Return an array of bytes.
func FileToBuffer(file *os.File) []byte {
FileReader := bufio.NewReader(file)
content, _ := ioutil.ReadAll(FileReader) //Array of Byte Slices
return content
}
//Encode File to normal Base64
func FileToBase64(file *os.File) string {
ByteArray := FileToBuffer(file)
Base64 := base64.StdEncoding.EncodeToString(ByteArray)
return Base64
}
//Encode File to Base64url
func FileToBase64Url(file *os.File) string {
ByteArray := FileToBuffer(file)
Base64 := base64.StdEncoding.EncodeToString(ByteArray)
FileType := GetFileType(ByteArray)
Mime := FileType.MIME.Value
Url := "data:" + Mime + ";base64," + Base64
return Url
}
//Convert Base64/Base64Url Encoded string back to array of bytes.
func Base64ToByte(code string) []byte {
SplitStringArray := strings.Split(code, ",") //If Base64Url, [data, base64], if Base64. [base64]
ByteArray, _ := base64.StdEncoding.DecodeString(SplitStringArray[len(SplitStringArray)-1]) //Get the last element of the array.
return ByteArray
}