This commit is contained in:
2026-10-02 23:26:21 +07:00
parent 4d8d6caadd
commit 7efbaf4754
3 changed files with 14 additions and 2 deletions
+1 -2
View File
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"evening_detective_server/internal/modules/file_storage"
"evening_detective_server/internal/modules/roles"
"evening_detective_server/internal/services/file_service"
proto "evening_detective_server/proto"
@@ -50,7 +49,7 @@ func (s *server) DownloadFile(ctx context.Context, req *proto.DownloadFileReq) (
file, err := s.fileService.DownloadFile(ctx, req.Filename)
if err != nil {
switch {
case errors.Is(err, file_storage.ErrFileNotFound):
case errors.Is(err, file_service.ErrFileNotFound):
return nil, status.Errorf(codes.NotFound, "%v", err)
default:
return nil, status.Errorf(codes.Internal, "%v", err)
@@ -20,6 +20,8 @@ var (
ErrInvalidExtension = errors.New("недопустимое расширение файла (разрешены pdf, jpg, jpeg, png, gif, webp, mp3, ogg, wav, mp4, webm)")
ErrInvalidContentType = errors.New("содержимое файла не соответствует разрешённым типам (pdf, изображения, аудио или видео)")
ErrFileTooLarge = errors.New("файл слишком большой")
// ErrFileNotFound — файл отсутствует в хранилище.
ErrFileNotFound = errors.New("файл не найден")
)
// allowedExtensions — расширения, разрешённые для загрузки: PDF, изображения
@@ -152,6 +154,9 @@ func (s *FileService) DownloadFile(
) (*file_storage.File, error) {
file, err := s.fileStorage.Get(ctx, filename)
if err != nil {
if errors.Is(err, file_storage.ErrFileNotFound) {
return nil, ErrFileNotFound
}
return nil, err
}
return file, nil
@@ -155,6 +155,14 @@ func TestUploadFileTooLarge(t *testing.T) {
}
}
func TestDownloadFileNotFound(t *testing.T) {
svc := newService(1 << 20)
_, err := svc.DownloadFile(context.Background(), "missing.pdf")
if !errors.Is(err, ErrFileNotFound) {
t.Fatalf("err = %v, want ErrFileNotFound", err)
}
}
func bytes1024() []byte {
b := make([]byte, 1024)
copy(b, "%PDF-1.7 ")