github.com/0chain/gosdk@v1.17.11/winsdk/browser.go (about)

     1  package main
     2  
     3  /*
     4  #include <stdlib.h>
     5  */
     6  import (
     7  	"C"
     8  )
     9  
    10  import (
    11  	"errors"
    12  	"strings"
    13  
    14  	"github.com/0chain/gosdk/zboxcore/sdk"
    15  )
    16  
    17  type RemoteFile struct {
    18  	sdk.FileInfo
    19  	Name string `json:"name"`
    20  	Path string `json:"path"`
    21  }
    22  
    23  // ListAll - list all files from blobbers
    24  // ## Inputs
    25  //   - allocationID
    26  //
    27  // ## Outputs
    28  //
    29  //	{
    30  //	"error":"",
    31  //	"result":[{},{}]",
    32  //	}
    33  //
    34  //export ListAll
    35  func ListAll(allocationID *C.char) *C.char {
    36  	defer func() {
    37  		if r := recover(); r != nil {
    38  			log.Error("win: crash ", r)
    39  		}
    40  	}()
    41  	alloc, err := getAllocation(C.GoString(allocationID))
    42  	if err != nil {
    43  		log.Error("win: ", err)
    44  		return WithJSON("", err)
    45  	}
    46  
    47  	ref, err := alloc.GetRemoteFileMap(nil, "/")
    48  	if err != nil {
    49  		log.Error("win: ", err)
    50  		return WithJSON("", err)
    51  	}
    52  
    53  	files := make([]RemoteFile, 0)
    54  	for path, data := range ref {
    55  		paths := strings.SplitAfter(path, "/")
    56  		var f = RemoteFile{
    57  			Name:     paths[len(paths)-1],
    58  			Path:     path,
    59  			FileInfo: data,
    60  		}
    61  		files = append(files, f)
    62  	}
    63  
    64  	return WithJSON(files, nil)
    65  }
    66  
    67  // List - list files from blobbers
    68  // ## Inputs
    69  //   - allocationID
    70  //   - remotePath
    71  //
    72  // - authTicket
    73  // - lookupHash
    74  //
    75  // ## Outputs
    76  //
    77  //	{
    78  //	"error":"",
    79  //	"result":[{},{}]",
    80  //	}
    81  //
    82  //export List
    83  func List(allocationID, remotePath, authTicket, lookupHash *C.char) *C.char {
    84  	defer func() {
    85  		if r := recover(); r != nil {
    86  			log.Error("win: crash ", r)
    87  		}
    88  	}()
    89  	allocID := C.GoString(allocationID)
    90  	remotepath := C.GoString(remotePath)
    91  	authticket := C.GoString(authTicket)
    92  	lookuphash := C.GoString(lookupHash)
    93  
    94  	if len(remotepath) == 0 && len(authticket) == 0 {
    95  		return WithJSON("[]", errors.New("Error: remotepath / authticket flag is missing"))
    96  	}
    97  
    98  	if len(remotepath) > 0 {
    99  		if len(allocID) == 0 {
   100  			return WithJSON("[]", errors.New("Error: allocationID is missing"))
   101  		}
   102  
   103  		allocationObj, err := getAllocation(allocID)
   104  		if err != nil {
   105  			log.Error("win: ", err)
   106  			return WithJSON("[]", err)
   107  		}
   108  
   109  		ref, err := allocationObj.ListDir(remotepath)
   110  		if err != nil {
   111  			if err != nil {
   112  				log.Error("win: ", err)
   113  				return WithJSON("[]", err)
   114  			}
   115  		}
   116  
   117  		return WithJSON(ref.Children, nil)
   118  	}
   119  	if len(authticket) > 0 {
   120  
   121  		if len(lookuphash) == 0 {
   122  			return WithJSON("[]", errors.New("Error: lookuphash flag is missing"))
   123  		}
   124  
   125  		allocationObj, _, err := getAllocationWith(authticket)
   126  		if err != nil {
   127  			log.Error("win: ", err)
   128  			return WithJSON("[]", err)
   129  		}
   130  
   131  		at := sdk.InitAuthTicket(authticket)
   132  		lookuphash, err = at.GetLookupHash()
   133  		if err != nil {
   134  			log.Error("win: ", err)
   135  			return WithJSON("[]", err)
   136  		}
   137  
   138  		ref, err := allocationObj.ListDirFromAuthTicket(authticket, lookuphash)
   139  		if err != nil {
   140  			log.Error("win: ", err)
   141  			return WithJSON("[]", err)
   142  		}
   143  
   144  		return WithJSON(ref.Children, nil)
   145  	}
   146  	return WithJSON("[]", nil)
   147  }