-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
10 create get fct to retrieve website on the blockchain (#45)
* Add Fetch and GetOwner functions to website pkg * Add zipper pkg with GetFileFromZip function * Update CLI main to show owner and index.html from downloaded website * Update .gitignore to exclude website archives * Improve Chunk tests and add some test cases * Remove useless error of prepareUploadParams function * Update website read.go to use FinalValue instead of CandidateValue
- Loading branch information
1 parent
9e773c9
commit 93a4f7a
Showing
6 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,8 @@ build | |
|
||
*.log | ||
|
||
# Websites archives | ||
*.zip | ||
|
||
# MacOS | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package zipper | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// GetFileFromZip returns the content of the given file from the zip file. | ||
// It returns an error if the file is not found in the zip. | ||
func GetFileFromZip(zipFilePath, fileName string) ([]byte, error) { | ||
zipReader, err := zip.OpenReader(zipFilePath) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("failed to open zip file: %w", err) | ||
} | ||
defer zipReader.Close() | ||
|
||
for _, file := range zipReader.File { | ||
if file.Name == fileName { | ||
rc, err := file.Open() | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("failed to open file in zip: %w", err) | ||
} | ||
defer rc.Close() | ||
|
||
buf, err := io.ReadAll(rc) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("failed to read file in zip: %w", err) | ||
} | ||
|
||
return buf, nil | ||
} | ||
} | ||
|
||
return []byte{}, fmt.Errorf("%s not found in zip", fileName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package website | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/massalabs/station/int/config" | ||
"github.com/massalabs/station/pkg/convert" | ||
"github.com/massalabs/station/pkg/node" | ||
) | ||
|
||
// Fetch retrieves the complete data of a website as bytes. | ||
func Fetch(network *config.NetworkInfos, websiteAddress string) ([]byte, error) { | ||
client := node.NewClient(network.NodeURL) | ||
|
||
chunkNumber, err := getNumberOfChunks(client, websiteAddress) | ||
if err != nil { | ||
return nil, fmt.Errorf("fetching number of chunks: %w", err) | ||
} | ||
|
||
dataStore, err := fetchAllChunks(client, websiteAddress, chunkNumber) | ||
if err != nil { | ||
return nil, fmt.Errorf("fetching all chunks: %w", err) | ||
} | ||
|
||
return dataStore, nil | ||
} | ||
|
||
// getNumberOfChunks fetches and returns the number of chunks for the website. | ||
func getNumberOfChunks(client *node.Client, websiteAddress string) (int32, error) { | ||
nbChunkResponse, err := node.FetchDatastoreEntry(client, websiteAddress, convert.ToBytes(nbChunkKey)) | ||
if err != nil { | ||
return 0, fmt.Errorf("fetching website number of chunks: %w", err) | ||
} | ||
|
||
chunkNumber, err := convert.BytesToI32(nbChunkResponse.FinalValue) | ||
if err != nil { | ||
return 0, fmt.Errorf("converting fetched data for key '%s': %w", nbChunkKey, err) | ||
} | ||
|
||
return chunkNumber, nil | ||
} | ||
|
||
// fetchAllChunks retrieves all chunks of data for the website. | ||
func fetchAllChunks(client *node.Client, websiteAddress string, chunkNumber int32) ([]byte, error) { | ||
keys := make([][]byte, chunkNumber) | ||
for i := 0; i < int(chunkNumber); i++ { | ||
keys[i] = convert.I32ToBytes(i) | ||
} | ||
|
||
response, err := node.ContractDatastoreEntries(client, websiteAddress, keys) | ||
if err != nil { | ||
return nil, fmt.Errorf("calling get_datastore_entries '%+v': %w", keys, err) | ||
} | ||
|
||
if len(response) != int(chunkNumber) { | ||
return nil, fmt.Errorf("expected %d entries, got %d", chunkNumber, len(response)) | ||
} | ||
|
||
var dataStore []byte | ||
for _, entry := range response { | ||
dataStore = append(dataStore, entry.FinalValue...) | ||
} | ||
|
||
return dataStore, nil | ||
} | ||
|
||
// GetOwner retrieves the owner of the website. | ||
func GetOwner(network *config.NetworkInfos, websiteAddress string) (string, error) { | ||
client := node.NewClient(network.NodeURL) | ||
|
||
ownerResponse, err := node.FetchDatastoreEntry(client, websiteAddress, convert.ToBytes(ownerKey)) | ||
if err != nil { | ||
return "", fmt.Errorf("fetching website owner: %w", err) | ||
} | ||
|
||
return string(ownerResponse.FinalValue), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters