- 보상 지급 실패 시 즉시 재시도(3회 backoff) + DB 기록 + 백그라운드 워커 재시도 - WaitForTx 폴링으로 블록체인 TX 확정 대기, SendTxAndWait 편의 메서드 - chain 트랜잭션 코드 중복 제거 (userTx/operatorTx 헬퍼, 50% 감소) - AppError 기반 에러 응답 포맷 통일 (8개 코드, 전 핸들러 마이그레이션) - TX 에러 분류 + 한국어 사용자 메시지 매핑 (11가지 패턴) - player 서비스 테스트 20개 + chain WaitForTx 테스트 10개 추가 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
134 lines
4.4 KiB
Go
134 lines
4.4 KiB
Go
package download
|
|
|
|
import (
|
|
"log"
|
|
"mime"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"a301_server/pkg/apperror"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type Handler struct {
|
|
svc *Service
|
|
baseURL string
|
|
}
|
|
|
|
func NewHandler(svc *Service, baseURL string) *Handler {
|
|
return &Handler{svc: svc, baseURL: baseURL}
|
|
}
|
|
|
|
// GetInfo godoc
|
|
// @Summary 다운로드 정보 조회
|
|
// @Description 게임 및 런처 다운로드 정보를 조회합니다
|
|
// @Tags Download
|
|
// @Produce json
|
|
// @Success 200 {object} docs.DownloadInfoResponse
|
|
// @Failure 404 {object} docs.ErrorResponse
|
|
// @Router /api/download/info [get]
|
|
func (h *Handler) GetInfo(c *fiber.Ctx) error {
|
|
info, err := h.svc.GetInfo()
|
|
if err != nil {
|
|
return apperror.NotFound("다운로드 정보가 없습니다")
|
|
}
|
|
return c.JSON(info)
|
|
}
|
|
|
|
// Upload godoc
|
|
// @Summary 게임 파일 업로드 (관리자)
|
|
// @Description 게임 zip 파일을 스트리밍 업로드합니다. Body는 raw binary입니다.
|
|
// @Tags Download
|
|
// @Accept application/octet-stream
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param filename query string false "파일명" default(game.zip)
|
|
// @Success 200 {object} docs.DownloadInfoResponse
|
|
// @Failure 400 {object} docs.ErrorResponse
|
|
// @Failure 401 {object} docs.ErrorResponse
|
|
// @Failure 403 {object} docs.ErrorResponse
|
|
// @Failure 500 {object} docs.ErrorResponse
|
|
// @Router /api/download/upload/game [post]
|
|
func (h *Handler) Upload(c *fiber.Ctx) error {
|
|
filename := strings.TrimSpace(c.Query("filename", "game.zip"))
|
|
// 경로 순회 방지: 디렉토리 구분자 제거, 기본 파일명만 사용
|
|
filename = filepath.Base(filename)
|
|
if !strings.HasSuffix(strings.ToLower(filename), ".zip") {
|
|
return apperror.BadRequest("zip 파일만 업로드 가능합니다")
|
|
}
|
|
if len(filename) > 200 {
|
|
return apperror.BadRequest("파일명이 너무 깁니다")
|
|
}
|
|
|
|
body := c.Request().BodyStream()
|
|
info, err := h.svc.Upload(filename, body, h.baseURL)
|
|
if err != nil {
|
|
log.Printf("game upload failed: %v", err)
|
|
return apperror.Internal("게임 파일 업로드에 실패했습니다")
|
|
}
|
|
return c.JSON(info)
|
|
}
|
|
|
|
// ServeFile godoc
|
|
// @Summary 게임 파일 다운로드
|
|
// @Description 게임 zip 파일을 다운로드합니다
|
|
// @Tags Download
|
|
// @Produce application/octet-stream
|
|
// @Success 200 {file} binary
|
|
// @Failure 404 {object} docs.ErrorResponse
|
|
// @Router /api/download/file [get]
|
|
func (h *Handler) ServeFile(c *fiber.Ctx) error {
|
|
path := h.svc.GameFilePath()
|
|
if _, err := os.Stat(path); err != nil {
|
|
return apperror.NotFound("파일이 없습니다")
|
|
}
|
|
info, _ := h.svc.GetInfo()
|
|
filename := "game.zip"
|
|
if info != nil && info.FileName != "" {
|
|
filename = info.FileName
|
|
}
|
|
c.Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
|
|
return c.SendFile(path)
|
|
}
|
|
|
|
// UploadLauncher godoc
|
|
// @Summary 런처 업로드 (관리자)
|
|
// @Description 런처 실행 파일을 스트리밍 업로드합니다. Body는 raw binary입니다.
|
|
// @Tags Download
|
|
// @Accept application/octet-stream
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Success 200 {object} docs.DownloadInfoResponse
|
|
// @Failure 401 {object} docs.ErrorResponse
|
|
// @Failure 403 {object} docs.ErrorResponse
|
|
// @Failure 500 {object} docs.ErrorResponse
|
|
// @Router /api/download/upload/launcher [post]
|
|
func (h *Handler) UploadLauncher(c *fiber.Ctx) error {
|
|
body := c.Request().BodyStream()
|
|
info, err := h.svc.UploadLauncher(body, h.baseURL)
|
|
if err != nil {
|
|
log.Printf("launcher upload failed: %v", err)
|
|
return apperror.Internal("런처 업로드에 실패했습니다")
|
|
}
|
|
return c.JSON(info)
|
|
}
|
|
|
|
// ServeLauncher godoc
|
|
// @Summary 런처 다운로드
|
|
// @Description 런처 실행 파일을 다운로드합니다
|
|
// @Tags Download
|
|
// @Produce application/octet-stream
|
|
// @Success 200 {file} binary
|
|
// @Failure 404 {object} docs.ErrorResponse
|
|
// @Router /api/download/launcher [get]
|
|
func (h *Handler) ServeLauncher(c *fiber.Ctx) error {
|
|
path := h.svc.LauncherFilePath()
|
|
if _, err := os.Stat(path); err != nil {
|
|
return apperror.NotFound("파일이 없습니다")
|
|
}
|
|
c.Set("Content-Disposition", `attachment; filename="launcher.exe"`)
|
|
return c.SendFile(path)
|
|
}
|