This commit is contained in:
2026-10-02 23:36:54 +07:00
parent 9419df9ced
commit b723a55e12
4 changed files with 31 additions and 27 deletions
+3 -4
View File
@@ -17,7 +17,6 @@ import (
"time"
"evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/repos"
)
// Лимиты на входящие архивы (защита от zip-bomb). Суммарный объём считается
@@ -63,7 +62,7 @@ func ImagePath(ref, domain string) (string, bool) {
// URL остаются без изменений. Архив детерминирован (порядок записей и метки).
func (a *scenarioArchive) Pack(
ctx context.Context,
scenario *repos.Scenario,
scenario *Scenario,
domain string,
getFile func(ctx context.Context, name string) ([]byte, error),
) ([]byte, error) {
@@ -72,8 +71,8 @@ func (a *scenarioArchive) Pack(
}
story := &storytelling.Story{}
if scenario.Scenario != "" {
if err := json.Unmarshal([]byte(scenario.Scenario), story); err != nil {
if scenario.Story != "" {
if err := json.Unmarshal([]byte(scenario.Story), story); err != nil {
return nil, fmt.Errorf("не удалось разобрать историю сценария: %w", err)
}
}
@@ -11,22 +11,20 @@ import (
"time"
"evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/repos"
)
const testDomain = "http://storage.test/api/files/"
var testArchive = NewScenarioArchive()
func buildScenario() *repos.Scenario {
func buildScenario() *Scenario {
description := "Детективная история"
image := "cover.png"
return &repos.Scenario{
ID: 1,
return &Scenario{
Name: "Ночной клуб",
Description: &description,
Image: &image,
Scenario: `{"places":[
Story: `{"places":[
{"code":"club","name":"Клуб","text":"Тёмный зал","image":"club.png","applications":[{"name":"Билет","image":"ticket.jpg"}]},
{"code":"parking","name":"Парковка","text":"Пусто","image":"club.png"}
]}`,
@@ -101,12 +99,11 @@ func TestPackUnpackRoundTrip(t *testing.T) {
func TestPackUnpackIntroAudioRoundTrip(t *testing.T) {
description := "desc"
image := "cover.png"
scenario := &repos.Scenario{
ID: 1,
scenario := &Scenario{
Name: "С введением",
Description: &description,
Image: &image,
Scenario: `{"introduction":{"text":"Введение","audio":"intro.mp3"},"places":[
Story: `{"introduction":{"text":"Введение","audio":"intro.mp3"},"places":[
{"code":"club","name":"Клуб","text":"Тёмный зал","image":"club.png"}
]}`,
}
@@ -142,12 +139,11 @@ func TestPackUnpackIntroAudioRoundTrip(t *testing.T) {
func TestPackExternalURLsKeptAndNotFetched(t *testing.T) {
description := "desc"
scenario := &repos.Scenario{
ID: 1,
scenario := &Scenario{
Name: "Внешний",
Description: &description,
Image: ptr("https://example.com/cover.png"),
Scenario: `{"places":[
Story: `{"places":[
{"code":"p","name":"P","text":"t","image":"http://other.example/x.png"}
]}`,
}
@@ -182,12 +178,11 @@ func TestPackExternalURLsKeptAndNotFetched(t *testing.T) {
func TestPackLegacyFullURLImage(t *testing.T) {
// Устаревшие данные: полный URL хранилища срезается до базового имени файла.
description := "desc"
scenario := &repos.Scenario{
ID: 1,
scenario := &Scenario{
Name: "Legacy",
Description: &description,
Image: ptr(testDomain + "cover.png"),
Scenario: `{"places":[]}`,
Story: `{"places":[]}`,
}
data, err := testArchive.Pack(context.Background(), scenario, testDomain, newMemoryFiles().get)
if err != nil {
@@ -240,11 +235,10 @@ func TestPackNilScenario(t *testing.T) {
func TestPackImageNameCollision(t *testing.T) {
description := "desc"
scenario := &repos.Scenario{
ID: 1,
scenario := &Scenario{
Name: "Конфликт",
Description: &description,
Scenario: `{"places":[
Story: `{"places":[
{"code":"a","name":"A","text":"t","image":"a.png"},
{"code":"b","name":"B","text":"t","image":"dir/a.png"}
]}`,
@@ -471,11 +465,10 @@ func TestPackTotalSizeLimit(t *testing.T) {
defer func() { maxPackSize = prev }()
description := "desc"
scenario := &repos.Scenario{
ID: 1,
scenario := &Scenario{
Name: "Большой",
Description: &description,
Scenario: `{"places":[
Story: `{"places":[
{"code":"a","name":"A","text":"t","image":"a.png"},
{"code":"b","name":"B","text":"t","image":"b.png"}
]}`,
@@ -4,7 +4,6 @@ import (
"context"
"evening_detective_server/internal/modules/storytelling"
"evening_detective_server/internal/repos"
)
// Имя файла с описанием сценария внутри архива.
@@ -23,6 +22,14 @@ type ScenarioJSON struct {
Story *storytelling.Story `json:"story"`
}
// Scenario — данные сценария, необходимые для сборки архива.
type Scenario struct {
Name string
Description *string
Image *string
Story string
}
type Bundle struct {
Scenario *ScenarioJSON
Files map[string][]byte
@@ -36,7 +43,7 @@ type IScenarioArchive interface {
// внешние URL остаются без изменений.
Pack(
ctx context.Context,
scenario *repos.Scenario,
scenario *Scenario,
domain string,
getFile func(ctx context.Context, name string) ([]byte, error),
) ([]byte, error)
@@ -424,7 +424,12 @@ func (s *ScenarioService) DownloadArchive(
}
data, err := s.scenarioArchive.Pack(
ctx,
scenario,
&scenario_archive.Scenario{
Name: scenario.Name,
Description: scenario.Description,
Image: scenario.Image,
Story: scenario.Scenario,
},
s.domain,
func(ctx context.Context, name string) ([]byte, error) {
file, err := s.fileStorage.Get(ctx, name)