mirror of https://github.com/Xhofe/alist
feat: check status before storage call
parent
849de88e68
commit
8fd56ef9dd
|
@ -9,6 +9,7 @@ type Config struct {
|
|||
NoUpload bool `json:"no_upload"`
|
||||
NeedMs bool `json:"need_ms"` // if need get message from user, such as validate code
|
||||
DefaultRoot string `json:"default_root"`
|
||||
CheckStatus bool
|
||||
}
|
||||
|
||||
func (c Config) MustProxy() bool {
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
package op
|
||||
|
||||
var WORK = "work"
|
|
@ -29,6 +29,9 @@ func ClearCache(storage driver.Driver, path string) {
|
|||
|
||||
// List files in storage, not contains virtual file
|
||||
func List(ctx context.Context, storage driver.Driver, path string, args model.ListArgs, refresh ...bool) ([]model.Obj, error) {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return nil, errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
path = utils.StandardizePath(path)
|
||||
log.Debugf("op.List %s", path)
|
||||
dir, err := Get(ctx, storage, path)
|
||||
|
@ -129,6 +132,9 @@ var linkG singleflight.Group[*model.Link]
|
|||
|
||||
// Link get link, if is an url. should have an expiry time
|
||||
func Link(ctx context.Context, storage driver.Driver, path string, args model.LinkArgs) (*model.Link, model.Obj, error) {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return nil, nil, errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
file, err := Get(ctx, storage, path)
|
||||
if err != nil {
|
||||
return nil, nil, errors.WithMessage(err, "failed to get file")
|
||||
|
@ -168,6 +174,9 @@ func Other(ctx context.Context, storage driver.Driver, args model.FsOtherArgs) (
|
|||
}
|
||||
|
||||
func MakeDir(ctx context.Context, storage driver.Driver, path string) error {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
// check if dir exists
|
||||
f, err := Get(ctx, storage, path)
|
||||
if err != nil {
|
||||
|
@ -198,6 +207,9 @@ func MakeDir(ctx context.Context, storage driver.Driver, path string) error {
|
|||
}
|
||||
|
||||
func Move(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string) error {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
srcObj, err := Get(ctx, storage, srcPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed to get src object")
|
||||
|
@ -210,6 +222,9 @@ func Move(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string
|
|||
}
|
||||
|
||||
func Rename(ctx context.Context, storage driver.Driver, srcPath, dstName string) error {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
srcObj, err := Get(ctx, storage, srcPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed to get src object")
|
||||
|
@ -219,6 +234,9 @@ func Rename(ctx context.Context, storage driver.Driver, srcPath, dstName string)
|
|||
|
||||
// Copy Just copy file[s] in a storage
|
||||
func Copy(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string) error {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
srcObj, err := Get(ctx, storage, srcPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed to get src object")
|
||||
|
@ -228,6 +246,9 @@ func Copy(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string
|
|||
}
|
||||
|
||||
func Remove(ctx context.Context, storage driver.Driver, path string) error {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
obj, err := Get(ctx, storage, path)
|
||||
if err != nil {
|
||||
// if object not found, it's ok
|
||||
|
@ -240,6 +261,9 @@ func Remove(ctx context.Context, storage driver.Driver, path string) error {
|
|||
}
|
||||
|
||||
func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file model.FileStreamer, up driver.UpdateProgress) error {
|
||||
if storage.Config().CheckStatus && storage.GetStorage().Status != "ok" {
|
||||
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
|
||||
}
|
||||
defer func() {
|
||||
if f, ok := file.GetReadCloser().(*os.File); ok {
|
||||
err := os.RemoveAll(f.Name())
|
||||
|
|
|
@ -55,7 +55,7 @@ func CreateStorage(ctx context.Context, storage model.Storage) error {
|
|||
MustSaveDriverStorage(storageDriver)
|
||||
return errors.Wrapf(err, "failed init storage but storage is already created")
|
||||
} else {
|
||||
storageDriver.GetStorage().SetStatus("work")
|
||||
storageDriver.GetStorage().SetStatus(WORK)
|
||||
MustSaveDriverStorage(storageDriver)
|
||||
}
|
||||
log.Debugf("storage %+v is created", storageDriver)
|
||||
|
@ -75,7 +75,12 @@ func LoadStorage(ctx context.Context, storage model.Storage) error {
|
|||
err = storageDriver.Init(ctx, storage)
|
||||
storagesMap.Store(storage.MountPath, storageDriver)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed init storage but storage is already created")
|
||||
storageDriver.GetStorage().SetStatus(fmt.Sprintf("%+v", err.Error()))
|
||||
MustSaveDriverStorage(storageDriver)
|
||||
return errors.Wrapf(err, "failed init storage")
|
||||
} else {
|
||||
storageDriver.GetStorage().SetStatus(WORK)
|
||||
MustSaveDriverStorage(storageDriver)
|
||||
}
|
||||
log.Debugf("storage %+v is created", storageDriver)
|
||||
return nil
|
||||
|
@ -165,10 +170,15 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error {
|
|||
return errors.Wrapf(err, "failed drop storage")
|
||||
}
|
||||
err = storageDriver.Init(ctx, storage)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed init storage")
|
||||
}
|
||||
storagesMap.Store(storage.MountPath, storageDriver)
|
||||
if err != nil {
|
||||
storageDriver.GetStorage().SetStatus(fmt.Sprintf("%+v", err.Error()))
|
||||
MustSaveDriverStorage(storageDriver)
|
||||
return errors.Wrapf(err, "failed init storage")
|
||||
} else {
|
||||
storageDriver.GetStorage().SetStatus(WORK)
|
||||
MustSaveDriverStorage(storageDriver)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue