Initial commit

This commit is contained in:
Jonas Kaninda
2024-10-23 02:36:04 +02:00
commit 4a4a7a23ab
12 changed files with 737 additions and 0 deletions

108
pkg/local/local.go Normal file
View File

@@ -0,0 +1,108 @@
package local
import (
"github.com/jkaninda/go-storage/pkg"
"io"
"os"
"path/filepath"
"time"
)
type localStorage struct {
*pkg.Backend
}
type Config struct {
LocalPath string
RemotePath string
}
// NewStorage creates new Storage
func NewStorage(conf Config) pkg.Storage {
return &localStorage{
Backend: &pkg.Backend{
LocalPath: conf.LocalPath,
RemotePath: conf.RemotePath,
},
}
}
// Copy copies file to the local destination path
func (l localStorage) Copy(file string) error {
if _, err := os.Stat(filepath.Join(l.LocalPath, file)); os.IsNotExist(err) {
return err
}
err := copyFile(filepath.Join(l.LocalPath, file), filepath.Join(l.RemotePath, file))
if err != nil {
return err
}
return nil
}
// CopyFrom copies file from a Path to local path
func (l localStorage) CopyFrom(file string) error {
if _, err := os.Stat(filepath.Join(l.RemotePath, file)); os.IsNotExist(err) {
return err
}
err := copyFile(filepath.Join(l.RemotePath, file), filepath.Join(l.LocalPath, file))
if err != nil {
return err
}
return nil
}
// Prune deletes old backup created more than specified days
func (l localStorage) Prune(retentionDays int) error {
currentTime := time.Now()
// Delete file
deleteFile := func(filePath string) error {
err := os.Remove(filePath)
return err
}
// Walk through the directory and delete files modified more than specified days ago
err := filepath.Walk(l.RemotePath, func(filePath string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
// Check if it's a regular file and if it was modified more than specified days ago
if fileInfo.Mode().IsRegular() {
timeDiff := currentTime.Sub(fileInfo.ModTime())
if timeDiff.Hours() > 24*float64(retentionDays) {
err := deleteFile(filePath)
if err != nil {
return err
}
}
}
return nil
})
if err != nil {
return err
}
return nil
}
// Name returns the storage name
func (l localStorage) Name() string {
return "local"
}
// copyFile copies file
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
_, err = io.Copy(out, in)
if err != nil {
out.Close()
return err
}
return out.Close()
}

60
pkg/local/local_test.go Normal file
View File

@@ -0,0 +1,60 @@
package local
import (
"fmt"
"os"
"path/filepath"
"testing"
)
const content = "Lorem ipsum dolor sit amet. Eum eius voluptas sit vitae vitae aut sequi molestias hic accusamus consequatur"
const inputFile = "file.txt"
const localPath = "./tests/local"
const RemotePath = "./tests/remote"
func TestCopy(t *testing.T) {
err := os.MkdirAll(localPath, 0777)
if err != nil {
t.Error(err)
}
err = os.MkdirAll(RemotePath, 0777)
if err != nil {
t.Error(err)
}
_, err = createFile(filepath.Join(localPath, inputFile), content)
if err != nil {
t.Error(err)
}
l := NewStorage(Config{
LocalPath: "./tests/local",
RemotePath: "./tests/remote",
})
err = l.Copy(inputFile)
if err != nil {
t.Error(err)
}
fmt.Printf("File copied to %s\n", filepath.Join(RemotePath, inputFile))
}
func createFile(fileName, content string) ([]byte, error) {
// Create a file named hello.txt
file, err := os.Create(fileName)
if err != nil {
fmt.Println("Error creating file:", err)
return nil, err
}
defer file.Close()
// Write the message to the file
_, err = file.WriteString(content)
if err != nil {
fmt.Println("Error writing to file:", err)
return nil, err
}
fmt.Printf("Successfully wrote to %s\n", fileName)
fileBytes, err := os.ReadFile(fileName)
return fileBytes, err
}