Skip to content

Commit

Permalink
feat: wasm add upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
FGadvancer committed Jul 15, 2023
1 parent 88d9051 commit 4174b33
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 2 deletions.
1 change: 1 addition & 0 deletions open_im_sdk/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ import (
func UploadFile(callback open_im_sdk_callback.Base, operationID string, req string, progress open_im_sdk_callback.UploadFileCallback) {
call(callback, operationID, UserForSDK.File().UploadFile, req, file.UploadFileCallback(progress))
}

90 changes: 90 additions & 0 deletions wasm/event_listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package event_listener

import (
"open_im_sdk/internal/file"
"open_im_sdk/open_im_sdk_callback"
"open_im_sdk/pkg/utils"
"open_im_sdk/sdk_struct"
"syscall/js"
Expand Down Expand Up @@ -188,6 +190,94 @@ func (s *SendMessageCallback) OnProgress(progress int) {
mReply["clientMsgID"] = s.clientMsgID
s.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}
type UploadInterface interface {
open_im_sdk_callback.Base
open_im_sdk_callback.UploadFileCallback
}
var _ UploadInterface = (*UploadFileCallback)(nil)
type UploadFileCallback struct {
BaseCallback
globalEvent CallbackWriter
Uuid string
}

func NewUploadFileCallback(funcName string, callback *js.Value) *UploadFileCallback {
return &UploadFileCallback{BaseCallback: BaseCallback{CallbackWriter: NewPromiseHandler().SetEvent(funcName)}, globalEvent: NewEventData(callback).SetEvent(funcName)}
}
func (u *UploadFileCallback) SetUuid(args *[]js.Value)*UploadFileCallback{
f := file.UploadFileReq{}
utils.JsonStringToStruct((*args)[1].String(), &f)
u.Uuid = f.Uuid
return u
}
func (u *UploadFileCallback) Open(size int64) {
mReply := make(map[string]interface{})
mReply["size"] = size
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}

func (u *UploadFileCallback) PartSize(partSize int64, num int) {
mReply := make(map[string]interface{})
mReply["partSize"] = partSize
mReply["num"] = num
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}

func (u *UploadFileCallback) HashPartProgress(index int, size int64, partHash string) {
mReply := make(map[string]interface{})
mReply["index"] = index
mReply["size"] = size
mReply["partHash"] = partHash
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}

func (u *UploadFileCallback) HashPartComplete(partsHash string, fileHash string) {
mReply := make(map[string]interface{})
mReply["partsHash"] = partsHash
mReply["fileHash"] = fileHash
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}

func (u *UploadFileCallback) UploadID(uploadID string) {
mReply := make(map[string]interface{})
mReply["uploadID"] = uploadID
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}

func (u *UploadFileCallback) UploadPartComplete(index int, partSize int64, partHash string) {
mReply := make(map[string]interface{})
mReply["index"] = index
mReply["partSize"] = partSize
mReply["partHash"] = partHash
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}

func (u *UploadFileCallback) UploadComplete(fileSize int64, streamSize int64, storageSize int64) {
mReply := make(map[string]interface{})
mReply["fileSize"] = fileSize
mReply["streamSize"] = streamSize
mReply["storageSize"] = storageSize
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}

func (u *UploadFileCallback) Complete(size int64, url string, typ int) {
mReply := make(map[string]interface{})
mReply["size"] = size
mReply["url"] = url
mReply["typ"] = typ
mReply["uuid"] = u.Uuid
u.globalEvent.SetEvent(utils.GetSelfFuncName()).SetData(utils.StructToJsonString(mReply)).SendMessage()
}




type BatchMessageCallback struct {
CallbackWriter
Expand Down
73 changes: 71 additions & 2 deletions wasm/wasm_wrapper/wasm_third.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package wasm_wrapper

import (
"open_im_sdk/open_im_sdk"
"open_im_sdk/open_im_sdk_callback"
"open_im_sdk/pkg/utils"
"open_im_sdk/wasm/event_listener"
"syscall/js"
Expand All @@ -37,6 +38,74 @@ func (w *WrapperThird) UpdateFcmToken(_ js.Value, args []js.Value) interface{} {
return event_listener.NewCaller(open_im_sdk.UpdateFcmToken, callback, &args).AsyncCallWithCallback()
}
func (w *WrapperThird) UploadFile(_ js.Value, args []js.Value) interface{} {
callback := event_listener.NewBaseCallback(utils.FirstLower(utils.GetSelfFuncName()), w.commonFunc)
return event_listener.NewCaller(open_im_sdk.UploadFile, callback, &args).AsyncCallWithCallback()
callback := event_listener.NewUploadFileCallback(utils.FirstLower(utils.GetSelfFuncName()), w.commonFunc).SetUuid(&args)
return event_listener.NewCaller(UploadFile, callback, &args).AsyncCallWithCallback()
}

var _ open_im_sdk_callback.Base = (*TempBase)(nil)

type TempBase struct {
u event_listener.UploadInterface
}

func NewTempBase(u event_listener.UploadInterface) *TempBase {
return &TempBase{u: u}
}

func (t TempBase) OnError(errCode int32, errMsg string) {
t.u.OnError(errCode,errMsg)
}

func (t TempBase) OnSuccess(data string) {
t.u.OnSuccess(data)
}

var _ open_im_sdk_callback.UploadFileCallback = (*TempUploadFile)(nil)

type TempUploadFile struct {
u event_listener.UploadInterface

}

func NewTempUploadFile(u event_listener.UploadInterface) *TempUploadFile {
return &TempUploadFile{u: u}
}

func (t TempUploadFile) Open(size int64) {
t.u.Open(size)
}

func (t TempUploadFile) PartSize(partSize int64, num int) {
t.u.PartSize(partSize,num)
}

func (t TempUploadFile) HashPartProgress(index int, size int64, partHash string) {
t.u.HashPartProgress(index,size,partHash)
}

func (t TempUploadFile) HashPartComplete(partsHash string, fileHash string) {
t.u.HashPartComplete(partsHash,fileHash)
}

func (t TempUploadFile) UploadID(uploadID string) {
t.u.UploadID(uploadID)
}

func (t TempUploadFile) UploadPartComplete(index int, partSize int64, partHash string) {
t.u.UploadPartComplete(index,partSize,partHash)
}

func (t TempUploadFile) UploadComplete(fileSize int64, streamSize int64, storageSize int64) {
t.u.UploadComplete(fileSize,streamSize,storageSize)
}

func (t TempUploadFile) Complete(size int64, url string, typ int) {
t.u.Complete(size,url,typ)
}

func UploadFile(callback event_listener.UploadInterface, operationID string, req string) {
b:=NewTempBase(callback)
t:=NewTempUploadFile(callback)
open_im_sdk.UploadFile(b,operationID,req,t)
}

0 comments on commit 4174b33

Please sign in to comment.