All checks were successful
Server CI/CD / deploy (push) Successful in 36s
- POST /api/download/upload/game - 게임 zip 업로드 - POST /api/download/upload/launcher - launcher.exe 업로드 - GET /api/download/launcher - launcher.exe 서빙 - Info 모델에 LauncherURL, LauncherSize 필드 추가 - Content-Disposition 헤더로 올바른 파일명 설정 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
package download
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
|
|
"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}
|
|
}
|
|
|
|
func (h *Handler) GetInfo(c *fiber.Ctx) error {
|
|
info, err := h.svc.GetInfo()
|
|
if err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "다운로드 정보가 없습니다"})
|
|
}
|
|
return c.JSON(info)
|
|
}
|
|
|
|
// Upload accepts a raw binary body (application/octet-stream).
|
|
// The filename is passed as a query parameter: ?filename=A301_v1.0.zip
|
|
func (h *Handler) Upload(c *fiber.Ctx) error {
|
|
filename := strings.TrimSpace(c.Query("filename", "game.zip"))
|
|
if !strings.HasSuffix(strings.ToLower(filename), ".zip") {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "zip 파일만 업로드 가능합니다"})
|
|
}
|
|
|
|
body := c.Request().BodyStream()
|
|
info, err := h.svc.Upload(filename, body, h.baseURL)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "업로드 실패: " + err.Error()})
|
|
}
|
|
return c.JSON(info)
|
|
}
|
|
|
|
func (h *Handler) ServeFile(c *fiber.Ctx) error {
|
|
path := h.svc.GameFilePath()
|
|
if _, err := os.Stat(path); err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "파일이 없습니다"})
|
|
}
|
|
info, _ := h.svc.GetInfo()
|
|
filename := "game.zip"
|
|
if info != nil && info.FileName != "" {
|
|
filename = info.FileName
|
|
}
|
|
c.Set("Content-Disposition", `attachment; filename="`+filename+`"`)
|
|
return c.SendFile(path)
|
|
}
|
|
|
|
func (h *Handler) UploadLauncher(c *fiber.Ctx) error {
|
|
body := c.Request().BodyStream()
|
|
info, err := h.svc.UploadLauncher(body, h.baseURL)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "업로드 실패: " + err.Error()})
|
|
}
|
|
return c.JSON(info)
|
|
}
|
|
|
|
func (h *Handler) ServeLauncher(c *fiber.Ctx) error {
|
|
path := h.svc.LauncherFilePath()
|
|
if _, err := os.Stat(path); err != nil {
|
|
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "파일이 없습니다"})
|
|
}
|
|
c.Set("Content-Disposition", `attachment; filename="launcher.exe"`)
|
|
return c.SendFile(path)
|
|
}
|