From 5a9a5a88852ae132abe8646535ebe3c0c2fc26d0 Mon Sep 17 00:00:00 2001 From: Lukas SP Date: Wed, 26 Aug 2020 21:59:22 +0200 Subject: [PATCH] Implement ID listing method in storage driver implementations --- internal/storage/driver.go | 1 + internal/storage/file_driver.go | 31 ++++++++++++++++++++++++++++++ internal/storage/mongodb_driver.go | 30 +++++++++++++++++++++++++++++ internal/storage/s3_driver.go | 19 ++++++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/internal/storage/driver.go b/internal/storage/driver.go index c64fe72..c2b59a1 100644 --- a/internal/storage/driver.go +++ b/internal/storage/driver.go @@ -14,6 +14,7 @@ var Current Driver type Driver interface { Initialize() error Terminate() error + ListIDs() ([]string, error) Get(id string) (*pastes.Paste, error) Save(paste *pastes.Paste) error Delete(id string) error diff --git a/internal/storage/file_driver.go b/internal/storage/file_driver.go index 04ac682..ca5f01a 100644 --- a/internal/storage/file_driver.go +++ b/internal/storage/file_driver.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" ) // FileDriver represents the file storage driver @@ -26,6 +27,36 @@ 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 + } + + // 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 func (driver *FileDriver) Get(id string) (*pastes.Paste, error) { // Read the file diff --git a/internal/storage/mongodb_driver.go b/internal/storage/mongodb_driver.go index d5a8ba9..7aaa36b 100644 --- a/internal/storage/mongodb_driver.go +++ b/internal/storage/mongodb_driver.go @@ -48,6 +48,36 @@ func (driver *MongoDBDriver) Terminate() error { 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 func (driver *MongoDBDriver) Get(id string) (*pastes.Paste, error) { // Define the collection to use for this database operation diff --git a/internal/storage/s3_driver.go b/internal/storage/s3_driver.go index ddfcbe6..8925b0f 100644 --- a/internal/storage/s3_driver.go +++ b/internal/storage/s3_driver.go @@ -9,6 +9,7 @@ import ( "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "io/ioutil" + "strings" ) // S3Driver represents the AWS S3 storage driver @@ -37,6 +38,24 @@ func (driver *S3Driver) Terminate() error { 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 func (driver *S3Driver) Get(id string) (*pastes.Paste, error) { // Read the object