aboutsummaryrefslogtreecommitdiff
path: root/s3_path.go
diff options
context:
space:
mode:
authorQuentin <quentin@deuxfleurs.fr>2021-08-23 20:40:03 +0200
committerQuentin <quentin@deuxfleurs.fr>2021-08-23 20:40:03 +0200
commit15e4d10fd4b35a2e70cc4fa6ad4117cd5c402cbc (patch)
tree0fe31ac79d5dbe480b1b386845b3dd8fcaa55bd4 /s3_path.go
parent5d64be33e64eb75160b6fe37b01997de7e96237a (diff)
downloadbagage-15e4d10fd4b35a2e70cc4fa6ad4117cd5c402cbc.tar.gz
bagage-15e4d10fd4b35a2e70cc4fa6ad4117cd5c402cbc.zip
Refactor the codebase
Diffstat (limited to 's3_path.go')
-rw-r--r--s3_path.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/s3_path.go b/s3_path.go
new file mode 100644
index 0000000..e1933ef
--- /dev/null
+++ b/s3_path.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+ "path"
+ "strings"
+
+ "github.com/minio/minio-go/v7"
+)
+
+type S3Class int
+
+const (
+ ROOT S3Class = 1 << iota
+ BUCKET
+ COMMON_PREFIX
+ OBJECT
+ OPAQUE_KEY
+
+ KEY = COMMON_PREFIX | OBJECT | OPAQUE_KEY
+)
+
+type S3Path struct {
+ path string
+ class S3Class
+ bucket string
+ key string
+}
+
+func NewS3Path(path string) S3Path {
+ exploded_path := strings.SplitN(path, "/", 3)
+
+ // If there is no bucket name (eg. "/")
+ if len(exploded_path) < 2 || exploded_path[1] == "" {
+ return S3Path{path, ROOT, "", ""}
+ }
+
+ // If there is no key
+ if len(exploded_path) < 3 || exploded_path[2] == "" {
+ return S3Path{path, BUCKET, exploded_path[1], ""}
+ }
+
+ return S3Path{path, OPAQUE_KEY, exploded_path[1], exploded_path[2]}
+}
+
+func NewTrustedS3Path(bucket string, obj minio.ObjectInfo) S3Path {
+ cl := OBJECT
+ if obj.Key[len(obj.Key)-1:] == "/" {
+ cl = COMMON_PREFIX
+ }
+
+ return S3Path{
+ path: path.Join("/", bucket, obj.Key),
+ bucket: bucket,
+ key: obj.Key,
+ class: cl,
+ }
+}