Skip to content

Commit

Permalink
Deprecate non multi-operation functions (0chain#1214)
Browse files Browse the repository at this point in the history
* remove CreateDir, Rename, Copy, Move

* use docker build wasm

* use @v4 version of git action

* Revert "use @v4 version of git action"

This reverts commit fbb93f2.
  • Loading branch information
boddumanohar authored Sep 11, 2023
1 parent d5d6721 commit 5fa2a66
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 726 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/sdk-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
fi
- name: Build
run: make wasm-build
run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.18 make wasm-build

- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2
Expand Down
32 changes: 28 additions & 4 deletions mobilesdk/zbox/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"time"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/blockchain"
"github.com/0chain/gosdk/zboxcore/sdk"
Expand Down Expand Up @@ -200,7 +201,13 @@ func (a *Allocation) RenameObject(remotePath string, destName string) error {
if a == nil || a.sdkAllocation == nil {
return ErrInvalidAllocation
}
return a.sdkAllocation.RenameObject(remotePath, destName)
return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationRename,
RemotePath: remotePath,
DestName: destName,
},
})
}

// GetStatistics - get allocation stats
Expand Down Expand Up @@ -374,15 +381,27 @@ func (a *Allocation) CopyObject(path string, destPath string) error {
if a == nil || a.sdkAllocation == nil {
return ErrInvalidAllocation
}
return a.sdkAllocation.CopyObject(path, destPath)
return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationRename,
RemotePath: path,
DestPath: destPath,
},
})
}

// MoveObject - move object from path to dest
func (a *Allocation) MoveObject(path string, destPath string) error {
if a == nil || a.sdkAllocation == nil {
return ErrInvalidAllocation
}
return a.sdkAllocation.MoveObject(path, destPath)
return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationRename,
RemotePath: path,
DestPath: destPath,
},
})
}

// GetMinWriteRead - getting back cost for allocation
Expand Down Expand Up @@ -455,7 +474,12 @@ func (a *Allocation) GetFirstSegment(localPath, remotePath, tmpPath string, dela
}

func (a *Allocation) CreateDir(dirName string) error {
return a.sdkAllocation.CreateDir(dirName)
return a.sdkAllocation.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationCreateDir,
RemotePath: dirName,
},
})
}

var currentPlayback StreamingImpl
Expand Down
35 changes: 29 additions & 6 deletions mobilesdk/zbox/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"strings"
"time"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
)

const SPACE string = " "

type fileResp struct {
sdk.FileInfo
Name string `json:"name"`
Expand Down Expand Up @@ -468,7 +467,13 @@ func RenameObject(allocationID, remotePath string, destName string) error {
if err != nil {
return err
}
return a.RenameObject(remotePath, destName)
return a.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationRename,
RemotePath: remotePath,
DestName: destName,
},
})
}

// GetStatistics - get allocation stats
Expand Down Expand Up @@ -747,7 +752,13 @@ func CopyObject(allocationID, path string, destPath string) error {
if err != nil {
return err
}
return a.CopyObject(path, destPath)
return a.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationCopy,
RemotePath: path,
DestPath: destPath,
},
})
}

// MoveObject - move object from path to dest
Expand All @@ -760,7 +771,14 @@ func MoveObject(allocationID, path string, destPath string) error {
if err != nil {
return err
}
return a.MoveObject(path, destPath)
return a.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationMove,
RemotePath: path,
DestPath: destPath,
},
})

}

// CreateDir create empty directoy on remote blobbers
Expand All @@ -773,7 +791,12 @@ func CreateDir(allocationID, dirName string) error {
if err != nil {
return err
}
return a.CreateDir(dirName)
return a.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationCreateDir,
RemotePath: dirName,
},
})
}

