/
public.go
69 lines (60 loc) · 1.76 KB
/
public.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package v3
import (
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"github.com/traPtitech/traQ/repository"
"github.com/traPtitech/traQ/router/consts"
"github.com/traPtitech/traQ/router/extension/herror"
"github.com/traPtitech/traQ/service/file"
)
// GetVersion GET /version
func (h *Handlers) GetVersion(c echo.Context) error {
extLogins := make([]string, 0, len(h.EnabledExternalAccountProviders))
for p := range h.EnabledExternalAccountProviders {
extLogins = append(extLogins, p)
}
return c.JSON(http.StatusOK, echo.Map{
"version": h.Version,
"revision": h.Revision,
"flags": echo.Map{
"externalLogin": extLogins,
"signUpAllowed": h.Config.AllowSignUp,
},
})
}
// GetPublicUserIcon GET /public/icon/{username}
func (h *Handlers) GetPublicUserIcon(c echo.Context) error {
username := c.Param("username")
// ユーザー取得
user, err := h.Repo.GetUserByName(username, false)
if err != nil {
switch err {
case repository.ErrNotFound:
return herror.NotFound()
default:
return herror.InternalServerError(err)
}
}
// ファイルメタ取得
meta, err := h.FileManager.Get(user.GetIconFileID())
if err != nil {
switch err {
case file.ErrNotFound:
return herror.NotFound()
default:
return herror.InternalServerError(err)
}
}
// ファイルオープン
file, err := meta.Open()
if err != nil {
return herror.InternalServerError(err)
}
defer file.Close()
c.Response().Header().Set(echo.HeaderContentType, meta.GetMIMEType())
c.Response().Header().Set(consts.HeaderETag, strconv.Quote(meta.GetMD5Hash()))
c.Response().Header().Set(consts.HeaderCacheControl, "public, max-age=3600") // 1時間キャッシュ
http.ServeContent(c.Response(), c.Request(), meta.GetFileName(), meta.GetCreatedAt(), file)
return nil
}