Skip to content

Commit

Permalink
🩹 handlerで動画のタイプmkv,m4vに対応する
Browse files Browse the repository at this point in the history
  • Loading branch information
ikura-hamu committed Dec 28, 2024
1 parent 7194f34 commit ff79bad
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 23 deletions.
45 changes: 25 additions & 20 deletions src/handler/v2/game_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
73 changes: 70 additions & 3 deletions src/handler/v2/game_video_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit ff79bad

Please sign in to comment.