2023-06-11 00:32:02 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2023-06-17 04:08:50 +00:00
|
|
|
"archive/tar"
|
|
|
|
"compress/gzip"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2023-06-11 00:32:02 +00:00
|
|
|
"fmt"
|
2023-06-17 04:08:50 +00:00
|
|
|
"io"
|
2023-06-11 00:32:02 +00:00
|
|
|
"io/fs"
|
2023-06-17 04:08:50 +00:00
|
|
|
"os"
|
2023-06-11 00:32:02 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2023-06-17 04:08:50 +00:00
|
|
|
"gosimplenpm/serviceidos"
|
2023-06-11 00:32:02 +00:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2023-06-17 04:08:50 +00:00
|
|
|
func GetRegistryPath() (string, error) {
|
|
|
|
registryPath, err := filepath.Abs("./examples")
|
2023-06-11 00:32:02 +00:00
|
|
|
if err != nil {
|
2023-06-17 04:08:50 +00:00
|
|
|
fmt.Printf("File repo not found: +%v\n", err)
|
2023-06-11 00:32:02 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2023-06-17 04:08:50 +00:00
|
|
|
return registryPath, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetIndexJsonFromStore(packageName string) (string, bool, error) {
|
|
|
|
fileToServe := ""
|
|
|
|
found := false
|
|
|
|
searchDir, err := GetRegistryPath()
|
|
|
|
if err != nil {
|
|
|
|
return searchDir, found, err
|
|
|
|
}
|
2023-06-11 00:32:02 +00:00
|
|
|
|
|
|
|
err = filepath.WalkDir(searchDir, func(fp string, info fs.DirEntry, e error) error {
|
|
|
|
if strings.Contains(fp, path.Join(packageName, "index.json")) {
|
|
|
|
fileToServe = fp
|
2023-06-17 04:08:50 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("List files error: +%v\n", err)
|
|
|
|
return fileToServe, found, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileToServe == "" && !found {
|
|
|
|
fileToServe = path.Join(searchDir, packageName, "index.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileToServe, found, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTarFromStore(packageName string, tarFileName string) (string, error) {
|
|
|
|
fileToServe := ""
|
|
|
|
searchDir, err := GetRegistryPath()
|
|
|
|
if err != nil {
|
|
|
|
return searchDir, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.WalkDir(searchDir, func(fp string, info fs.DirEntry, e error) error {
|
|
|
|
if strings.Contains(fp, path.Join(packageName, tarFileName)) {
|
|
|
|
fileToServe = fp
|
2023-06-11 00:32:02 +00:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2023-06-17 04:08:50 +00:00
|
|
|
fmt.Printf("List files error: +%v\n", err)
|
|
|
|
return fileToServe, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileToServe == "" {
|
|
|
|
return fileToServe, fmt.Errorf("file %s is not found for package %s", tarFileName, packageName)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(fileToServe)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Open error: %s\n", fileToServe)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
archive, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Archive Open error: %s\n", fileToServe)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(archive)
|
|
|
|
bs, err := io.ReadAll(tr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Archive Read error: %s\n", fileToServe)
|
2023-06-11 00:32:02 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2023-06-17 04:08:50 +00:00
|
|
|
return base64.StdEncoding.EncodeToString(bs), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadIndexJson(fPath string, res *serviceidos.IndexJson) error {
|
|
|
|
jsonFile, err := os.Open(fPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("File Not found: %s\n", fPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer jsonFile.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(jsonFile).Decode(res)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Unmarshalerror: %+v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteIndexJson(fPath string, res *serviceidos.IndexJson) error {
|
|
|
|
// Need to create the directory first
|
|
|
|
parent := path.Dir(fPath)
|
|
|
|
err := os.MkdirAll(parent, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Folder (%s) creation failed.\n", fPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
jsonFile, err := os.Create(fPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Creation error for path(%s): %+v\n ", fPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer jsonFile.Close()
|
|
|
|
|
|
|
|
err = json.NewEncoder(jsonFile).Encode(res)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Marshalerror: %+v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WritePackageToStore(fPath string, data string) error {
|
|
|
|
dec, err := base64.StdEncoding.DecodeString(data)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Base64 Decode error: %+v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dataFile, err := os.Create(fPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Creation error: %s\n", fPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer dataFile.Close()
|
|
|
|
|
|
|
|
_, err = dataFile.Write(dec)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Write error: %s\n", fPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dataFile.Sync()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Sync error: %s\n", fPath)
|
|
|
|
return err
|
|
|
|
}
|
2023-06-11 00:32:02 +00:00
|
|
|
|
2023-06-17 04:08:50 +00:00
|
|
|
return nil
|
2023-06-11 00:32:02 +00:00
|
|
|
}
|