Skip to content

Commit bf05888

Browse files
committed
feat(s3): Making browsing and uploading of file working
Signed-off-by: Vincent Boutour <[email protected]>
1 parent 364073d commit bf05888

File tree

5 files changed

+64
-21
lines changed

5 files changed

+64
-21
lines changed

pkg/crud/upload.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"mime/multipart"
99
"net/http"
1010
"path"
11+
"time"
1112

1213
"github.com/ViBiOh/fibr/pkg/provider"
14+
"github.com/ViBiOh/httputils/v4/pkg/logger"
1315
"github.com/ViBiOh/httputils/v4/pkg/model"
1416
"github.com/ViBiOh/httputils/v4/pkg/renderer"
1517
)
@@ -56,12 +58,17 @@ func (a App) saveUploadedFile(request provider.Request, part *multipart.Part) (f
5658
return "", fmt.Errorf("error while copying file: %s", err)
5759
}
5860

59-
info, err := a.storageApp.Info(filePath)
60-
if err != nil {
61-
return "", err
62-
}
61+
go func() {
62+
// Waiting 5 seconds before sending hooks
63+
time.Sleep(time.Second * 5)
6364

64-
go a.notify(provider.NewUploadEvent(info))
65+
info, err := a.storageApp.Info(filePath)
66+
if err != nil {
67+
logger.Error("unable to get info for upload event: %s", err)
68+
}
69+
70+
a.notify(provider.NewUploadEvent(info))
71+
}()
6572

6673
return filename, nil
6774
}

pkg/provider/request.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ type Request struct {
3131

3232
// GetFilepath of request
3333
func (r Request) GetFilepath(name string) string {
34-
return GetPathname(r.Path, name, r.Share)
34+
pathname := GetPathname(r.Path, name, r.Share)
35+
36+
if len(name) == 0 && strings.HasSuffix(r.Path, "/") {
37+
pathname += "/"
38+
}
39+
40+
return pathname
3541
}
3642

3743
// URL of request

pkg/provider/request_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ func TestGetFilepath(t *testing.T) {
1919
"",
2020
"/index",
2121
},
22+
{
23+
"directory",
24+
Request{
25+
Path: "www/",
26+
},
27+
"",
28+
"/www/",
29+
},
30+
{
31+
"directory file",
32+
Request{
33+
Path: "www/",
34+
},
35+
"index.html",
36+
"/www/index.html",
37+
},
2238
{
2339
"with given path",
2440
Request{

pkg/s3/s3.go

+26-13
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"flag"
66
"fmt"
77
"io"
8+
"path"
89
"sort"
910
"time"
1011

1112
"github.com/ViBiOh/fibr/pkg/provider"
1213
"github.com/ViBiOh/httputils/v4/pkg/flags"
14+
"github.com/ViBiOh/httputils/v4/pkg/logger"
1315

1416
"github.com/minio/minio-go/v7"
1517
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -77,7 +79,8 @@ func (a App) WithIgnoreFn(ignoreFn func(provider.StorageItem) bool) provider.Sto
7779
// Info provide metadata about given pathname
7880
func (a App) Info(pathname string) (provider.StorageItem, error) {
7981
realPathname := getPath(pathname)
80-
if len(realPathname) == 0 {
82+
83+
if realPathname == "/" {
8184
return provider.StorageItem{
8285
Name: "/",
8386
Pathname: "/",
@@ -90,18 +93,28 @@ func (a App) Info(pathname string) (provider.StorageItem, error) {
9093
return provider.StorageItem{}, convertError(err)
9194
}
9295

93-
return convertToItem(pathname, info), nil
96+
return convertToItem(realPathname, info), nil
9497
}
9598

9699
// List items in the storage
97100
func (a App) List(pathname string) ([]provider.StorageItem, error) {
101+
realPathname := getPath(pathname)
102+
103+
if realPathname == "/" {
104+
realPathname = ""
105+
}
106+
98107
objectsCh := a.client.ListObjects(context.Background(), a.bucket, minio.ListObjectsOptions{
99-
Prefix: getPath(pathname),
108+
Prefix: realPathname,
100109
})
101110

102111
var items []provider.StorageItem
103112
for object := range objectsCh {
104-
item := convertToItem(pathname, object)
113+
item := convertToItem(realPathname, object)
114+
if item.IsDir && item.Name == path.Base(realPathname) {
115+
continue
116+
}
117+
105118
if a.ignoreFn != nil && a.ignoreFn(item) {
106119
continue
107120
}
@@ -118,13 +131,15 @@ func (a App) List(pathname string) ([]provider.StorageItem, error) {
118131
func (a App) WriterTo(pathname string) (io.WriteCloser, error) {
119132
reader, writer := io.Pipe()
120133

121-
if _, err := a.client.PutObject(context.Background(), a.bucket, getPath(pathname), reader, -1, minio.PutObjectOptions{}); err != nil {
122-
if closeErr := writer.Close(); closeErr != nil {
123-
err = fmt.Errorf("%s: %w", err, closeErr)
124-
}
134+
go func() {
135+
if _, err := a.client.PutObject(context.Background(), a.bucket, getPath(pathname), reader, -1, minio.PutObjectOptions{}); err != nil {
136+
if closeErr := writer.Close(); closeErr != nil {
137+
err = fmt.Errorf("%s: %w", err, closeErr)
138+
}
125139

126-
return nil, convertError(err)
127-
}
140+
logger.Error("unable to put object: %s", err)
141+
}
142+
}()
128143

129144
return writer, nil
130145
}
@@ -183,7 +198,5 @@ func (a App) Rename(oldName, newName string) error {
183198

184199
// Remove file or directory from storage
185200
func (a App) Remove(pathname string) error {
186-
// TODO
187-
188-
return nil
201+
return convertError(a.client.RemoveObject(context.Background(), a.bucket, getPath(pathname), minio.RemoveObjectOptions{}))
189202
}

pkg/s3/utils.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package s3
22

33
import (
4+
"path"
45
"strings"
56

67
"github.com/ViBiOh/fibr/pkg/provider"
@@ -13,8 +14,8 @@ func getPath(pathname string) string {
1314

1415
func convertToItem(pathname string, info minio.ObjectInfo) provider.StorageItem {
1516
return provider.StorageItem{
16-
Name: info.Key,
17-
Pathname: pathname,
17+
Name: path.Base(info.Key),
18+
Pathname: info.Key,
1819
IsDir: strings.HasSuffix(info.Key, "/"),
1920
Date: info.LastModified,
2021
Size: info.Size,

0 commit comments

Comments
 (0)