-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgosfile.go
115 lines (89 loc) · 3.02 KB
/
gosfile.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package gosfile
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"github.com/firdasafridi/gocrypt"
)
// GosFile contains functions for file encryption.
type GosFile struct {
option gocrypt.GocryptOption
listSupportedMime []string
}
// New creates a new GosFile instance with the given configuration.
// It returns an error if the configuration validation fails or if the encryption option cannot be retrieved.
func New(cfg *Config) (*GosFile, error) {
if err := cfg.validation(); err != nil {
return nil, err
}
cryptOption, err := cfg.getEncryption()
if err != nil {
return nil, err
}
return &GosFile{
option: cryptOption,
listSupportedMime: cfg.SupportedMime,
}, nil
}
// EncryptFromPath encrypts a file from the given path.
// It returns an Option containing the encrypted data or an error.
func (gosFile *GosFile) EncryptFromPath(path string) *Option {
fileB, err := os.ReadFile(path)
if err != nil {
return newOption("", err)
}
return gosFile.EncryptFromByte(fileB)
}
// EncryptFromByte encrypts a byte slice of file data.
// It returns an Option containing the encrypted data or an error.
func (gosFile *GosFile) EncryptFromByte(fileB []byte) *Option {
mimeType := http.DetectContentType(fileB)
if !gosFile.isSupportedMime(mimeType) {
return newOption("", ErrMimeNotSupported)
}
encodeString := fmt.Sprintf("data:%s;base64,%s", mimeType,
string(base64.StdEncoding.EncodeToString(fileB)))
return gosFile.EncryptFromBase64(encodeString)
}
// EncryptFromBase64 encrypts a base64 encoded string.
// It returns an Option containing the encrypted data or an error.
func (gosFile *GosFile) EncryptFromBase64(encodeString string) *Option {
encString, err := gosFile.option.Encrypt([]byte(encodeString))
if err != nil {
return newOption("", ErrMimeNotSupported)
}
return newOption(encString, nil)
}
// isSupportedMime checks if the given MIME type is supported for encryption.
// It returns true if supported, otherwise false.
func (gosFile *GosFile) isSupportedMime(mimeContent string) bool {
for _, mimeType := range gosFile.listSupportedMime {
if mimeContent == mimeType {
return true
}
}
return false
}
// EncryptFromFile is a placeholder function, it currently returns nil and should be implemented to encrypt a file from the given path.
func (gosFile *GosFile) EncryptFromFile(path string) *Option {
return nil
}
// DecryptFromPath decrypts encrypted data from a file at the given path.
// It returns an Option containing the decrypted data or an error.
func (gosFile *GosFile) DecryptFromPath(path string) *Option {
encByte, err := os.ReadFile(path)
if err != nil {
return newOption("", err)
}
return gosFile.DecryptFromByte(encByte)
}
// DecryptFromByte decrypts a byte slice of encrypted data.
// It returns an Option containing the decrypted data or an error.
func (gosFile *GosFile) DecryptFromByte(encByte []byte) *Option {
decString, err := gosFile.option.Decrypt(encByte)
if err != nil {
return newOption("", ErrMimeNotSupported)
}
return newOption(decString, nil)
}