Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cache system #55

Merged
merged 11 commits into from
Jul 31, 2024
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3'
tasks:
install:
cmds:
# Empty for now
- cmd: go install ./cmd/cli
pivilartisant marked this conversation as resolved.
Show resolved Hide resolved

generate:
cmds:
Expand Down
25 changes: 7 additions & 18 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,27 @@ func processFileForUpload(filepath string) ([][]byte, error) {
return website.DivideIntoChunks(websiteBytes, website.ChunkSize), nil
}

func viewWebsite(address string, config *pkgConfig.Config) error {
owner, err := website.GetOwner(&config.NetworkInfos, address)
func viewWebsite(scAddress string, config *pkgConfig.Config) error {
owner, err := website.GetOwner(&config.NetworkInfos, scAddress)
if err != nil {
logger.Warnf("failed to get owner of %s: %v", address, err)
logger.Warnf("failed to get owner of %s: %v", scAddress, err)
}

logger.Infof("Website owner: %s", owner)

websiteBytes, err := website.Fetch(&config.NetworkInfos, address)
zipFile, err := website.CheckWebsiteCache(scAddress, config)
if err != nil {
return fmt.Errorf("failed to fetch website: %v", err)
return fmt.Errorf("failed to check website cache: %v", err)
pivilartisant marked this conversation as resolved.
Show resolved Hide resolved
}

logger.Infof("Website fetched successfully with size: %d", len(websiteBytes))

outputZipPath := fmt.Sprintf("website_%s.zip", address)

err = os.WriteFile(outputZipPath, websiteBytes, 0o644)
if err != nil {
return fmt.Errorf("failed to write website zip file %v", err)
}

logger.Infof("Website successfully written to file: %s", outputZipPath)

fileName := "index.html"

content, err := zipper.GetFileFromZip(outputZipPath, fileName)
indexFile, err := zipper.ReadFileFromZip(zipFile, fileName)
if err != nil {
return fmt.Errorf("failed to get file %s from zip: %v", fileName, err)
}

logger.Infof("%s content:\n %s", fileName, content)
logger.Infof("viewing content for %s:\n %s", scAddress, indexFile)

return nil
}
21 changes: 12 additions & 9 deletions int/zipper/zipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,38 @@ package zipper

import (
"archive/zip"
"bytes"
"fmt"
"io"

"github.com/massalabs/station/pkg/logger"
)

// 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)
// ReadIndexFromZip returns the content of the desired file from the given zip file.
func ReadFileFromZip(zipFile []byte, fileName string) ([]byte, error) {
reader := bytes.NewReader(zipFile)

zipReader, err := zip.NewReader(reader, int64(reader.Len()))
if err != nil {
return []byte{}, fmt.Errorf("failed to open zip file: %w", err)
logger.Errorf("failed to initiate reader: %v", err)
pivilartisant marked this conversation as resolved.
Show resolved Hide resolved
}
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)
return nil, 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 nil, fmt.Errorf("failed to read file in zip: %w", err)
}

return buf, nil
}
}

return []byte{}, fmt.Errorf("%s not found in zip", fileName)
return nil, fmt.Errorf("%s not found in zip", fileName)
}
43 changes: 43 additions & 0 deletions pkg/website/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package website

import (
"fmt"

pkgConfig "github.com/massalabs/DeWeb/pkg/config"
"github.com/massalabs/station/pkg/cache"
"github.com/massalabs/station/pkg/logger"
)

func CheckWebsiteCache(scAddress string, config *pkgConfig.Config) ([]byte, error) {
cache := new(cache.Cache)

websiteBytes, err := Fetch(&config.NetworkInfos, scAddress)
pivilartisant marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("failed to fetch website: %v", err)
}

logger.Infof("Website fetched successfully with size: %d", len(websiteBytes))

fileName := fmt.Sprintf("website_%s.zip", scAddress)

isWebsitePresent := cache.IsPresent(fileName)
if !isWebsitePresent {
logger.Infof("Website was not found in cache at: %s", fileName)

err := cache.Save(fileName, websiteBytes)
if err != nil {
return nil, fmt.Errorf("failed to write website zip file %v", err)
}

logger.Infof("Website successfully written in cache at: %s", fileName)
} else {
logger.Infof("Website was found in cache at: %s", fileName)
}

content, err := cache.Read(fileName)
if err != nil {
return nil, fmt.Errorf("failed to read cached website: %v", err)
}

return content, nil
}