gosimplenpm/internal/storage/fs.go

181 lines
4.1 KiB
Go
Raw Permalink Normal View History

package storage
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path"
"strings"
2023-11-25 02:17:09 +00:00
"gosimplenpm/internal/serviceidos"
"path/filepath"
2023-06-30 19:06:46 +00:00
"github.com/sirupsen/logrus"
)
2023-12-25 08:56:48 +00:00
type FSStorage struct{}
func (f *FSStorage) GetIndexJsonFromStore(packageName string, registryPath string, log *logrus.Logger) (string, bool, error) {
fileToServe := ""
found := false
2023-06-30 19:06:46 +00:00
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 {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "get-index-json-from-store",
2023-12-25 08:56:48 +00:00
}).Errorf("List files error: +%v\n", err)
return fileToServe, found, err
}
if fileToServe == "" && !found {
2023-06-30 19:06:46 +00:00
fileToServe = path.Join(registryPath, packageName, "index.json")
}
return fileToServe, found, nil
}
2023-12-25 08:56:48 +00:00
func (f *FSStorage) GetTarFromStore(packageName string, tarFileName string, registryPath string, log *logrus.Logger) (string, error) {
fileToServe := ""
2023-06-30 19:06:46 +00:00
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 {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "get-tar-from-store",
2023-12-25 08:56:48 +00:00
}).Errorf("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 {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "get-tar-from-store",
2023-12-25 08:56:48 +00:00
}).Errorf("Open error: %s\n", fileToServe)
return "", err
}
2023-12-25 08:56:48 +00:00
defer file.Close()
2023-12-25 08:56:48 +00:00
bs, err := io.ReadAll(file)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "get-tar-from-store",
2023-12-25 08:56:48 +00:00
}).Errorf("File Read error: %s\n", fileToServe)
return "", err
}
return base64.StdEncoding.EncodeToString(bs), err
}
2023-12-25 08:56:48 +00:00
func (f *FSStorage) ReadIndexJson(fPath string, res *serviceidos.IndexJson, log *logrus.Logger) error {
jsonFile, err := os.Open(fPath)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "read-index-json",
2023-12-25 08:56:48 +00:00
}).Errorf("File Not found: %s\n", fPath)
return err
}
defer jsonFile.Close()
err = json.NewDecoder(jsonFile).Decode(res)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "read-index-json",
2023-12-25 08:56:48 +00:00
}).Errorf("Unmarshalerror: %+v\n", err)
return err
}
return nil
}
2023-12-25 08:56:48 +00:00
func (f *FSStorage) 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 {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "write-index-json",
2023-12-25 08:56:48 +00:00
}).Errorf("Folder (%s) creation failed.\n", fPath)
return err
}
// Create the file
jsonFile, err := os.Create(fPath)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "write-index-json",
2023-12-25 08:56:48 +00:00
}).Errorf("Creation error for path(%s): %+v\n ", fPath, err)
return err
}
defer jsonFile.Close()
err = json.NewEncoder(jsonFile).Encode(res)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "write-index-json",
}).Debugf("Marshalerror: %+v\n", err)
return err
}
return nil
}
2023-12-25 08:56:48 +00:00
func (f *FSStorage) WritePackageToStore(fPath string, data string, log *logrus.Logger) error {
dec, err := base64.StdEncoding.DecodeString(data)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
2023-12-25 08:56:48 +00:00
}).Errorf("Base64 Decode error: %+v\n", err)
return err
}
dataFile, err := os.Create(fPath)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
2023-12-25 08:56:48 +00:00
}).Errorf("Creation error: %s\n", fPath)
return err
}
defer dataFile.Close()
_, err = dataFile.Write(dec)
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
2023-12-25 08:56:48 +00:00
}).Errorf("Write error: %s\n", fPath)
return err
}
err = dataFile.Sync()
if err != nil {
2023-06-30 19:06:46 +00:00
log.WithFields(logrus.Fields{
"function": "write-package-to-store",
2023-12-25 08:56:48 +00:00
}).Errorf("Sync error: %s\n", fPath)
return err
}
return nil
}