You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.6 KiB

package fileserver
import (
"ehttp/http"
"epur-pay/cache"
fileConfig "epur-pay/pkg/fileserver/config"
"epur-pay/pkg/utils"
"fmt"
"io"
"reflect"
)
type File struct {
FileName *string
Data interface{}
DataLen int64
ContentType *string
Encryption bool
Key string
Iv string
}
func New() {
// 根据类型加载存储方式
storeType := utils.GetStoreType(cache.Global.Caches.Config.Get("storeType"))
storeJson := cache.Global.Caches.Config.GetMap(fmt.Sprintf("storeJson_%s", storeType))
if storeJson != nil {
fileConfig.New(&fileConfig.FileStoreRequest{
AccessKeyId: storeJson["AccessKeyId"].(string),
AccessKeySecret: storeJson["AccessKeySecret"].(string),
BucketName: storeJson["BucketName"].(string),
EndPoint: storeJson["EndPoint"].(string),
Access: storeJson["Access"].(string),
RegionId: storeJson["regionId"].(string),
StoreType: storeType,
})
}
}
// 文件上传
func (f *File) UpLoad() string {
filePathResponse := ""
f.encryption()
filePathResponse = fileConfig.Cf.Store.Api.Put(*f.FileName, "", f.Data)
return filePathResponse
}
// 加密
func (f *File) encryption() {
typeOf := reflect.TypeOf(f.Data).Kind()
handlerData := make([]byte, f.DataLen+1)
//logger.AccessLogger.Infoln("类型:", typeOf)
switch typeOf {
case reflect.String:
handlerData = f.DownLoad(f.Data.(string))
case reflect.Slice:
handlerData = f.Data.([]byte)
case reflect.Struct:
_, err := f.Data.(io.Reader).Read(handlerData)
if err != nil {
panic(err)
}
//case reflect.Ptr:
// _, err := f.Data.(*os.File).Read(handlerData)
// if err != nil {
// panic(err)
// }
default:
panic("数据不合法!")
}
if f.Encryption {
f.Data = handlerData
//s := aes.Encrypt(handlerData, []byte("PTIU3VR6HdfhziklcFQBXee1lkdpnesr"))
//encoded := base64.StdEncoding.EncodeToString(handlerData)
//f.ContentType = utils.PString("application/octet-stream")
//f.Data = []byte(aesnew.P1(f.Key, f.Iv, base64.StdEncoding.EncodeToString(handlerData)))
} else {
f.Data = handlerData
}
}
// 文件下载
func (f *File) DownLoad(url string) []byte {
r := http.New(
http.WithUrl(url),
http.WithMethod(http.GET),
)
if err := r.Do(); err != nil {
panic(err.Error())
}
return r.Result
}
// 文件解密
func (f *File) DecryptFromUrl(url string) string {
result := f.DownLoad(url)
return string(result)
//return aesnew.P2(f.Key, f.Iv, string(result))
}
func (f *File) SetKeyIv(key, iv string) {
f.Key = key
f.Iv = iv
}
//func GetImageKeyIv(uid int64) (string, string) {
// return kk.KK2(fmt.Sprintf("%d", uid), 6), kk.KK2(fmt.Sprintf("%d", uid), 10)
//}