132 lines
3.2 KiB
Go
132 lines
3.2 KiB
Go
package web_api
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/api"
|
|
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
|
"git.itzana.me/strafesnet/maps-service/pkg/roblox"
|
|
"git.itzana.me/strafesnet/maps-service/pkg/service"
|
|
)
|
|
|
|
// ListMaps implements listMaps operation.
|
|
//
|
|
// Get list of maps.
|
|
//
|
|
// GET /maps
|
|
func (svc *Service) ListMaps(ctx context.Context, params api.ListMapsParams) ([]api.Map, error) {
|
|
filter := service.NewMapFilter()
|
|
|
|
if display_name, display_name_ok := params.DisplayName.Get(); display_name_ok{
|
|
filter.SetDisplayName(display_name)
|
|
}
|
|
if creator, creator_ok := params.Creator.Get(); creator_ok{
|
|
filter.SetCreator(creator)
|
|
}
|
|
if game_id, game_id_ok := params.GameID.Get(); game_id_ok{
|
|
filter.SetGameID(uint32(game_id))
|
|
}
|
|
|
|
items, err := svc.inner.ListMaps(ctx,
|
|
filter,
|
|
model.Page{
|
|
Size: params.Limit,
|
|
Number: params.Page,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var resp []api.Map
|
|
for _, item := range items {
|
|
resp = append(resp, api.Map{
|
|
ID: item.ID,
|
|
DisplayName: item.DisplayName,
|
|
Creator: item.Creator,
|
|
GameID: int32(item.GameID),
|
|
Date: item.Date.Unix(),
|
|
CreatedAt: item.CreatedAt.Unix(),
|
|
UpdatedAt: item.UpdatedAt.Unix(),
|
|
Submitter: item.Submitter,
|
|
Thumbnail: item.Thumbnail,
|
|
AssetVersion: item.AssetVersion,
|
|
LoadCount: item.LoadCount,
|
|
Modes: item.Modes,
|
|
})
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
// GetMap implements getScript operation.
|
|
//
|
|
// Get the specified script by ID.
|
|
//
|
|
// GET /maps/{MapID}
|
|
func (svc *Service) GetMap(ctx context.Context, params api.GetMapParams) (*api.Map, error) {
|
|
mapResponse, err := svc.inner.GetMap(ctx, params.MapID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &api.Map{
|
|
ID: mapResponse.ID,
|
|
DisplayName: mapResponse.DisplayName,
|
|
Creator: mapResponse.Creator,
|
|
GameID: int32(mapResponse.GameID),
|
|
Date: mapResponse.Date.Unix(),
|
|
CreatedAt: mapResponse.CreatedAt.Unix(),
|
|
UpdatedAt: mapResponse.UpdatedAt.Unix(),
|
|
Submitter: mapResponse.Submitter,
|
|
Thumbnail: mapResponse.Thumbnail,
|
|
AssetVersion: mapResponse.AssetVersion,
|
|
LoadCount: mapResponse.LoadCount,
|
|
Modes: mapResponse.Modes,
|
|
}, nil
|
|
}
|
|
|
|
// DownloadMapAsset invokes downloadMapAsset operation.
|
|
//
|
|
// Download the map asset.
|
|
//
|
|
// GET /maps/{MapID}/download
|
|
func (svc *Service) DownloadMapAsset(ctx context.Context, params api.DownloadMapAssetParams) (ok api.DownloadMapAssetOK, err error) {
|
|
userInfo, success := ctx.Value("UserInfo").(UserInfoHandle)
|
|
if !success {
|
|
return ok, ErrUserInfo
|
|
}
|
|
|
|
has_role, err := userInfo.HasRoleMapDownload()
|
|
if err != nil {
|
|
return ok, err
|
|
}
|
|
|
|
if !has_role {
|
|
return ok, ErrPermissionDeniedNeedRoleMapDownload
|
|
}
|
|
|
|
// Ensure map exists in the db!
|
|
// This could otherwise be used to access any asset
|
|
_, err = svc.inner.GetMap(ctx, params.MapID)
|
|
if err != nil {
|
|
return ok, err
|
|
}
|
|
|
|
info, err := svc.roblox.GetAssetLocation(roblox.GetAssetLatestRequest{
|
|
AssetID: uint64(params.MapID),
|
|
})
|
|
if err != nil{
|
|
return ok, err
|
|
}
|
|
|
|
// download the complete file
|
|
asset, err := svc.roblox.DownloadAsset(info)
|
|
if err != nil{
|
|
return ok, err
|
|
}
|
|
|
|
ok.Data = asset
|
|
return ok, nil
|
|
}
|