mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-07 07:01:21 +08:00
refactor: update storage interface to use io.Reader for Save method and remove stream implementations
This commit is contained in:
@@ -3,6 +3,7 @@ package minio
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"path"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/common"
|
||||
@@ -59,10 +60,10 @@ func (m *Minio) JoinStoragePath(task types.Task) string {
|
||||
return path.Join(m.config.BasePath, task.StoragePath)
|
||||
}
|
||||
|
||||
func (m *Minio) Save(ctx context.Context, localFilePath, storagePath string) error {
|
||||
common.Log.Infof("Saving file %s to %s", localFilePath, storagePath)
|
||||
func (m *Minio) Save(ctx context.Context, r io.Reader, storagePath string) error {
|
||||
common.Log.Infof("Saving file from reader to %s", storagePath)
|
||||
|
||||
_, err := m.client.FPutObject(ctx, m.config.BucketName, storagePath, localFilePath, minio.PutObjectOptions{})
|
||||
_, err := m.client.PutObject(ctx, m.config.BucketName, storagePath, r, -1, minio.PutObjectOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to upload file to minio: %w", err)
|
||||
}
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
package minio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/common"
|
||||
"github.com/minio/minio-go/v7"
|
||||
)
|
||||
|
||||
type MinioWriter struct {
|
||||
pipeWriter *io.PipeWriter
|
||||
done chan error
|
||||
path string
|
||||
ctx context.Context
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (w *MinioWriter) Write(p []byte) (n int, err error) {
|
||||
select {
|
||||
case <-w.ctx.Done():
|
||||
return 0, w.ctx.Err()
|
||||
default:
|
||||
return w.pipeWriter.Write(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *MinioWriter) Close() error {
|
||||
if w.closed {
|
||||
return nil
|
||||
}
|
||||
w.closed = true
|
||||
|
||||
if err := w.pipeWriter.Close(); err != nil {
|
||||
return fmt.Errorf("failed to close pipe writer: %w", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-w.done:
|
||||
if err != nil {
|
||||
return fmt.Errorf("upload failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
case <-w.ctx.Done():
|
||||
return fmt.Errorf("upload cancelled: %w", w.ctx.Err())
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Minio) NewUploadStream(ctx context.Context, storagePath string) (io.WriteCloser, error) {
|
||||
common.Log.Infof("Creating upload stream for %s", storagePath)
|
||||
|
||||
uploadCtx, cancel := context.WithCancel(ctx)
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
done := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
done <- fmt.Errorf("panic during upload: %v", r)
|
||||
}
|
||||
pipeReader.Close()
|
||||
cancel()
|
||||
}()
|
||||
|
||||
info, err := m.client.PutObject(
|
||||
uploadCtx,
|
||||
m.config.BucketName,
|
||||
storagePath,
|
||||
pipeReader,
|
||||
-1,
|
||||
minio.PutObjectOptions{},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
common.Log.Errorf("Failed to upload to %s: %v", storagePath, err)
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
|
||||
common.Log.Infof("uploaded %d bytes to %s", info.Size, storagePath)
|
||||
done <- nil
|
||||
}()
|
||||
|
||||
return &MinioWriter{
|
||||
pipeWriter: pipeWriter,
|
||||
done: done,
|
||||
path: storagePath,
|
||||
ctx: uploadCtx,
|
||||
closed: false,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user