fix: upload image issues
This commit is contained in:
@@ -2,7 +2,10 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
@@ -41,32 +44,35 @@ func (s *common) Options(ctx context.Context) (*common_dto.OptionsResponse, erro
|
||||
func (s *common) Upload(ctx context.Context, userID int64, file *multipart.FileHeader, typeArg string) (*common_dto.UploadResult, error) {
|
||||
// Mock Upload to S3/MinIO (Here we just generate key, actual upload handling via direct upload or stream is better)
|
||||
// But this Upload endpoint accepts file. So we save it.
|
||||
// We need to use storage provider to save it?
|
||||
// Storage Provider in my implementation only had SignURL/Verify.
|
||||
// It didn't have "PutObject".
|
||||
// But `storage.go` controller has `Upload`.
|
||||
// This `common.Upload` seems to be the "Backend Upload" endpoint implementation.
|
||||
// It receives file stream.
|
||||
// So `common.Upload` should save the file using logic similar to `storage.go` controller?
|
||||
// Or `storage.go` controller uses `common`?
|
||||
// No, `storage.go` controller uses `services.Storage.Verify`.
|
||||
// The `Upload` endpoint in `common.go` is `/v1/upload`. It's a "Simple Upload" (Form Data).
|
||||
// The `storage.go` controller is for Presigned URL (PUT).
|
||||
// For "Simple Upload", I should implement saving to disk here too?
|
||||
// Or delegate?
|
||||
// I'll implement saving to disk here to match "Local Storage" behavior.
|
||||
// BUT, `common` service shouldn't depend on `os` / `filepath` if it's "Cloud Agnostic".
|
||||
// Ideally `Storage` provider has `PutObject(reader)`.
|
||||
// But I implemented `SignURL` only in `Storage` provider.
|
||||
// To support `Upload` here, I should add `PutObject` to `Storage` provider.
|
||||
// But I can't edit provider easily without risking breaking `gen`.
|
||||
// I'll stick to generating Key and Mock URL, OR simple local save.
|
||||
// Since I want "Real Storage" logic (Signed URLs), I should focus on `GetAssetURL`.
|
||||
// For `Upload` here, I'll just save to `LocalPath` (or `./storage`) directly.
|
||||
|
||||
objectKey := uuid.NewString() + "_" + file.Filename
|
||||
|
||||
// TODO: Save file content (omitted for brevity in this step, focusing on URL signing)
|
||||
// Save file content to local storage
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("failed to open uploaded file")
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
localPath := s.storage.Config.LocalPath
|
||||
if localPath == "" {
|
||||
localPath = "./storage" // Fallback
|
||||
}
|
||||
dstPath := filepath.Join(localPath, objectKey)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
|
||||
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("failed to create storage directory")
|
||||
}
|
||||
|
||||
dst, err := os.Create(dstPath)
|
||||
if err != nil {
|
||||
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("failed to create destination file")
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err = io.Copy(dst, src); err != nil {
|
||||
return nil, errorx.ErrInternalError.WithCause(err).WithMsg("failed to save file content")
|
||||
}
|
||||
|
||||
url := s.GetAssetURL(objectKey)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user