-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage.go
135 lines (111 loc) · 4.27 KB
/
storage.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2023 sigma
//
// 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 storage
import (
"context"
"fmt"
"io"
"path"
"strings"
"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/types/enums"
)
const (
// MultipartCopyThresholdSize ...
MultipartCopyThresholdSize = 32 << 20 // 32MB
// MultipartCopyChunkSize ...
MultipartCopyChunkSize = 32 << 20 // 32MB
// MultipartCopyMaxConcurrency ...
MultipartCopyMaxConcurrency = 100 // 100 goroutines
// MaxPaginationKeys ...
MaxPaginationKeys = 1000 // 1000 keys
)
//go:generate mockgen -destination=mocks/storage_driver.go -package=mocks github.com/go-sigma/sigma/pkg/storage StorageDriver
//go:generate mockgen -destination=mocks/storage_driver_factory.go -package=mocks github.com/go-sigma/sigma/pkg/storage StorageDriverFactory
// StorageDriver is the interface for the storage driver
type StorageDriver interface {
// Move moves an object stored at sourcePath to destPath, removing the
// original object.
// Note: This may be no more efficient than a copy followed by a delete for
// many implementations.
Move(ctx context.Context, sourcePath string, destPath string) error
// Delete recursively deletes all objects stored at "path" and its subpaths.
Delete(ctx context.Context, path string) error
// Reader retrieves an io.ReadCloser for the content stored at "path"
// with a given byte offset.
Reader(ctx context.Context, path string) (io.ReadCloser, error)
// CreateUploadID creates a new multipart upload and returns an
// opaque upload ID.
CreateUploadID(ctx context.Context, path string) (string, error)
// WritePart writes a part of a multipart upload.
UploadPart(ctx context.Context, path, uploadID string, partNumber int64, body io.Reader) (string, error)
// CommitUpload commits a multipart upload.
CommitUpload(ctx context.Context, path string, uploadID string, parts []string) error
// AbortUpload aborts a multipart upload.
AbortUpload(ctx context.Context, path string, uploadID string) error
// Upload upload a file to the given path.
Upload(ctx context.Context, path string, body io.Reader) error
// Redirect get a temporary link
Redirect(ctx context.Context, path string) (string, error)
}
// Factory is the interface for the storage driver factory
type Factory interface {
New(config configs.Configuration) (StorageDriver, error)
}
var driverFactories = make(map[enums.StorageType]Factory)
// RegisterDriverFactory registers a storage factory driver by name.
// If RegisterDriverFactory is called twice with the same name or if driver is nil, it panics.
func RegisterDriverFactory(name enums.StorageType, factory Factory) error {
if _, ok := driverFactories[name]; ok {
return fmt.Errorf("driver %q already registered", name)
}
driverFactories[name] = factory
return nil
}
// Driver is the storage driver
var Driver StorageDriver
// StorageDriverFactory ...
type StorageDriverFactory interface {
// New new storage driver
New() StorageDriver
}
type storageDriverFactory struct{}
// NewStorageDriverFactory ...
func NewStorageDriverFactory() StorageDriverFactory {
return &storageDriverFactory{}
}
// New new storage driver
func (s *storageDriverFactory) New() StorageDriver {
return Driver
}
// Initialize initializes the storage driver
func Initialize(config configs.Configuration) error {
factory, ok := driverFactories[config.Storage.Type]
if !ok {
return fmt.Errorf("driver %q not registered", config.Storage.Type)
}
var err error
Driver, err = factory.New(config)
if err != nil {
return err
}
return nil
}
// SanitizePath ...
func SanitizePath(rootDirectory, p string) string {
if rootDirectory == "" || rootDirectory == "." || rootDirectory == "./" || rootDirectory == "/" {
return p
}
return path.Join(strings.TrimPrefix(rootDirectory, "./"), strings.TrimPrefix(p, "./"))
}