generated from VLADIMIR/template
add Upload and Download files
This commit is contained in:
@@ -2,12 +2,15 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"evening_detective_server/internal/modules/file_storage"
|
||||
"evening_detective_server/internal/modules/processor_jwt"
|
||||
"evening_detective_server/internal/modules/roles"
|
||||
"evening_detective_server/internal/services/file_service"
|
||||
"evening_detective_server/internal/services/ui_service"
|
||||
"evening_detective_server/internal/services/users_service"
|
||||
proto "evening_detective_server/proto"
|
||||
|
||||
"google.golang.org/genproto/googleapis/api/httpbody"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
@@ -17,15 +20,18 @@ type server struct {
|
||||
|
||||
usersService *users_service.UsersService
|
||||
uiService *ui_service.UiService
|
||||
fileService *file_service.FileService
|
||||
}
|
||||
|
||||
func NewServer(
|
||||
usersService *users_service.UsersService,
|
||||
uiService *ui_service.UiService,
|
||||
fileService *file_service.FileService,
|
||||
) proto.EveningDetectiveServerServer {
|
||||
return &server{
|
||||
usersService: usersService,
|
||||
uiService: uiService,
|
||||
fileService: fileService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,3 +173,32 @@ func (s *server) GetPermissions(
|
||||
Permissions: permissions,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *server) UploadFile(ctx context.Context, req *proto.UploadFileReq) (*proto.UploadFileRsp, error) {
|
||||
filename, err := s.fileService.UploadFile(
|
||||
ctx,
|
||||
&file_storage.File{
|
||||
Name: req.Filename,
|
||||
Data: req.Data,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return &proto.UploadFileRsp{
|
||||
Error: err.Error(),
|
||||
}, nil
|
||||
}
|
||||
return &proto.UploadFileRsp{
|
||||
Filename: filename,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *server) DownloadFile(ctx context.Context, req *proto.DownloadFileReq) (*httpbody.HttpBody, error) {
|
||||
file, err := s.fileService.DownloadFile(ctx, req.Filename)
|
||||
if err != nil {
|
||||
return &httpbody.HttpBody{}, nil
|
||||
}
|
||||
return &httpbody.HttpBody{
|
||||
Data: file.Data,
|
||||
ContentType: file.Mime,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/kzzan/s3kit"
|
||||
)
|
||||
@@ -28,6 +29,17 @@ func NewRustFSStorage(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
err = client.CreateBucket(
|
||||
ctx,
|
||||
bucket,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &storage{
|
||||
client: client,
|
||||
bucket: bucket,
|
||||
@@ -35,7 +47,8 @@ func NewRustFSStorage(
|
||||
}
|
||||
|
||||
func (s *storage) Put(ctx context.Context, file *File) error {
|
||||
return s.client.PutObjectBytes(ctx, s.bucket, file.Name, file.Data, file.Mime)
|
||||
mime := s.getMimeType(file.Name)
|
||||
return s.client.PutObjectBytes(ctx, s.bucket, file.Name, file.Data, mime)
|
||||
}
|
||||
|
||||
func (s *storage) Get(ctx context.Context, filename string) (*File, error) {
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package file_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"evening_detective_server/internal/modules/file_storage"
|
||||
)
|
||||
|
||||
type FileService struct {
|
||||
fileStorage file_storage.IFileStorage
|
||||
}
|
||||
|
||||
func NewFileService(
|
||||
fileStorage file_storage.IFileStorage,
|
||||
) *FileService {
|
||||
return &FileService{
|
||||
fileStorage: fileStorage,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *FileService) UploadFile(
|
||||
ctx context.Context,
|
||||
file *file_storage.File,
|
||||
) (string, error) {
|
||||
if err := s.fileStorage.Put(ctx, file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return file.Name, nil
|
||||
}
|
||||
|
||||
func (s *FileService) DownloadFile(
|
||||
ctx context.Context,
|
||||
filename string,
|
||||
) (*file_storage.File, error) {
|
||||
file, err := s.fileStorage.Get(ctx, filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
Reference in New Issue
Block a user