mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-22 00:32:26 +08:00
fix: preserve webdav error causes
Wrap mkdir and write failures with %w and drop dead error values.
This commit is contained in:
@@ -1,9 +0,0 @@
|
|||||||
package webdav
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrFailedToCreateDirectory = errors.New("webdav: failed to create directory")
|
|
||||||
ErrFailedToWriteFile = errors.New("webdav: failed to write file")
|
|
||||||
ErrFailedToCheckFileExists = errors.New("webdav: failed to check if file exists")
|
|
||||||
)
|
|
||||||
@@ -11,11 +11,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
||||||
config "github.com/krau/SaveAny-Bot/config/storage"
|
config "github.com/krau/SaveAny-Bot/config/storage"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
||||||
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
||||||
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
|
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
|
||||||
"github.com/rs/xid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Webdav struct {
|
type Webdav struct {
|
||||||
@@ -54,28 +54,18 @@ func (w *Webdav) JoinStoragePath(p string) string {
|
|||||||
|
|
||||||
func (w *Webdav) Save(ctx context.Context, r io.Reader, storagePath string) error {
|
func (w *Webdav) Save(ctx context.Context, r io.Reader, storagePath string) error {
|
||||||
w.logger.Infof("Saving file to %s", storagePath)
|
w.logger.Infof("Saving file to %s", storagePath)
|
||||||
storagePath = w.JoinStoragePath(storagePath)
|
candidate := w.JoinStoragePath(storagePath)
|
||||||
ext := path.Ext(storagePath)
|
|
||||||
base := strings.TrimSuffix(storagePath, ext)
|
|
||||||
candidate := storagePath
|
|
||||||
if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
|
if overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool); !overwrite {
|
||||||
for i := 1; w.existsPath(ctx, candidate); i++ {
|
candidate = fsutil.UniquePath(w.config.BasePath, storagePath, func(c string) bool {
|
||||||
candidate = fmt.Sprintf("%s_%d%s", base, i, ext)
|
return w.existsPath(ctx, c)
|
||||||
if i > 1000 {
|
}, 1000)
|
||||||
w.logger.Errorf("Too many attempts to find a unique filename for %s", storagePath)
|
|
||||||
candidate = fmt.Sprintf("%s_%s%s", base, xid.New().String(), ext)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := w.client.MkDir(ctx, path.Dir(candidate)); err != nil {
|
if err := w.client.MkDir(ctx, path.Dir(candidate)); err != nil {
|
||||||
w.logger.Errorf("Failed to create directory %s: %v", path.Dir(candidate), err)
|
return fmt.Errorf("failed to create directory: %w", err)
|
||||||
return ErrFailedToCreateDirectory
|
|
||||||
}
|
}
|
||||||
if err := w.client.WriteFile(ctx, candidate, r); err != nil {
|
if err := w.client.WriteFile(ctx, candidate, r); err != nil {
|
||||||
w.logger.Errorf("Failed to write file %s: %v", candidate, err)
|
return fmt.Errorf("failed to write file: %w", err)
|
||||||
return ErrFailedToWriteFile
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -136,9 +126,6 @@ func (w *Webdav) ListFiles(ctx context.Context, dirPath string) ([]storagetypes.
|
|||||||
|
|
||||||
isDir := resp.Propstat.Prop.ResourceType.IsCollection()
|
isDir := resp.Propstat.Prop.ResourceType.IsCollection()
|
||||||
|
|
||||||
filePath := strings.TrimPrefix(decodedHref, path.Join("/", strings.Trim(path.Dir(fullPath), "/")))
|
|
||||||
filePath = strings.TrimPrefix(filePath, "/")
|
|
||||||
|
|
||||||
fileInfo := storagetypes.FileInfo{
|
fileInfo := storagetypes.FileInfo{
|
||||||
Name: name,
|
Name: name,
|
||||||
Path: path.Join(dirPath, name),
|
Path: path.Join(dirPath, name),
|
||||||
|
|||||||
Reference in New Issue
Block a user