* feat: add import command and batch import functionality - Implemented the `/import` command to allow users to import files from storage to Telegram. - Added support for listing files in storage and filtering based on regex patterns. - Created a batch import task to handle multiple file uploads concurrently. - Introduced progress tracking for batch imports, providing real-time updates to users. - Enhanced storage interfaces to support file listing and reading capabilities. - Updated localization files for the new import command and its usage instructions. - Added utility functions for file size formatting and speed calculation. - Refactored Telegram storage handling to support reading from non-seekable streams. * feat: add i18n for import command * feat: implement ListFiles and OpenFile methods for WebDAV and Alist storage * Update common/i18n/locale/zh-Hans.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update core/tasks/batchimport/progress.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update core/tasks/batchimport/execute.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update storage/alist/alist.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update common/i18n/locale/en.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update pkg/storagetypes/fileinfo.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update common/i18n/locale/en.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update core/tasks/batchimport/execute.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update storage/webdav/webdav.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update storage/telegram/telegram.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update core/tasks/batchimport/execute.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update storage/webdav/webdav.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: missing progress stats i18n * refactor: use strutil to parse args * chore: update generated code files for consistency --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
storcfg "github.com/krau/SaveAny-Bot/config/storage"
|
|
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
|
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
|
|
"github.com/krau/SaveAny-Bot/storage/alist"
|
|
"github.com/krau/SaveAny-Bot/storage/local"
|
|
"github.com/krau/SaveAny-Bot/storage/minio"
|
|
"github.com/krau/SaveAny-Bot/storage/s3"
|
|
"github.com/krau/SaveAny-Bot/storage/telegram"
|
|
"github.com/krau/SaveAny-Bot/storage/webdav"
|
|
)
|
|
|
|
type Storage interface {
|
|
// Init 只应该在创建存储时调用一次
|
|
Init(ctx context.Context, cfg storcfg.StorageConfig) error
|
|
Type() storenum.StorageType
|
|
Name() string
|
|
JoinStoragePath(p string) string
|
|
Save(ctx context.Context, reader io.Reader, storagePath string) error
|
|
Exists(ctx context.Context, storagePath string) bool
|
|
}
|
|
|
|
type StorageCannotStream interface {
|
|
Storage
|
|
CannotStream() string
|
|
}
|
|
|
|
// StorageListable 表示支持列举目录内容的存储
|
|
type StorageListable interface {
|
|
Storage
|
|
ListFiles(ctx context.Context, dirPath string) ([]storagetypes.FileInfo, error)
|
|
}
|
|
|
|
// StorageReadable 表示支持读取文件内容的存储
|
|
type StorageReadable interface {
|
|
Storage
|
|
OpenFile(ctx context.Context, filePath string) (io.ReadCloser, int64, error)
|
|
}
|
|
|
|
var Storages = make(map[string]Storage)
|
|
|
|
type StorageConstructor func() Storage
|
|
|
|
var storageConstructors = map[storenum.StorageType]StorageConstructor{
|
|
storenum.Alist: func() Storage { return new(alist.Alist) },
|
|
storenum.Local: func() Storage { return new(local.Local) },
|
|
storenum.Webdav: func() Storage { return new(webdav.Webdav) },
|
|
storenum.Minio: func() Storage { return new(minio.Minio) },
|
|
storenum.S3: func() Storage { return new(s3.S3) },
|
|
storenum.Telegram: func() Storage { return new(telegram.Telegram) },
|
|
}
|
|
|
|
// NewStorage creates a new storage instance based on the provided config and initializes it
|
|
func NewStorage(ctx context.Context, cfg storcfg.StorageConfig) (Storage, error) {
|
|
constructor, ok := storageConstructors[cfg.GetType()]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unsupported storage type: %s", cfg.GetType())
|
|
}
|
|
|
|
storage := constructor()
|
|
if err := storage.Init(ctx, cfg); err != nil {
|
|
return nil, fmt.Errorf("failed to initialize storage %s: %w", cfg.GetName(), err)
|
|
}
|
|
|
|
return storage, nil
|
|
}
|