Skip to content

Commit 17574a7

Browse files
committed
Fix consistency issues
1 parent e2dab55 commit 17574a7

File tree

8 files changed

+42
-39
lines changed

8 files changed

+42
-39
lines changed

internal/app/s3manager/bucket_view.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// HandleBucketView shows the details page of a bucket.
17-
func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bool, rootUrl string) http.HandlerFunc {
17+
func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bool, rootURL string) http.HandlerFunc {
1818
type objectWithIcon struct {
1919
Key string
2020
Size int64
@@ -26,7 +26,7 @@ func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bo
2626
}
2727

2828
type pageData struct {
29-
RootUrl string
29+
RootURL string
3030
BucketName string
3131
Objects []objectWithIcon
3232
AllowDelete bool
@@ -41,8 +41,6 @@ func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bo
4141
path := matches[2]
4242

4343
var objs []objectWithIcon
44-
doneCh := make(chan struct{})
45-
defer close(doneCh)
4644
opts := minio.ListObjectsOptions{
4745
Recursive: listRecursive,
4846
Prefix: path,
@@ -66,7 +64,7 @@ func HandleBucketView(s3 S3, templates fs.FS, allowDelete bool, listRecursive bo
6664
objs = append(objs, obj)
6765
}
6866
data := pageData{
69-
RootUrl: rootUrl,
67+
RootURL: rootURL,
7068
BucketName: bucketName,
7169
Objects: objs,
7270
AllowDelete: allowDelete,
@@ -107,11 +105,12 @@ func icon(fileName string) string {
107105
}
108106

109107
func removeEmptyStrings(input []string) []string {
110-
var result []string
108+
result := make([]string, 0, len(input))
111109
for _, str := range input {
112-
if str != "" {
113-
result = append(result, str)
110+
if str == "" {
111+
continue
114112
}
113+
result = append(result, str)
115114
}
116115
return result
117116
}

internal/app/s3manager/buckets_view.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
)
1111

1212
// HandleBucketsView renders all buckets on an HTML page.
13-
func HandleBucketsView(s3 S3, templates fs.FS, allowDelete bool, rootUrl string) http.HandlerFunc {
13+
func HandleBucketsView(s3 S3, templates fs.FS, allowDelete bool, rootURL string) http.HandlerFunc {
1414
type pageData struct {
15-
RootUrl string
15+
RootURL string
1616
Buckets []minio.BucketInfo
1717
AllowDelete bool
1818
}
@@ -26,7 +26,7 @@ func HandleBucketsView(s3 S3, templates fs.FS, allowDelete bool, rootUrl string)
2626
}
2727

2828
data := pageData{
29-
RootUrl: rootUrl,
29+
RootURL: rootURL,
3030
Buckets: buckets,
3131
AllowDelete: allowDelete,
3232
}

internal/app/s3manager/create_object.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ func HandleCreateObject(s3 S3, sseInfo SSEType) http.HandlerFunc {
3535
opts := minio.PutObjectOptions{ContentType: "application/octet-stream"}
3636

3737
if sseInfo.Type == "KMS" {
38-
opts.ServerSideEncryption, _ = encrypt.NewSSEKMS(sseInfo.Key, nil)
38+
opts.ServerSideEncryption, err = encrypt.NewSSEKMS(sseInfo.Key, nil)
39+
if err != nil {
40+
handleHTTPError(w, fmt.Errorf("error setting SSE-KMS key: %w", err))
41+
return
42+
}
3943
}
4044

4145
if sseInfo.Type == "SSE" {

internal/app/s3manager/generate_presigned_url.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func HandleGenerateURL(s3 S3) http.HandlerFunc {
2020

2121
parsedExpiry, err := strconv.ParseInt(expiry, 10, 0)
2222
if err != nil {
23-
handleHTTPError(w, fmt.Errorf("error when converting expiry: %w", err))
23+
handleHTTPError(w, fmt.Errorf("error converting expiry: %w", err))
2424
return
2525
}
2626

@@ -29,11 +29,11 @@ func HandleGenerateURL(s3 S3) http.HandlerFunc {
2929
return
3030
}
3131

32-
expirySecond := time.Duration(parsedExpiry * 1e9)
32+
expiryDuration := time.Duration(parsedExpiry) * time.Second
3333
reqParams := make(url.Values)
34-
url, err := s3.PresignedGetObject(r.Context(), bucketName, objectName, expirySecond, reqParams)
34+
url, err := s3.PresignedGetObject(r.Context(), bucketName, objectName, expiryDuration, reqParams)
3535
if err != nil {
36-
handleHTTPError(w, fmt.Errorf("error when generate url: %w", err))
36+
handleHTTPError(w, fmt.Errorf("error generating url: %w", err))
3737
return
3838
}
3939

main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ func parseConfiguration() configuration {
5858
iamEndpoint = viper.GetString("IAM_ENDPOINT")
5959
} else {
6060
accessKeyID = viper.GetString("ACCESS_KEY_ID")
61-
if len(accessKeyID) == 0 {
61+
if accessKeyID == "" {
6262
log.Fatal("please provide ACCESS_KEY_ID")
6363
}
6464

6565
secretAccessKey = viper.GetString("SECRET_ACCESS_KEY")
66-
if len(secretAccessKey) == 0 {
66+
if secretAccessKey == "" {
6767
log.Fatal("please provide SECRET_ACCESS_KEY")
6868
}
6969
}
@@ -171,18 +171,18 @@ func main() {
171171
if err != nil {
172172
log.Fatalln(fmt.Errorf("error creating s3 client: %w", err))
173173
}
174-
// Check for a root Url to insert into HTML templates in case of reverse proxying
175-
rootUrl, rootSet := os.LookupEnv("ROOT_URL")
176-
if rootSet && !strings.HasPrefix(rootUrl, "/") {
177-
rootUrl = "/" + rootUrl
174+
// Check for a root URL to insert into HTML templates in case of reverse proxying
175+
rootURL, rootSet := os.LookupEnv("ROOT_URL")
176+
if rootSet && !strings.HasPrefix(rootURL, "/") {
177+
rootURL = "/" + rootURL
178178
}
179179

180180
// Set up router
181181
r := mux.NewRouter()
182182
r.Handle("/", http.RedirectHandler("/buckets", http.StatusPermanentRedirect)).Methods(http.MethodGet)
183183
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(statics)))).Methods(http.MethodGet)
184-
r.Handle("/buckets", s3manager.HandleBucketsView(s3, templates, configuration.AllowDelete, rootUrl)).Methods(http.MethodGet)
185-
r.PathPrefix("/buckets/").Handler(s3manager.HandleBucketView(s3, templates, configuration.AllowDelete, configuration.ListRecursive, rootUrl)).Methods(http.MethodGet)
184+
r.Handle("/buckets", s3manager.HandleBucketsView(s3, templates, configuration.AllowDelete, rootURL)).Methods(http.MethodGet)
185+
r.PathPrefix("/buckets/").Handler(s3manager.HandleBucketView(s3, templates, configuration.AllowDelete, configuration.ListRecursive, rootURL)).Methods(http.MethodGet)
186186
r.Handle("/api/buckets", s3manager.HandleCreateBucket(s3)).Methods(http.MethodPost)
187187
if configuration.AllowDelete {
188188
r.Handle("/api/buckets/{bucketName}", s3manager.HandleDeleteBucket(s3)).Methods(http.MethodDelete)

web/template/bucket.html.tmpl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<nav class="nav-extended">
1515
<div class="nav-wrapper container">
16-
<a href="{{$.RootUrl}}/buckets/{{$.BucketName}}" class="brand-logo center"><i class="material-icons">folder_open</i>{{ .BucketName }}</a>
16+
<a href="{{$.RootURL}}/buckets/{{$.BucketName}}" class="brand-logo center"><i class="material-icons">folder_open</i>{{ .BucketName }}</a>
1717
{{ if not .Objects }}
1818
<ul class="right">
1919
<li>
@@ -26,8 +26,8 @@
2626
</div>
2727

2828
<div class="nav-wrapper container">
29-
<a href="{{$.RootUrl}}/buckets" class="breadcrumb"><i class="material-icons">arrow_back</i> buckets </a>
30-
{{ $url := printf "%s/buckets/%s/" $.RootUrl $.BucketName }}
29+
<a href="{{$.RootURL}}/buckets" class="breadcrumb"><i class="material-icons">arrow_back</i> buckets </a>
30+
{{ $url := printf "%s/buckets/%s/" $.RootURL $.BucketName }}
3131
<a href="{{ $url }}" class="breadcrumb">{{ $.BucketName }}</a>
3232

3333
{{ range $index, $path := .Paths }}
@@ -56,7 +56,7 @@
5656
<tr>
5757
<td
5858
{{ if $object.IsFolder }}
59-
onclick="location.href='{{$.RootUrl}}/buckets/{{ $.BucketName }}/{{ $object.Key }}'"}
59+
onclick="location.href='{{$.RootURL}}/buckets/{{ $.BucketName }}/{{ $object.Key }}'"}
6060
style="cursor:pointer;"
6161
{{ end }}>
6262
<i class="material-icons">{{ $object.Icon }}</i> {{ $object.DisplayName }}
@@ -71,7 +71,7 @@
7171
</button>
7272
<!-- Dropdown Structure -->
7373
<ul id="actions-dropdown-{{ $index }}" class="dropdown-content">
74-
<li><a target="_blank" href="{{$.RootUrl}}/api/buckets/{{ $.BucketName }}/objects/{{ $object.Key }}">Download</a></li>
74+
<li><a target="_blank" href="{{$.RootURL}}/api/buckets/{{ $.BucketName }}/objects/{{ $object.Key }}">Download</a></li>
7575
<li><a onclick="handleOpenDownloadLinkModal({{ $object.Key }})">Download link</a></li>
7676
{{- if $.AllowDelete }}
7777
<li><a href="#" onclick="deleteObject({{ $.BucketName }}, {{ $object.Key }})">Delete</a></li>
@@ -192,22 +192,22 @@
192192
function deleteObject(bucketName, objectName) {
193193
$.ajax({
194194
type: 'DELETE',
195-
url: '{{$.RootUrl}}/api/buckets/' + bucketName + '/objects/' + objectName,
195+
url: '{{$.RootURL}}/api/buckets/' + bucketName + '/objects/' + objectName,
196196
success: function () { location.reload(); }
197197
})
198198
}
199199

200200
function deleteBucket(bucketName) {
201201
$.ajax({
202202
type: 'DELETE',
203-
url: '{{$.RootUrl}}/api/buckets/' + bucketName,
203+
url: '{{$.RootURL}}/api/buckets/' + bucketName,
204204
success: function () { window.location.replace('/buckets'); }
205205
})
206206
}
207207

208208
function handleUploadFiles(event) {
209209
files = event.target.files
210-
url = "{{$.RootUrl}}/api/buckets/{{ .BucketName }}/objects"
210+
url = "{{$.RootURL}}/api/buckets/{{ .BucketName }}/objects"
211211
uploadFiles(files, url);
212212
}
213213

@@ -299,7 +299,7 @@ function handleGenerateDownloadLink(event) {
299299

300300
$.ajax({
301301
type: 'GET',
302-
url: '{{$.RootUrl}}/api/buckets/' + {{ $.BucketName }}+ "/objects/" + objectName + "/url?expiry=" + expiryTime,
302+
url: '{{$.RootURL}}/api/buckets/' + {{ $.BucketName }}+ "/objects/" + objectName + "/url?expiry=" + expiryTime,
303303
success: function (result) {
304304
genUrlMessage.innerHTML = "";
305305
document.getElementById("generated-link").setAttribute('value', JSON.parse(result).url);

web/template/buckets.html.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{ define "content" }}
22
<nav>
33
<div class="nav-wrapper container">
4-
<a href="{{$.RootUrl}}" class="brand-logo">S3 Manager</a>
4+
<a href="{{$.RootURL}}" class="brand-logo">S3 Manager</a>
55
</div>
66
</nav>
77

@@ -12,7 +12,7 @@
1212
{{ if .Buckets }}
1313
{{ range $bucket := .Buckets }}
1414
<div class="col l6 m12">
15-
<a href="{{$.RootUrl}}/buckets/{{ $bucket.Name }}" style="color:black;">
15+
<a href="{{$.RootURL}}/buckets/{{ $bucket.Name }}" style="color:black;">
1616
<div class="card">
1717
<div class="card-content">
1818
<div class="row">

web/template/layout.html.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
1010
<meta http-equiv="X-UA-Compatible" content="IE=edge">
1111

12-
<link rel="stylesheet" href="{{$.RootUrl}}/static/css/material-fonts.css" />
13-
<link rel="stylesheet" href="{{$.RootUrl}}/static/css/materialize.min.css" />
12+
<link rel="stylesheet" href="{{$.RootURL}}/static/css/material-fonts.css" />
13+
<link rel="stylesheet" href="{{$.RootURL}}/static/css/materialize.min.css" />
1414
</head>
1515

1616
<body>
1717

1818
{{ template "content" . }}
1919

2020
<script
21-
src="{{$.RootUrl}}/static/js/jquery-3.6.0.min.js"
21+
src="{{$.RootURL}}/static/js/jquery-3.6.0.min.js"
2222
></script>
23-
<script src="{{$.RootUrl}}/static/js/materialize.min.js"></script>
23+
<script src="{{$.RootURL}}/static/js/materialize.min.js"></script>
2424
<script>
2525
$(document).ready(function(){
2626
$('.dropdown-trigger').dropdown();

0 commit comments

Comments
 (0)