-
Notifications
You must be signed in to change notification settings - Fork 31
/
object.go
118 lines (105 loc) · 2.88 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2021 Harshavardhana
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"os"
"strings"
"time"
minio "github.com/minio/minio-go/v7"
)
const (
pathSeparator = "/"
)
// A httpMinioObject implements http.File interface, returned by a S3
// Open method and can be served by the FileServer implementation.
type httpMinioObject struct {
client *minio.Client
object *minio.Object
bucket string
prefix string
isDir bool
}
func (h *httpMinioObject) Close() error {
return h.object.Close()
}
func (h *httpMinioObject) Read(p []byte) (n int, err error) {
return h.object.Read(p)
}
func (h *httpMinioObject) Seek(offset int64, whence int) (int64, error) {
return h.object.Seek(offset, whence)
}
func (h *httpMinioObject) Readdir(count int) ([]os.FileInfo, error) {
// List 'N' number of objects from a bucket-name with a matching prefix.
listObjectsN := func(bucket, prefix string, count int) (objsInfo []minio.ObjectInfo, err error) {
i := 1
for object := range h.client.ListObjects(context.Background(), bucket, minio.ListObjectsOptions{
Prefix: prefix,
Recursive: false,
}) {
if object.Err != nil {
return nil, object.Err
}
i++
// Verify if we have printed N objects.
if i == count {
return
}
objsInfo = append(objsInfo, object)
}
return objsInfo, nil
}
// List non-recursively first count entries for prefix 'prefix" prefix.
objsInfo, err := listObjectsN(h.bucket, h.prefix, count)
if err != nil {
return nil, os.ErrNotExist
}
var fileInfos []os.FileInfo
for _, objInfo := range objsInfo {
if strings.HasSuffix(objInfo.Key, pathSeparator) {
fileInfos = append(fileInfos, objectInfo{
ObjectInfo: minio.ObjectInfo{
Key: strings.TrimSuffix(objInfo.Key, pathSeparator),
LastModified: objInfo.LastModified,
},
prefix: strings.TrimSuffix(objInfo.Key, pathSeparator),
isDir: true,
})
continue
}
fileInfos = append(fileInfos, objectInfo{
ObjectInfo: objInfo,
})
}
return fileInfos, nil
}
func (h *httpMinioObject) Stat() (os.FileInfo, error) {
if h.isDir {
return objectInfo{
ObjectInfo: minio.ObjectInfo{
Key: h.prefix,
LastModified: time.Now().UTC(),
},
prefix: h.prefix,
isDir: true,
}, nil
}
objInfo, err := h.object.Stat()
if err != nil {
return nil, os.ErrNotExist
}
return objectInfo{
ObjectInfo: objInfo,
}, nil
}