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

Implement ID listing method in storage driver implementations

This commit is contained in:
Lukas SP 2020-08-26 21:59:22 +02:00
parent f63b94f8c1
commit 5a9a5a8885
4 changed files with 81 additions and 0 deletions

View File

@ -14,6 +14,7 @@ var Current Driver
type Driver interface { type Driver interface {
Initialize() error Initialize() error
Terminate() error Terminate() error
ListIDs() ([]string, error)
Get(id string) (*pastes.Paste, error) Get(id string) (*pastes.Paste, error)
Save(paste *pastes.Paste) error Save(paste *pastes.Paste) error
Delete(id string) error Delete(id string) error

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
// FileDriver represents the file storage driver // FileDriver represents the file storage driver
@ -26,6 +27,36 @@ func (driver *FileDriver) Terminate() error {
return nil 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
}
// 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
}
// Get loads a paste // Get loads a paste
func (driver *FileDriver) Get(id string) (*pastes.Paste, error) { func (driver *FileDriver) Get(id string) (*pastes.Paste, error) {
// Read the file // Read the file

View File

@ -48,6 +48,36 @@ func (driver *MongoDBDriver) Terminate() error {
return driver.client.Disconnect(context.TODO()) return driver.client.Disconnect(context.TODO())
} }
// ListIDs returns a list of all existing paste IDs
func (driver *MongoDBDriver) ListIDs() ([]string, error) {
// Define the collection to use for this database operation
collection := driver.client.Database(driver.database).Collection(driver.collection)
// Define the context for the following database operation
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Retrieve all paste documents
result, err := collection.Find(ctx, bson.M{})
if err != nil {
return nil, err
}
// Decode all paste documents
var pasteSlice []pastes.Paste
err = result.All(ctx, &pasteSlice)
if err != nil {
return nil, err
}
// Read and return the IDs of all paste objects
var ids []string
for _, paste := range pasteSlice {
ids = append(ids, paste.ID)
}
return ids, nil
}
// Get loads a paste // Get loads a paste
func (driver *MongoDBDriver) Get(id string) (*pastes.Paste, error) { func (driver *MongoDBDriver) Get(id string) (*pastes.Paste, error) {
// Define the collection to use for this database operation // Define the collection to use for this database operation

View File

@ -9,6 +9,7 @@ import (
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
"io/ioutil" "io/ioutil"
"strings"
) )
// S3Driver represents the AWS S3 storage driver // S3Driver represents the AWS S3 storage driver
@ -37,6 +38,24 @@ func (driver *S3Driver) Terminate() error {
return nil return nil
} }
// ListIDs returns a list of all existing paste IDs
func (driver *S3Driver) ListIDs() ([]string, error) {
// Define the IDs slice
var ids []string
// Fill the IDs slice
channel := driver.client.ListObjects(context.Background(), driver.bucket, minio.ListObjectsOptions{})
for object := range channel {
if object.Err != nil {
return nil, object.Err
}
ids = append(ids, strings.TrimSuffix(object.Key, ".json"))
}
// Return the IDs slice
return ids, nil
}
// Get loads a paste // Get loads a paste
func (driver *S3Driver) Get(id string) (*pastes.Paste, error) { func (driver *S3Driver) Get(id string) (*pastes.Paste, error) {
// Read the object // Read the object