store, config: Added option to disable space.
the build was successful

This commit is contained in:
2019-01-12 12:13:24 +01:00
parent e0b93c6b4b
commit 2eab263d8a
3 changed files with 18 additions and 3 deletions
+1
View File
@@ -14,6 +14,7 @@ var global *Config
// Config is configuration // Config is configuration
type Config struct { type Config struct {
Space struct { Space struct {
Enabled bool `json:"enabled"`
Host string `json:"host"` Host string `json:"host"`
AccessKey string `json:"accessKey"` AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"` SecretKey string `json:"secretKey"`
+5 -3
View File
@@ -26,9 +26,11 @@ func Init() error {
} }
sconf := conf.Space sconf := conf.Space
err = ConnectSpace(sconf.Host, sconf.AccessKey, sconf.SecretKey, sconf.Bucket, sconf.MaxSize, sconf.Root) if sconf.Enabled {
if err != nil { err = ConnectSpace(sconf.Host, sconf.AccessKey, sconf.SecretKey, sconf.Bucket, sconf.MaxSize, sconf.Root)
return err if err != nil {
return err
}
} }
hasInitialized = true hasInitialized = true
+12
View File
@@ -43,6 +43,10 @@ func ConnectSpace(host, accessKey, secretKey, bucket string, maxSize int64, root
// UploadFile uploads the file to the space. This does not do any checks on it, so the endpoints should // UploadFile uploads the file to the space. This does not do any checks on it, so the endpoints should
// ensure that's all okay. // ensure that's all okay.
func UploadFile(ctx context.Context, folder string, name string, mimeType string, reader io.Reader, size int64) (string, error) { func UploadFile(ctx context.Context, folder string, name string, mimeType string, reader io.Reader, size int64) (string, error) {
if spaceClient == nil {
return "", errors.New("This functionality is not enabled")
}
path := folder + "/" + name path := folder + "/" + name
if size > spaceMaxSize { if size > spaceMaxSize {
@@ -69,6 +73,10 @@ func UploadFile(ctx context.Context, folder string, name string, mimeType string
// RemoveFile removes a file from the space // RemoveFile removes a file from the space
func RemoveFile(folder string, name string) error { func RemoveFile(folder string, name string) error {
if spaceClient == nil {
return errors.New("This functionality is not enabled")
}
path := folder + "/" + name path := folder + "/" + name
return spaceClient.RemoveObject(spaceBucket, spaceRoot+"/"+path) return spaceClient.RemoveObject(spaceBucket, spaceRoot+"/"+path)
@@ -76,6 +84,10 @@ func RemoveFile(folder string, name string) error {
// DownloadFile opens a file for download, using the same path format as the UploadFile function. Remember to Close it! // DownloadFile opens a file for download, using the same path format as the UploadFile function. Remember to Close it!
func DownloadFile(ctx context.Context, path string) (io.ReadCloser, error) { func DownloadFile(ctx context.Context, path string) (io.ReadCloser, error) {
if spaceClient == nil {
return nil, errors.New("This functionality is not enabled")
}
return spaceClient.GetObjectWithContext(ctx, spaceBucket, spaceRoot+"/"+path, minio.GetObjectOptions{}) return spaceClient.GetObjectWithContext(ctx, spaceBucket, spaceRoot+"/"+path, minio.GetObjectOptions{})
} }