// RevokeShare revoke authTicket
Expand Down
14 changes: 1 addition & 13 deletions wasmsdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ set bls.SecretKey on runtime env(browser,nodejs...etc), and call `zcn.sdk.setWal

> N/A
### zcn.bulkUpload

bulk upload files. it will wrap options, and call `zcn.sdk.bulkUpload` to process upload

**Input**:

> bulkOptions: [ { allocationId:string,remotePath:string,file:FileReader, thumbnailBytes:[]byte, encrypt:bool,isUpdate:bool,isRepair:bool,numBlocks:int,callback:function(totalBytes, completedBytes, error) } ]
**Output**:

> [ {remotePath:"/d.png", success:true,error:""} ]
## ZCN methods

### zcn.sdk.init
Expand Down Expand Up @@ -512,7 +500,7 @@ upload file
> {commandSuccess:bool, error:string}
### zcn.sdk.bulkUpload
### zcn.sdk.multiUpload
bulk upload files with json options
Expand Down
147 changes: 32 additions & 115 deletions wasmsdk/blobber.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"syscall/js"
"time"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/pathutil"
"github.com/0chain/gosdk/core/sys"

"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/wasmsdk/jsbridge"
"github.com/0chain/gosdk/zboxcore/fileref"
Expand Down Expand Up @@ -52,7 +54,12 @@ func createDir(allocationID, remotePath string) error {
return err
}

return allocationObj.CreateDir(remotePath)
return allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationCreateDir,
RemotePath: remotePath,
},
})
}

// getFileStats get file stats from blobbers
Expand Down Expand Up @@ -152,7 +159,14 @@ func Rename(allocationID, remotePath, destName string) (*FileCommandResponse, er
return nil, err
}

err = allocationObj.RenameObject(remotePath, destName)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationRename,
RemotePath: remotePath,
DestName: destName,
},
})

if err != nil {
PrintError(err.Error())
return nil, err
Expand Down Expand Up @@ -187,7 +201,14 @@ func Copy(allocationID, remotePath, destPath string) (*FileCommandResponse, erro
return nil, err
}

err = allocationObj.CopyObject(remotePath, destPath)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationCopy,
RemotePath: remotePath,
DestPath: destPath,
},
})

if err != nil {
PrintError(err.Error())
return nil, err
Expand Down Expand Up @@ -222,7 +243,14 @@ func Move(allocationID, remotePath, destPath string) (*FileCommandResponse, erro
return nil, err
}

err = allocationObj.MoveObject(remotePath, destPath)
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationMove,
RemotePath: remotePath,
DestPath: destPath,
},
})

if err != nil {
PrintError(err.Error())
return nil, err
Expand Down Expand Up @@ -305,71 +333,6 @@ func Share(allocationID, remotePath, clientID, encryptionPublicKey string, expir

}

// download file
func download(
allocationID, remotePath, authTicket, lookupHash string,
downloadThumbnailOnly bool, numBlocks int, callbackFuncName string, isFinal bool) (*DownloadCommandResponse, error) {

wg := &sync.WaitGroup{}
statusBar := &StatusBar{wg: wg}
if callbackFuncName != "" {
callback := js.Global().Get(callbackFuncName)
statusBar.callback = func(totalBytes, completedBytes int, filename, objURL, err string) {
callback.Invoke(totalBytes, completedBytes, filename, objURL, err)
}
}
wg.Add(1)

if len(remotePath) == 0 && len(authTicket) == 0 {
return nil, RequiredArg("remotePath/authTicket")
}

fileName := strings.Replace(path.Base(remotePath), "/", "-", -1)
localPath := allocationID + "_" + fileName
var (
err error
downloader sdk.Downloader
)

fs, _ := sys.Files.Open(localPath)
mf, _ := fs.(*sys.MemFile)

downloader, err = sdk.CreateDownloader(allocationID, localPath, remotePath,
sdk.WithAuthticket(authTicket, lookupHash),
sdk.WithOnlyThumbnail(downloadThumbnailOnly),
sdk.WithBlocks(0, 0, numBlocks),
sdk.WithFileHandler(mf))

if err != nil {
PrintError(err.Error())
return nil, err
}

defer sys.Files.Remove(localPath) //nolint

err = downloader.Start(statusBar, isFinal)

if err == nil {
wg.Wait()
} else {
PrintError("Download failed.", err.Error())
return nil, err
}
if !statusBar.success {
return nil, errors.New("Download failed: unknown error")
}

resp := &DownloadCommandResponse{
CommandSuccess: true,
FileName: downloader.GetFileName(),
}

resp.Url = CreateObjectURL(mf.Buffer.Bytes(), "application/octet-stream")

return resp, nil

}

// MultiOperation - do copy, move, delete and createdir operation together
// ## Inputs
// - allocationID
Expand Down Expand Up @@ -548,52 +511,6 @@ func MultiOperation(allocationID string, jsonMultiUploadOptions string) error {
return allocationObj.DoMultiOperation(operations)
}

func bulkUpload(jsonBulkUploadOptions string) ([]BulkUploadResult, error) {
var options []BulkUploadOption
err := json.Unmarshal([]byte(jsonBulkUploadOptions), &options)
if err != nil {
return nil, err
}

n := len(options)
wait := make(chan BulkUploadResult, 1)

for _, option := range options {
go func(o BulkUploadOption) {
result := BulkUploadResult{
RemotePath: o.RemotePath,
}
defer func() { wait <- result }()

ok, err := uploadWithJsFuncs(o.AllocationID, o.RemotePath,
o.ReadChunkFuncName,
o.FileSize,
o.ThumbnailBytes.Buffer,
o.Webstreaming,
o.Encrypt,
o.IsUpdate,
o.IsRepair,
o.NumBlocks,
o.CallbackFuncName)
result.Success = ok
if err != nil {
result.Error = err.Error()
result.Success = false
}

}(option)

}

results := make([]BulkUploadResult, 0, n)
for i := 0; i < n; i++ {
result := <-wait
results = append(results, result)
}

return results, nil
}

func multiUpload(jsonBulkUploadOptions string) (MultiUploadResult, error) {
var options []BulkUploadOption
result := MultiUploadResult{}
Expand Down
2 changes: 1 addition & 1 deletion wasmsdk/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ <h2>please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates
}
})
}
const results = await goWasm.bulkUpload(objects)
const results = await goWasm.multiUpload(objects)
console.log(JSON.stringify(results))
}
})
Expand Down
Loading

0 comments on commit 5fa2a66

Please sign in to comment.