Skip to content

Commit

Permalink
remove CreateDir, Rename, Copy, Move
Browse files Browse the repository at this point in the history
  • Loading branch information
boddumanohar committed Sep 9, 2023
1 parent d94e9f4 commit 11c6dd1
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 725 deletions.
32 changes: 14 additions & 18 deletions mobilesdk/zbox/allocation.go
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 @@ -369,22 +376,6 @@ func (a *Allocation) CancelRepair() error {
return a.sdkAllocation.CancelRepair()
}

// CopyObject - copy object from path to dest
func (a *Allocation) CopyObject(path string, destPath string) error {
if a == nil || a.sdkAllocation == nil {
return ErrInvalidAllocation
}
return a.sdkAllocation.CopyObject(path, 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)
}

// GetMinWriteRead - getting back cost for allocation
func (a *Allocation) GetMinWriteRead() (string, error) {
if a == nil || a.sdkAllocation == nil {
Expand Down Expand Up @@ -455,7 +446,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
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
147 changes: 32 additions & 115 deletions wasmsdk/blobber.go
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
5 changes: 0 additions & 5 deletions wasmsdk/proxy.go
Expand Up @@ -163,14 +163,9 @@ func main() {

//blobber
"delete": Delete,
"rename": Rename,
"copy": Copy,
"move": Move,
"share": Share,
"download": download,
"multiDownload": multiDownload,
"upload": upload,
"bulkUpload": bulkUpload,
"multiUpload": multiUpload,
"multiOperation": MultiOperation,
"listObjects": listObjects,
Expand Down

0 comments on commit 11c6dd1

Please sign in to comment.