Skip to content

Commit

Permalink
Merge pull request #1083 from traPtitech/fix/movie_other
Browse files Browse the repository at this point in the history
mp4以外の動画形式も許容する
  • Loading branch information
ikura-hamu authored Dec 30, 2024
2 parents 8d846d7 + 1211314 commit a52a8bc
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 176 deletions.
4 changes: 3 additions & 1 deletion docs/openapi/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ servers:
- url: /api/v2 #oapi-codegenでproxy後にもmatchできるようにするため必要
info:
description: 'traP Collection v2'
version: '2.4.0'
version: '2.4.1'
title: 'traP Collection v2'
contact:
name: traP
Expand Down Expand Up @@ -3064,6 +3064,8 @@ components:
type: string
enum:
- video/mp4
- video/m4v
- video/mkv
description: |
ゲーム紹介動画のmimeです。
GameVideoContent:
Expand Down
2 changes: 2 additions & 0 deletions src/domain/values/game_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func NewGameVideoIDFromUUID(id uuid.UUID) GameVideoID {

const (
GameVideoTypeMp4 GameVideoType = iota
GameVideoTypeM4v
GameVideoTypeMkv
)

func NewGameVideoTmpURL(tmpURL *url.URL) GameVideoTmpURL {
Expand Down
41 changes: 26 additions & 15 deletions src/handler/v2/game_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v2

import (
"errors"
"fmt"
"io"
"log"
"net/http"
Expand All @@ -26,6 +27,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 "", fmt.Errorf("invalid video type: %v", value)
}
}

// ゲーム動画一覧の取得
// (GET /games/{gameID}/videos)
func (gameVideo *GameVideo) GetGameVideos(c echo.Context, gameID openapi.GameIDInPath) error {
Expand All @@ -41,11 +55,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,11 +100,10 @@ func (gameVideo *GameVideo) PostGameVideo(c echo.Context, gameID openapi.GameIDI
return echo.NewHTTPError(http.StatusInternalServerError, "failed to save game video")
}

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 nil
Expand Down Expand Up @@ -149,11 +161,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
114 changes: 111 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 @@ -225,9 +264,41 @@ func TestPostGameVideo(t *testing.T) {
CreatedAt: now,
},
},
{
description: "m4vでもエラーなし",
gameID: uuid.UUID(values.NewGameID()),
reader: bytes.NewReader([]byte("test")),
executeSaveGameVideo: true,
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()),
reader: bytes.NewReader([]byte("test")),
executeSaveGameVideo: true,
video: domain.NewGameVideo(
gameVideoID1,
values.GameVideoTypeMkv,
now,
),
resVideo: openapi.GameVideo{
Id: uuid.UUID(gameVideoID1),
Mime: openapi.Videomkv,
CreatedAt: now,
},
},
{
// serviceが正しく動作していればあり得ないが、念のため確認
description: "mp4でないので500",
description: "mp4,m4v,mkvでないので500",
gameID: uuid.UUID(values.NewGameID()),
reader: bytes.NewReader([]byte("test")),
executeSaveGameVideo: true,
Expand All @@ -248,6 +319,15 @@ func TestPostGameVideo(t *testing.T) {
isErr: true,
statusCode: http.StatusNotFound,
},
{
description: "SaveGameVideoがErrInvalidFormatなので400",
gameID: uuid.UUID(values.NewGameID()),
reader: bytes.NewReader([]byte("test")),
executeSaveGameVideo: true,
saveGameVideoErr: service.ErrInvalidFormat,
isErr: true,
statusCode: http.StatusBadRequest,
},
{
description: "SaveGameVideoがエラーなので500",
gameID: uuid.UUID(values.NewGameID()),
Expand Down Expand Up @@ -483,7 +563,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
Loading

0 comments on commit a52a8bc

Please sign in to comment.