From ff79badf9a27e1be0c321a6b0cb4616b47172beb Mon Sep 17 00:00:00 2001 From: ikura-hamu <104292023+ikura-hamu@users.noreply.github.com> Date: Sat, 28 Dec 2024 18:40:36 +0900 Subject: [PATCH] =?UTF-8?q?:adhesive=5Fbandage:=20handler=E3=81=A7?= =?UTF-8?q?=E5=8B=95=E7=94=BB=E3=81=AE=E3=82=BF=E3=82=A4=E3=83=97mkv,m4v?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E5=BF=9C=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handler/v2/game_video.go | 45 ++++++++++--------- src/handler/v2/game_video_test.go | 73 +++++++++++++++++++++++++++++-- 2 files changed, 95 insertions(+), 23 deletions(-) diff --git a/src/handler/v2/game_video.go b/src/handler/v2/game_video.go index 4af687e9..e440adc8 100644 --- a/src/handler/v2/game_video.go +++ b/src/handler/v2/game_video.go @@ -26,6 +26,19 @@ func NewGameVideo(gameVideoService service.GameVideoV2) *GameVideo { } } +func convertVideoType(value values.GameVideoType) (openapi.GameVideoMime, error) { + switch value { + case values.GameVideoTypeMp4: + return openapi.Videomp4, nil + case values.GameVideoTypeM4v: + return openapi.Videom4v, nil + case values.GameVideoTypeMkv: + return openapi.Videomkv, nil + default: + return "", errors.New("invalid video type") + } +} + // ゲーム動画一覧の取得 // (GET /games/{gameID}/videos) func (gameVideo *GameVideo) GetGameVideos(c echo.Context, gameID openapi.GameIDInPath) error { @@ -41,11 +54,10 @@ func (gameVideo *GameVideo) GetGameVideos(c echo.Context, gameID openapi.GameIDI resVideos := make([]openapi.GameVideo, 0, len(videos)) for _, video := range videos { var mime openapi.GameVideoMime - if video.GetType() == values.GameVideoTypeMp4 { - mime = openapi.Videomp4 - } else { - log.Printf("error: unknown game video type: %v\n", video.GetType()) - return echo.NewHTTPError(http.StatusInternalServerError, "unknown game video type") + mime, err := convertVideoType(video.GetType()) + if err != nil { + log.Printf("error: failed to convert video type: %v\n", err) + return echo.NewHTTPError(http.StatusInternalServerError, "failed to convert video type") } resVideos = append(resVideos, openapi.GameVideo{ @@ -87,16 +99,10 @@ func (gameVideo *GameVideo) PostGameVideo(c echo.Context, gameID openapi.GameIDI return echo.NewHTTPError(http.StatusInternalServerError, "failed to save game video") } - switch video.GetType() { - case values.GameVideoTypeMp4: - mime = openapi.Videomp4 - case values.GameVideoTypeM4v: - mime = openapi.Videom4v - case values.GameVideoTypeMkv: - mime = openapi.Videomkv - default: - log.Printf("error: unknown game video type: %v\n", video.GetType()) - return echo.NewHTTPError(http.StatusInternalServerError, "unknown game video type") + mime, err = convertVideoType(video.GetType()) + if err != nil { + log.Printf("error: failed to convert video type: %v\n", err) + return echo.NewHTTPError(http.StatusInternalServerError, "failed to convert video type") } return nil @@ -154,11 +160,10 @@ func (gameVideo *GameVideo) GetGameVideoMeta(ctx echo.Context, gameID openapi.Ga } var mime openapi.GameVideoMime - if video.GetType() == values.GameVideoTypeMp4 { - mime = openapi.Videomp4 - } else { - log.Printf("error: unknown game video type: %v\n", video.GetType()) - return echo.NewHTTPError(http.StatusInternalServerError, "unknown game video type") + mime, err = convertVideoType(video.GetType()) + if err != nil { + log.Printf("error: failed to convert video type: %v\n", err) + return echo.NewHTTPError(http.StatusInternalServerError, "failed to convert video type") } return ctx.JSON(http.StatusOK, openapi.GameVideo{ diff --git a/src/handler/v2/game_video_test.go b/src/handler/v2/game_video_test.go index ea772184..5f628f80 100644 --- a/src/handler/v2/game_video_test.go +++ b/src/handler/v2/game_video_test.go @@ -48,6 +48,7 @@ func TestGetGameVideos(t *testing.T) { gameVideoID1 := values.NewGameVideoID() gameVideoID2 := values.NewGameVideoID() gameVideoID3 := values.NewGameVideoID() + gameVideoID4 := values.NewGameVideoID() now := time.Now() testCases := []test{ @@ -70,7 +71,7 @@ func TestGetGameVideos(t *testing.T) { }, }, { - description: "mp4でないので500", + description: "動画タイプがmp4,mkv,m4vでないので500", gameID: uuid.UUID(values.NewGameID()), videos: []*domain.GameVideo{ domain.NewGameVideo( @@ -130,6 +131,44 @@ func TestGetGameVideos(t *testing.T) { }, }, }, + { + description: "動画のタイプが複数あっても問題なし", + gameID: uuid.UUID(values.NewGameID()), + videos: []*domain.GameVideo{ + domain.NewGameVideo( + gameVideoID2, + values.GameVideoTypeMp4, + now, + ), + domain.NewGameVideo( + gameVideoID3, + values.GameVideoTypeM4v, + now.Add(-10*time.Hour), + ), + domain.NewGameVideo( + gameVideoID4, + values.GameVideoTypeMkv, + now.Add(-20*time.Hour), + ), + }, + resVideos: []openapi.GameVideo{ + { + Id: uuid.UUID(gameVideoID2), + Mime: openapi.Videomp4, + CreatedAt: now, + }, + { + Id: uuid.UUID(gameVideoID3), + Mime: openapi.Videom4v, + CreatedAt: now.Add(-10 * time.Hour), + }, + { + Id: uuid.UUID(gameVideoID4), + Mime: openapi.Videomkv, + CreatedAt: now.Add(-20 * time.Hour), + }, + }, + }, } for _, testCase := range testCases { @@ -259,7 +298,7 @@ func TestPostGameVideo(t *testing.T) { }, { // serviceが正しく動作していればあり得ないが、念のため確認 - description: "mp4でないので500", + description: "mp4,m4v,mkvでないので500", gameID: uuid.UUID(values.NewGameID()), reader: bytes.NewReader([]byte("test")), executeSaveGameVideo: true, @@ -515,7 +554,35 @@ func TestGetGameVideoMeta(t *testing.T) { }, }, { - description: "mp4でないので500", + description: "m4vで問題ないのでエラーなし", + gameID: uuid.UUID(values.NewGameID()), + video: domain.NewGameVideo( + gameVideoID1, + values.GameVideoTypeM4v, + now, + ), + resVideo: openapi.GameVideo{ + Id: uuid.UUID(gameVideoID1), + Mime: openapi.Videom4v, + CreatedAt: now, + }, + }, + { + description: "mkvで問題ないのでエラーなし", + gameID: uuid.UUID(values.NewGameID()), + video: domain.NewGameVideo( + gameVideoID1, + values.GameVideoTypeMkv, + now, + ), + resVideo: openapi.GameVideo{ + Id: uuid.UUID(gameVideoID1), + Mime: openapi.Videomkv, + CreatedAt: now, + }, + }, + { + description: "mp4,mkv,m4vでないので500", gameID: uuid.UUID(values.NewGameID()), video: domain.NewGameVideo( values.NewGameVideoID(),