- Restructure handlers into file_commands/file_queries/file_handlers - Add gRPC auth client, JWT middleware, rate limiting, request ID - Add common utilities: logger, sanitizer, s3_errors - Add unit tests for config, mediator, auth, request_id, sanitize - Add proto definitions and generated code - Remove old web UI pages - Add .dockerignore and .env.example
141 lines
4.1 KiB
Go
141 lines
4.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"rag/file-system/internal/domain/repository"
|
|
)
|
|
|
|
type UploadFileHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewUploadFileHandler(repo repository.FileRepository) *UploadFileHandler {
|
|
return &UploadFileHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *UploadFileHandler) Handle(ctx context.Context, cmd UploadFileCommand) (string, error) {
|
|
err := h.Repo.UploadFile(ctx, cmd.BucketName, cmd.FileName, cmd.Data)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return "File uploaded successfully", nil
|
|
}
|
|
|
|
type DownloadFileHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewDownloadFileHandler(repo repository.FileRepository) *DownloadFileHandler {
|
|
return &DownloadFileHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *DownloadFileHandler) Handle(ctx context.Context, query DownloadFileQuery) (io.ReadCloser, error) {
|
|
return h.Repo.DownloadFile(ctx, query.BucketName, query.ObjectKey)
|
|
}
|
|
|
|
type ListFilesHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewListFilesHandler(repo repository.FileRepository) *ListFilesHandler {
|
|
return &ListFilesHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *ListFilesHandler) Handle(ctx context.Context, q ListFilesQuery) (*repository.ListFilesResult, error) {
|
|
return h.Repo.ListObjectsV2(ctx, q.BucketName, q.Prefix, q.MaxKeys, q.Token)
|
|
}
|
|
|
|
type GetFilePreviewHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewGetFilePreviewHandler(repo repository.FileRepository) *GetFilePreviewHandler {
|
|
return &GetFilePreviewHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *GetFilePreviewHandler) Handle(ctx context.Context, q GetFilePreviewQuery) (string, error) {
|
|
return h.Repo.GeneratePresignedURL(ctx, q.BucketName, q.ObjectKey, q.Expiry)
|
|
}
|
|
|
|
type GetFileContentHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewGetFileContentHandler(repo repository.FileRepository) *GetFileContentHandler {
|
|
return &GetFileContentHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *GetFileContentHandler) Handle(ctx context.Context, q GetFileContentQuery) (string, error) {
|
|
return h.Repo.GetFileContent(ctx, q.BucketName, q.ObjectKey)
|
|
}
|
|
|
|
type DeleteFileHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewDeleteFileHandler(repo repository.FileRepository) *DeleteFileHandler {
|
|
return &DeleteFileHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *DeleteFileHandler) Handle(ctx context.Context, cmd DeleteFileCommand) (string, error) {
|
|
err := h.Repo.DeleteFile(ctx, cmd.BucketName, cmd.ObjectKey)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return "File deleted successfully", nil
|
|
}
|
|
|
|
type InitMultipartHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewInitMultipartHandler(repo repository.FileRepository) *InitMultipartHandler {
|
|
return &InitMultipartHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *InitMultipartHandler) Handle(ctx context.Context, cmd InitMultipartCommand) (string, error) {
|
|
return h.Repo.CreateMultipartUpload(ctx, cmd.BucketName, cmd.ObjectKey)
|
|
}
|
|
|
|
type UploadPartHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewUploadPartHandler(repo repository.FileRepository) *UploadPartHandler {
|
|
return &UploadPartHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *UploadPartHandler) Handle(ctx context.Context, cmd UploadPartCommand) (string, error) {
|
|
return h.Repo.UploadPart(ctx, cmd.BucketName, cmd.ObjectKey, cmd.UploadId, cmd.PartNumber, cmd.Data)
|
|
}
|
|
|
|
type CompleteMultipartHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewCompleteMultipartHandler(repo repository.FileRepository) *CompleteMultipartHandler {
|
|
return &CompleteMultipartHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *CompleteMultipartHandler) Handle(ctx context.Context, cmd CompleteMultipartCommand) (string, error) {
|
|
return h.Repo.CompleteMultipartUpload(ctx, cmd.BucketName, cmd.ObjectKey, cmd.UploadId, cmd.Parts)
|
|
}
|
|
|
|
type AbortMultipartHandler struct {
|
|
Repo repository.FileRepository
|
|
}
|
|
|
|
func NewAbortMultipartHandler(repo repository.FileRepository) *AbortMultipartHandler {
|
|
return &AbortMultipartHandler{Repo: repo}
|
|
}
|
|
|
|
func (h *AbortMultipartHandler) Handle(ctx context.Context, cmd AbortMultipartCommand) (string, error) {
|
|
err := h.Repo.AbortMultipartUpload(ctx, cmd.BucketName, cmd.ObjectKey, cmd.UploadId)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return "Multipart upload aborted successfully", nil
|
|
}
|