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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:download file/thumbnail by authticket in winsdk #1265

Merged
merged 5 commits into from
Oct 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion winsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import (
//
//export GetAllocation
func GetAllocation(allocationID *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
allocID := C.GoString(allocationID)
return WithJSON(getAllocation(allocID))
}
Expand All @@ -41,6 +46,11 @@ func GetAllocation(allocationID *C.char) *C.char {
//
//export ListAllocations
func ListAllocations() *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
items, err := sdk.GetAllocations()
if err != nil {
log.Error("win: ", err)
Expand All @@ -61,7 +71,11 @@ func ListAllocations() *C.char {
//
//export CreateFreeAllocation
func CreateFreeAllocation(freemarker *C.char) *C.char {

defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
marker := &zboxapi.FreeMarker{}
js := C.GoString(freemarker)
err := json.Unmarshal([]byte(js), marker)
Expand Down
13 changes: 11 additions & 2 deletions winsdk/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type RemoteFile struct {
//
//export ListAll
func ListAll(allocationID *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
alloc, err := getAllocation(C.GoString(allocationID))
if err != nil {
log.Error("win: ", err)
Expand Down Expand Up @@ -76,7 +81,11 @@ func ListAll(allocationID *C.char) *C.char {
//
//export List
func List(allocationID, remotePath, authTicket, lookupHash *C.char) *C.char {

defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
allocID := C.GoString(allocationID)
remotepath := C.GoString(remotePath)
authticket := C.GoString(authTicket)
Expand Down Expand Up @@ -113,7 +122,7 @@ func List(allocationID, remotePath, authTicket, lookupHash *C.char) *C.char {
return WithJSON("[]", errors.New("Error: lookuphash flag is missing"))
}

allocationObj, err := getAllocationWith(authticket)
allocationObj, _, err := getAllocationWith(authticket)
if err != nil {
log.Error("win: ", err)
return WithJSON("[]", err)
Expand Down
9 changes: 5 additions & 4 deletions winsdk/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ func getAllocation(allocationID string) (*sdk.Allocation, error) {
return it.Allocation, nil
}

func getAllocationWith(authTicket string) (*sdk.Allocation, error) {
func getAllocationWith(authTicket string) (*sdk.Allocation, *marker.AuthTicket, error) {

sEnc, err := base64.StdEncoding.DecodeString(authTicket)
if err != nil {
return nil, errors.New("Error decoding the auth ticket." + err.Error())
return nil, nil, errors.New("Error decoding the auth ticket." + err.Error())
}
at := &marker.AuthTicket{}
err = json.Unmarshal(sEnc, at)
if err != nil {
return nil, errors.New("Error unmarshaling the auth ticket." + err.Error())
return nil, nil, errors.New("Error unmarshaling the auth ticket." + err.Error())
}
return getAllocation(at.AllocationID)
alloc, err := getAllocation(at.AllocationID)
return alloc, at, err
}

func getFileMeta(allocationID, remotePath string) (*sdk.ConsolidatedFileMeta, error) {
Expand Down
14 changes: 11 additions & 3 deletions winsdk/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"C"
)
import (
"encoding/base64"
"encoding/json"
"os"
"path/filepath"

"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/zboxcore/marker"
"github.com/0chain/gosdk/zboxcore/sdk"
)

func getZcnWorkDir() (string, error) {
Expand Down Expand Up @@ -57,9 +57,17 @@ func getLookupHash(allocationID, path string) string {
return encryption.Hash(allocationID + ":" + path)
}

func getAuthTicket(authTicket *C.char) (*marker.AuthTicket, string, error) {
func decodeAuthTicket(authTicket *C.char) (*marker.AuthTicket, string, error) {
at := C.GoString(authTicket)
t, err := sdk.InitAuthTicket(at).Unmarshall()
buf, err := base64.StdEncoding.DecodeString(at)
if err != nil {
return nil, at, err
}
t := &marker.AuthTicket{}
err = json.Unmarshal(buf, t)
if err != nil {
return nil, at, err
}

return t, at, err
}
57 changes: 55 additions & 2 deletions winsdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func main() {
//
//export SetLogFile
func SetLogFile(file *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()

f, err := os.OpenFile(C.GoString(file), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
Expand Down Expand Up @@ -81,6 +86,11 @@ func SetLogFile(file *C.char) *C.char {
//
//export InitSDKs
func InitSDKs(configJson *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()

l.Logger.Info("Start InitSDKs")

Expand Down Expand Up @@ -146,7 +156,11 @@ func InitSDKs(configJson *C.char) *C.char {
//
//export InitWallet
func InitWallet(clientJson *C.char) *C.char {

defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
l.Logger.Info("Start InitStorageSDK")

clientJs := C.GoString(clientJson)
Expand Down Expand Up @@ -188,6 +202,11 @@ var ErrInvalidSignatureScheme = errors.New("invalid_signature_scheme")
//
//export SignRequest
func SignRequest(privateKey, signatureScheme, data *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
key := C.GoString(privateKey)
scheme := C.GoString(signatureScheme)
d := C.GoString(data)
Expand All @@ -209,7 +228,11 @@ func SignRequest(privateKey, signatureScheme, data *C.char) *C.char {
//
//export VerifySignature
func VerifySignature(publicKey, signatureScheme string, data string, signature string) *C.char {

defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
hash := encryption.Hash(data)

signScheme := zcncrypto.NewSignatureScheme(signatureScheme)
Expand All @@ -233,6 +256,11 @@ func VerifySignature(publicKey, signatureScheme string, data string, signature s
//
//export CryptoJsEncrypt
func CryptoJsEncrypt(passphrase, message *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
pass := C.GoString(passphrase)
msg := C.GoString(message)

Expand All @@ -249,6 +277,11 @@ func CryptoJsEncrypt(passphrase, message *C.char) *C.char {
//
//export CryptoJsDecrypt
func CryptoJsDecrypt(passphrase, encryptedMessage *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
pass := C.GoString(passphrase)
msg := C.GoString(encryptedMessage)

Expand All @@ -265,6 +298,11 @@ func CryptoJsDecrypt(passphrase, encryptedMessage *C.char) *C.char {
//
//export GetPublicEncryptionKey
func GetPublicEncryptionKey(mnemonics *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
m := C.GoString(mnemonics)
return WithJSON(zcncore.GetPublicEncryptionKey(m))
}
Expand All @@ -281,6 +319,11 @@ func GetPublicEncryptionKey(mnemonics *C.char) *C.char {
//
//export GetLookupHash
func GetLookupHash(allocationID *C.char, path *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
hash := getLookupHash(C.GoString(allocationID), C.GoString(path))
return WithJSON(hash, nil)
}
Expand All @@ -296,6 +339,11 @@ func GetLookupHash(allocationID *C.char, path *C.char) *C.char {
//
//export SetFFmpeg
func SetFFmpeg(fullFileName *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
f := C.GoString(fullFileName)

_, err := os.Stat(f)
Expand All @@ -317,6 +365,11 @@ func SetFFmpeg(fullFileName *C.char) *C.char {
//
//export GetFileContentType
func GetFileContentType(file *C.char) *C.char {
defer func() {
if r := recover(); r != nil {
log.Error("win: crash ", r)
}
}()
f, err := os.Open(C.GoString(file))
if err != nil {
log.Error("win: ", err)
Expand Down