generated from VLADIMIR/template
44 lines
1.4 KiB
Go
44 lines
1.4 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"mime"
|
|
|
|
"evening_detective_server/internal/modules/roles"
|
|
"evening_detective_server/proto"
|
|
|
|
"google.golang.org/genproto/googleapis/api/httpbody"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func (s *server) DownloadGamePrintMaterials(ctx context.Context, req *proto.GetGameReq) (*httpbody.HttpBody, error) {
|
|
claims, err := claimsFromContext(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !roles.HasRole(claims, roles.Organizer) && !roles.HasRole(claims, roles.Author) {
|
|
return nil, status.Error(codes.PermissionDenied, "нет доступа к материалам игры")
|
|
}
|
|
if req == nil || req.ID <= 0 {
|
|
return nil, status.Error(codes.InvalidArgument, "некорректный идентификатор игры")
|
|
}
|
|
data, filename, err := s.gamePrintService.Download(ctx, int(req.ID))
|
|
if err != nil {
|
|
return nil, gamePrintError(err)
|
|
}
|
|
if err := grpc.SetHeader(ctx, metadata.Pairs(
|
|
"Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}),
|
|
"X-Content-Type-Options", "nosniff",
|
|
"Cache-Control", "no-store",
|
|
)); err != nil {
|
|
return nil, status.Error(codes.Internal, "не удалось установить заголовки архива")
|
|
}
|
|
return &httpbody.HttpBody{
|
|
ContentType: "application/zip",
|
|
Data: data,
|
|
}, nil
|
|
}
|