mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore] cleanup storage implementation, no need for multiple interface types (#1131)
Signed-off-by: kim <grufwub@gmail.com> Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
@@ -33,15 +33,15 @@ import (
|
||||
)
|
||||
|
||||
// NewInMemoryStorage returns a new in memory storage with the default test config
|
||||
func NewInMemoryStorage() *gtsstorage.Local {
|
||||
storage, err := kv.OpenStorage(storage.OpenMemory(200, false))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
func NewInMemoryStorage() *gtsstorage.Driver {
|
||||
storage := storage.OpenMemory(200, false)
|
||||
return >sstorage.Driver{
|
||||
KVStore: kv.New(storage),
|
||||
Storage: storage,
|
||||
}
|
||||
return >sstorage.Local{KVStore: storage}
|
||||
}
|
||||
|
||||
func NewS3Storage() gtsstorage.Driver {
|
||||
func NewS3Storage() *gtsstorage.Driver {
|
||||
endpoint := config.GetStorageS3Endpoint()
|
||||
access := config.GetStorageS3AccessKey()
|
||||
secret := config.GetStorageS3SecretKey()
|
||||
@@ -65,16 +65,16 @@ func NewS3Storage() gtsstorage.Driver {
|
||||
panic(fmt.Errorf("error opening s3 storage: %w", err))
|
||||
}
|
||||
|
||||
return >sstorage.S3{
|
||||
return >sstorage.Driver{
|
||||
KVStore: kv.New(s3),
|
||||
Storage: s3,
|
||||
Proxy: proxy,
|
||||
Bucket: bucket,
|
||||
Storage: s3,
|
||||
KVStore: kv.New(s3),
|
||||
}
|
||||
}
|
||||
|
||||
// StandardStorageSetup populates the storage with standard test entries from the given directory.
|
||||
func StandardStorageSetup(s gtsstorage.Driver, relativePath string) {
|
||||
func StandardStorageSetup(storage *gtsstorage.Driver, relativePath string) {
|
||||
storedA := newTestStoredAttachments()
|
||||
a := NewTestAttachments()
|
||||
for k, paths := range storedA {
|
||||
@@ -90,14 +90,14 @@ func StandardStorageSetup(s gtsstorage.Driver, relativePath string) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := s.Put(context.TODO(), pathOriginal, bOriginal); err != nil {
|
||||
if err := storage.Put(context.TODO(), pathOriginal, bOriginal); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bSmall, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameSmall))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := s.Put(context.TODO(), pathSmall, bSmall); err != nil {
|
||||
if err := storage.Put(context.TODO(), pathSmall, bSmall); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -117,14 +117,14 @@ func StandardStorageSetup(s gtsstorage.Driver, relativePath string) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := s.Put(context.TODO(), pathOriginal, bOriginal); err != nil {
|
||||
if err := storage.Put(context.TODO(), pathOriginal, bOriginal); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bStatic, err := os.ReadFile(fmt.Sprintf("%s/%s", relativePath, filenameStatic))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := s.Put(context.TODO(), pathStatic, bStatic); err != nil {
|
||||
if err := storage.Put(context.TODO(), pathStatic, bStatic); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -133,24 +133,27 @@ func StandardStorageSetup(s gtsstorage.Driver, relativePath string) {
|
||||
// StandardStorageTeardown deletes everything in storage so that it's clean for
|
||||
// the next test
|
||||
// nolint:gocritic // complains about the type switch, but it's the cleanest solution
|
||||
func StandardStorageTeardown(s gtsstorage.Driver) {
|
||||
func StandardStorageTeardown(storage *gtsstorage.Driver) {
|
||||
defer os.RemoveAll(path.Join(os.TempDir(), "gotosocial"))
|
||||
|
||||
switch st := s.(type) {
|
||||
case *gtsstorage.Local:
|
||||
iter, err := st.KVStore.Iterator(context.Background(), nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
keys := []string{}
|
||||
for iter.Next() {
|
||||
keys = append(keys, iter.Key())
|
||||
}
|
||||
iter.Release()
|
||||
for _, k := range keys {
|
||||
if err := s.Delete(context.TODO(), k); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
// Open a storage iterator
|
||||
iter, err := storage.Iterator(context.Background(), nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var keys []string
|
||||
|
||||
for iter.Next() {
|
||||
// Collate all of the storage keys
|
||||
keys = append(keys, iter.Key())
|
||||
}
|
||||
|
||||
// Done with iter
|
||||
iter.Release()
|
||||
|
||||
for _, key := range keys {
|
||||
// Ignore errors, we just want to attempt delete all
|
||||
_ = storage.Delete(context.Background(), key)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user