summaryrefslogtreecommitdiff
path: root/storage/storage.go
diff options
context:
space:
mode:
authorJan Wolff <janw@mailbox.org>2025-09-12 09:34:01 +0200
committerJan Wolff <janw@mailbox.org>2025-09-12 09:34:01 +0200
commit283c31564a9d5dab4b8b71d7498886b0cd20a999 (patch)
treebc15867a389f25b1855042b5af20efba96c68dbb /storage/storage.go
initial commit
Diffstat (limited to 'storage/storage.go')
-rw-r--r--storage/storage.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/storage/storage.go b/storage/storage.go
new file mode 100644
index 0000000..6dc4a37
--- /dev/null
+++ b/storage/storage.go
@@ -0,0 +1,57 @@
+package storage
+
+import (
+ "crypto/rand"
+ "errors"
+ "math/big"
+ "strings"
+)
+
+var ErrNoSuchFile = errors.New("no such file")
+
+type Storage struct {
+ files map[string]File
+}
+
+func NewStorage() *Storage {
+ return &Storage{
+ files: make(map[string]File),
+ }
+}
+
+func generateKey(length uint) (string, error) {
+ key := make([]string, 0, 0)
+ alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghkmnpqrstuvwxyz"
+
+ for i := uint(0); i < length; i++ {
+ n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphabet))))
+
+ if err != nil {
+ return "", err
+ }
+
+ key = append(key, string(alphabet[n.Int64()]))
+ }
+
+ return strings.Join(key, ""), nil
+}
+
+func (s *Storage) Get(key string) (*File, error) {
+ if file, ok := s.files[key]; ok {
+ return &file, nil
+ } else {
+ return nil, ErrNoSuchFile
+ }
+}
+
+func (s *Storage) Put(file File) (string, error) {
+ key, err := generateKey(8)
+
+ if err != nil {
+ return "", err
+ }
+
+ s.files[key] = file
+
+ return key, nil
+}