summaryrefslogtreecommitdiff
path: root/storage
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
initial commit
Diffstat (limited to 'storage')
-rw-r--r--storage/file.go21
-rw-r--r--storage/storage.go57
2 files changed, 78 insertions, 0 deletions
diff --git a/storage/file.go b/storage/file.go
new file mode 100644
index 0000000..6fa5070
--- /dev/null
+++ b/storage/file.go
@@ -0,0 +1,21 @@
+package storage
+
+import (
+ "bytes"
+ "time"
+)
+
+type File struct {
+ Protected bool
+ Filename string
+ Data []byte
+ AvailableUntil time.Time
+}
+
+func (f File) IsAvailable() bool {
+ return time.Now().Before(f.AvailableUntil)
+}
+
+func (f File) Reader() *bytes.Reader {
+ return bytes.NewReader(f.Data)
+}
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
+}