2020-08-18 09:53:45 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
2020-09-29 14:35:13 +05:30
|
|
|
"os"
|
2020-08-18 09:53:45 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrURLNotSupported represents url is not supported
|
|
|
|
ErrURLNotSupported = errors.New("url method not supported")
|
2020-09-29 14:35:13 +05:30
|
|
|
// ErrIterateObjectsNotSupported represents IterateObjects not supported
|
|
|
|
ErrIterateObjectsNotSupported = errors.New("iterateObjects method not supported")
|
2020-08-18 09:53:45 +05:30
|
|
|
)
|
|
|
|
|
2020-10-13 09:28:34 +05:30
|
|
|
// ErrInvalidConfiguration is called when there is invalid configuration for a storage
|
|
|
|
type ErrInvalidConfiguration struct {
|
|
|
|
cfg interface{}
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrInvalidConfiguration) Error() string {
|
|
|
|
if err.err != nil {
|
|
|
|
return fmt.Sprintf("Invalid Configuration Argument: %v: Error: %v", err.cfg, err.err)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Invalid Configuration Argument: %v", err.cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrInvalidConfiguration checks if an error is an ErrInvalidConfiguration
|
|
|
|
func IsErrInvalidConfiguration(err error) bool {
|
|
|
|
_, ok := err.(ErrInvalidConfiguration)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type is a type of Storage
|
|
|
|
type Type string
|
|
|
|
|
|
|
|
// NewStorageFunc is a function that creates a storage
|
|
|
|
type NewStorageFunc func(ctx context.Context, cfg interface{}) (ObjectStorage, error)
|
|
|
|
|
|
|
|
var storageMap = map[Type]NewStorageFunc{}
|
|
|
|
|
|
|
|
// RegisterStorageType registers a provided storage type with a function to create it
|
|
|
|
func RegisterStorageType(typ Type, fn func(ctx context.Context, cfg interface{}) (ObjectStorage, error)) {
|
|
|
|
storageMap[typ] = fn
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:15:10 +05:30
|
|
|
// Object represents the object on the storage
|
|
|
|
type Object interface {
|
|
|
|
io.ReadCloser
|
|
|
|
io.Seeker
|
2020-09-29 14:35:13 +05:30
|
|
|
Stat() (os.FileInfo, error)
|
2020-09-08 21:15:10 +05:30
|
|
|
}
|
|
|
|
|
2020-08-18 09:53:45 +05:30
|
|
|
// ObjectStorage represents an object storage to handle a bucket and files
|
|
|
|
type ObjectStorage interface {
|
2020-09-08 21:15:10 +05:30
|
|
|
Open(path string) (Object, error)
|
2020-08-18 09:53:45 +05:30
|
|
|
Save(path string, r io.Reader) (int64, error)
|
2020-09-29 14:35:13 +05:30
|
|
|
Stat(path string) (os.FileInfo, error)
|
2020-08-18 09:53:45 +05:30
|
|
|
Delete(path string) error
|
|
|
|
URL(path, name string) (*url.URL, error)
|
2020-09-29 14:35:13 +05:30
|
|
|
IterateObjects(func(path string, obj Object) error) error
|
2020-08-18 09:53:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copys a file from source ObjectStorage to dest ObjectStorage
|
|
|
|
func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, srcPath string) (int64, error) {
|
|
|
|
f, err := srcStorage.Open(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return dstStorage.Save(dstPath, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Attachments represents attachments storage
|
|
|
|
Attachments ObjectStorage
|
2020-09-08 21:15:10 +05:30
|
|
|
|
|
|
|
// LFS represents lfs storage
|
|
|
|
LFS ObjectStorage
|
2020-08-18 09:53:45 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Init init the stoarge
|
|
|
|
func Init() error {
|
2020-09-08 21:15:10 +05:30
|
|
|
if err := initAttachments(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return initLFS()
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:28:34 +05:30
|
|
|
// NewStorage takes a storage type and some config and returns an ObjectStorage or an error
|
|
|
|
func NewStorage(typStr string, cfg interface{}) (ObjectStorage, error) {
|
|
|
|
if len(typStr) == 0 {
|
|
|
|
typStr = string(LocalStorageType)
|
2020-08-18 09:53:45 +05:30
|
|
|
}
|
2020-10-13 09:28:34 +05:30
|
|
|
fn, ok := storageMap[Type(typStr)]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Unsupported storage type: %s", typStr)
|
2020-08-18 09:53:45 +05:30
|
|
|
}
|
|
|
|
|
2020-10-13 09:28:34 +05:30
|
|
|
return fn(context.Background(), cfg)
|
2020-08-18 09:53:45 +05:30
|
|
|
}
|
2020-09-08 21:15:10 +05:30
|
|
|
|
2020-09-29 14:35:13 +05:30
|
|
|
func initAttachments() (err error) {
|
2020-10-13 09:28:34 +05:30
|
|
|
Attachments, err = NewStorage(setting.Attachment.Storage.Type, setting.Attachment.Storage)
|
2020-09-29 14:35:13 +05:30
|
|
|
return
|
|
|
|
}
|
2020-09-08 21:15:10 +05:30
|
|
|
|
2020-09-29 14:35:13 +05:30
|
|
|
func initLFS() (err error) {
|
2020-10-13 09:28:34 +05:30
|
|
|
LFS, err = NewStorage(setting.LFS.Storage.Type, setting.LFS.Storage)
|
2020-09-29 14:35:13 +05:30
|
|
|
return
|
2020-09-08 21:15:10 +05:30
|
|
|
}
|