1
0
mirror of https://github.com/lus/pasty.git synced 2023-08-10 21:13:09 +03:00
pasty/internal/storage/file/file_driver.go

146 lines
3.2 KiB
Go
Raw Normal View History

2021-04-15 22:03:26 +03:00
package file
2020-08-23 01:06:29 +03:00
import (
"encoding/base64"
2020-08-23 01:06:29 +03:00
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
2020-09-19 02:56:50 +03:00
"time"
2021-04-15 20:26:17 +03:00
2021-04-15 21:15:42 +03:00
"github.com/lus/pasty/internal/config"
"github.com/lus/pasty/internal/shared"
2020-08-23 01:06:29 +03:00
)
// FileDriver represents the file storage driver
type FileDriver struct {
2020-08-24 20:18:05 +03:00
filePath string
2020-08-23 01:06:29 +03:00
}
// Initialize initializes the file storage driver
func (driver *FileDriver) Initialize() error {
2021-04-15 21:15:42 +03:00
driver.filePath = config.Current.File.Path
2020-08-24 20:18:05 +03:00
return os.MkdirAll(driver.filePath, os.ModePerm)
2020-08-23 01:06:29 +03:00
}
// Terminate terminates the file storage driver (does nothing, because the file storage driver does not need any termination)
func (driver *FileDriver) Terminate() error {
return nil
}
// ListIDs returns a list of all existing paste IDs
func (driver *FileDriver) ListIDs() ([]string, error) {
// Define the IDs slice
var ids []string
// Fill the IDs slice
err := filepath.Walk(driver.filePath, func(path string, info os.FileInfo, err error) error {
// Check if a walking error occurred
if err != nil {
return err
}
2021-04-18 23:05:48 +03:00
// Only count JSON files
if !strings.HasSuffix(info.Name(), ".json") {
return nil
}
// Decode the file name
decoded, err := base64.StdEncoding.DecodeString(strings.TrimSuffix(info.Name(), ".json"))
if err != nil {
return err
}
// Append the ID to the IDs slice
ids = append(ids, string(decoded))
return nil
})
if err != nil {
return nil, err
}
// Return the IDs slice
return ids, nil
}
2020-08-24 00:44:47 +03:00
// Get loads a paste
2021-04-15 21:15:42 +03:00
func (driver *FileDriver) Get(id string) (*shared.Paste, error) {
2020-08-23 01:06:29 +03:00
// Read the file
id = base64.StdEncoding.EncodeToString([]byte(id))
data, err := ioutil.ReadFile(filepath.Join(driver.filePath, id+".json"))
2020-08-23 01:06:29 +03:00
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
2020-08-23 01:06:29 +03:00
return nil, err
}
// Unmarshal the file into a paste
2021-04-15 21:15:42 +03:00
paste := new(shared.Paste)
2020-08-23 01:06:29 +03:00
err = json.Unmarshal(data, &paste)
if err != nil {
return nil, err
}
return paste, nil
}
2020-08-24 00:44:47 +03:00
// Save saves a paste
2021-04-15 21:15:42 +03:00
func (driver *FileDriver) Save(paste *shared.Paste) error {
2020-08-23 01:06:29 +03:00
// Marshal the paste
jsonBytes, err := json.Marshal(paste)
if err != nil {
return err
}
// Create the file to save the paste to
id := base64.StdEncoding.EncodeToString([]byte(paste.ID))
file, err := os.Create(filepath.Join(driver.filePath, id+".json"))
2020-08-23 01:06:29 +03:00
if err != nil {
return err
}
defer file.Close()
// Write the JSON data into the file
_, err = file.Write(jsonBytes)
return err
}
// Delete deletes a paste
func (driver *FileDriver) Delete(id string) error {
id = base64.StdEncoding.EncodeToString([]byte(id))
return os.Remove(filepath.Join(driver.filePath, id+".json"))
2020-08-23 01:06:29 +03:00
}
2020-09-19 02:56:50 +03:00
// Cleanup cleans up the expired pastes
func (driver *FileDriver) Cleanup() (int, error) {
// Retrieve all paste IDs
ids, err := driver.ListIDs()
if err != nil {
return 0, err
}
// Define the amount of deleted items
deleted := 0
// Loop through all pastes
for _, id := range ids {
// Retrieve the paste object
paste, err := driver.Get(id)
if err != nil {
2021-04-15 21:15:42 +03:00
return deleted, err
2020-09-19 02:56:50 +03:00
}
// Delete the paste if it is expired
2021-04-15 21:15:42 +03:00
lifetime := config.Current.AutoDelete.Lifetime
2020-09-19 02:56:50 +03:00
if paste.AutoDelete && paste.Created+int64(lifetime.Seconds()) < time.Now().Unix() {
err = driver.Delete(id)
if err != nil {
2021-04-15 21:15:42 +03:00
return deleted, err
2020-09-19 02:56:50 +03:00
}
deleted++
}
}
return deleted, nil
}