gosimplenpm/storage/fs.go

188 lines
4.2 KiB
Go

package storage
import (
"archive/tar"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path"
"strings"
"gosimplenpm/serviceidos"
"path/filepath"
"github.com/sirupsen/logrus"
)
func GetIndexJsonFromStore(packageName string, registryPath string, log *logrus.Logger) (string, bool, error) {
fileToServe := ""
found := false
err := filepath.WalkDir(registryPath, func(fp string, info fs.DirEntry, e error) error {
if strings.Contains(fp, path.Join(packageName, "index.json")) {
fileToServe = fp
found = true
}
return e
})
if err != nil {
log.WithFields(logrus.Fields{
"function": "get-index-json-from-store",
}).Debugf("List files error: +%v\n", err)
return fileToServe, found, err
}
if fileToServe == "" && !found {
fileToServe = path.Join(registryPath, packageName, "index.json")
}
return fileToServe, found, nil
}
func GetTarFromStore(packageName string, tarFileName string, registryPath string, log *logrus.Logger) (string, error) {
fileToServe := ""
err := filepath.WalkDir(registryPath, func(fp string, info fs.DirEntry, e error) error {
if strings.Contains(fp, path.Join(packageName, tarFileName)) {
fileToServe = fp
}
return e
})
if err != nil {
log.WithFields(logrus.Fields{
"function": "get-tar-from-store",
}).Debugf("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 {
log.WithFields(logrus.Fields{
"function": "get-tar-from-store",
}).Debugf("Open error: %s\n", fileToServe)
return "", err
}
archive, err := gzip.NewReader(file)
if err != nil {
log.WithFields(logrus.Fields{
"function": "get-tar-from-store",
}).Debugf("Archive Open error: %s\n", fileToServe)
return "", err
}
tr := tar.NewReader(archive)
bs, err := io.ReadAll(tr)
if err != nil {
log.WithFields(logrus.Fields{
"function": "get-tar-from-store",
}).Debugf("Archive Read error: %s\n", fileToServe)
return "", err
}
return base64.StdEncoding.EncodeToString(bs), err
}
func ReadIndexJson(fPath string, res *serviceidos.IndexJson, log *logrus.Logger) error {
jsonFile, err := os.Open(fPath)
if err != nil {
log.WithFields(logrus.Fields{
"function": "read-index-json",
}).Debugf("File Not found: %s\n", fPath)
return err
}
defer jsonFile.Close()
err = json.NewDecoder(jsonFile).Decode(res)
if err != nil {
log.WithFields(logrus.Fields{
"function": "read-index-json",
}).Debugf("Unmarshalerror: %+v\n", err)
return err
}
return nil
}
func WriteIndexJson(fPath string, res *serviceidos.IndexJson, log *logrus.Logger) error {
// Need to create the directory first
parent := path.Dir(fPath)
err := os.MkdirAll(parent, os.ModePerm)
if err != nil {
log.WithFields(logrus.Fields{
"function": "write-index-json",
}).Debugf("Folder (%s) creation failed.\n", fPath)
return err
}
// Create the file
jsonFile, err := os.Create(fPath)
if err != nil {
log.WithFields(logrus.Fields{
"function": "write-index-json",
}).Debugf("Creation error for path(%s): %+v\n ", fPath, err)
return err
}
defer jsonFile.Close()
err = json.NewEncoder(jsonFile).Encode(res)
if err != nil {
log.WithFields(logrus.Fields{
"function": "write-index-json",
}).Debugf("Marshalerror: %+v\n", err)
return err
}
return nil
}
func WritePackageToStore(fPath string, data string, log *logrus.Logger) error {
dec, err := base64.StdEncoding.DecodeString(data)
if err != nil {
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
}).Debugf("Base64 Decode error: %+v\n", err)
return err
}
dataFile, err := os.Create(fPath)
if err != nil {
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
}).Debugf("Creation error: %s\n", fPath)
return err
}
defer dataFile.Close()
_, err = dataFile.Write(dec)
if err != nil {
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
}).Debugf("Write error: %s\n", fPath)
return err
}
err = dataFile.Sync()
if err != nil {
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
}).Debugf("Sync error: %s\n", fPath)
return err
}
return nil
}