From 19822368045d0b98e4a1b293e00b97f5168743ce Mon Sep 17 00:00:00 2001 From: Tri Vo Date: Tue, 20 Jun 2023 12:18:56 +0700 Subject: [PATCH] Improve upload for subdirectory --- internal/app/s3manager/bucket_view.go | 2 + internal/app/s3manager/create_object.go | 4 +- web/template/bucket.html.tmpl | 120 +++++++++++++++++++++--- 3 files changed, 112 insertions(+), 14 deletions(-) diff --git a/internal/app/s3manager/bucket_view.go b/internal/app/s3manager/bucket_view.go index 4450231..021160d 100644 --- a/internal/app/s3manager/bucket_view.go +++ b/internal/app/s3manager/bucket_view.go @@ -30,6 +30,7 @@ func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bo Objects []objectWithIcon AllowDelete bool Paths []string + CurrentPath string } return func(w http.ResponseWriter, r *http.Request) { @@ -68,6 +69,7 @@ func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bo Objects: objs, AllowDelete: allowDelete, Paths: removeEmptyStrings(strings.Split(path, "/")), + CurrentPath: path, } t, err := template.ParseFS(templates, "layout.html.tmpl", "bucket.html.tmpl") diff --git a/internal/app/s3manager/create_object.go b/internal/app/s3manager/create_object.go index 85af919..aa6c20d 100644 --- a/internal/app/s3manager/create_object.go +++ b/internal/app/s3manager/create_object.go @@ -22,6 +22,7 @@ func HandleCreateObject(s3 S3, sseInfo SSEType) http.HandlerFunc { return } file, header, err := r.FormFile("file") + path := r.FormValue("path") if err != nil { handleHTTPError(w, fmt.Errorf("error getting file from form: %w", err)) return @@ -50,7 +51,8 @@ func HandleCreateObject(s3 S3, sseInfo SSEType) http.HandlerFunc { } } - _, err = s3.PutObject(r.Context(), bucketName, header.Filename, file, -1, opts) + objectName := fmt.Sprintf("%s%s", path, header.Filename) + _, err = s3.PutObject(r.Context(), bucketName, objectName, file, -1, opts) if err != nil { handleHTTPError(w, fmt.Errorf("error putting object: %w", err)) return diff --git a/web/template/bucket.html.tmpl b/web/template/bucket.html.tmpl index 318d649..d6d75bd 100644 --- a/web/template/bucket.html.tmpl +++ b/web/template/bucket.html.tmpl @@ -1,7 +1,13 @@ {{ define "content" }} @@ -32,7 +38,7 @@ -
+
{{ if .Objects }} @@ -59,16 +65,18 @@ {{ end }} @@ -77,15 +85,30 @@ {{ end }} {{ if not .Objects }} -

No objects in {{ .BucketName }} yet

+

No objects in {{ .BucketName }}/{{ .CurrentPath }} yet

{{ end }} +
+ +
+ +
@@ -117,6 +141,30 @@ + + {{ end }}
{{ $object.Owner }} {{ $object.LastModified }} - - - + {{ if not $object.IsFolder }} + + + + {{ end }}