116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
import fs from 'fs'
|
|
|
|
import { Request, Response } from 'express'
|
|
|
|
import { BaseController } from '#application/base/baseController'
|
|
import Database from '#application/database'
|
|
import Storage from '#application/storage'
|
|
import { AssetData, UUID } from '#application/types'
|
|
import SpriteRepository from '#repositories/spriteRepository'
|
|
import TileRepository from '#repositories/tileRepository'
|
|
import MapRepository from '#repositories/mapRepository'
|
|
|
|
export class AssetsController extends BaseController {
|
|
/**
|
|
* List tiles
|
|
* @param req
|
|
* @param res
|
|
*/
|
|
public async listTiles(req: Request, res: Response) {
|
|
const assets: AssetData[] = []
|
|
const tiles = await TileRepository.getAll()
|
|
|
|
for (const tile of tiles) {
|
|
assets.push({ key: tile.getId(), data: '/assets/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
|
|
}
|
|
|
|
return this.sendSuccess(res, assets)
|
|
}
|
|
|
|
/**
|
|
* List tiles by map
|
|
* @param req
|
|
* @param res
|
|
*/
|
|
public async listTilesByMap(req: Request, res: Response) {
|
|
const mapId = req.params.mapId as UUID
|
|
|
|
if (!mapId) {
|
|
return this.sendError(res, 'Invalid map ID', 400)
|
|
}
|
|
|
|
const map = await MapRepository.getById(mapId)
|
|
if (!map) {
|
|
return this.sendError(res, 'Map not found', 404)
|
|
}
|
|
|
|
const assets: AssetData[] = []
|
|
const tiles = await TileRepository.getByMapId(mapId)
|
|
|
|
for (const tile of tiles) {
|
|
assets.push({ key: tile.getId(), data: '/assets/tiles/' + tile.getId() + '.png', group: 'tiles', updatedAt: tile.getUpdatedAt() } as AssetData)
|
|
}
|
|
|
|
return this.sendSuccess(res, assets)
|
|
}
|
|
|
|
/**
|
|
* List sprite actions
|
|
* @param req
|
|
* @param res
|
|
*/
|
|
public async listSpriteActions(req: Request, res: Response) {
|
|
const spriteId = req.params.spriteId as UUID
|
|
|
|
if (!spriteId) {
|
|
return this.sendError(res, 'Invalid sprite ID', 400)
|
|
}
|
|
|
|
const sprite = await SpriteRepository.getById(spriteId)
|
|
if (!sprite) {
|
|
return this.sendError(res, 'Sprite not found', 404)
|
|
}
|
|
|
|
await Database.getEntityManager().populate(sprite, ['spriteActions'])
|
|
|
|
const assets: AssetData[] = sprite.spriteActions.getItems().map((spriteAction) => ({
|
|
key: sprite.getId() + '-' + spriteAction.getAction(),
|
|
data: '/assets/sprites/' + sprite.getId() + '/' + spriteAction.getAction() + '.png',
|
|
group: spriteAction.getIsAnimated() ? 'sprite_animations' : 'sprites',
|
|
updatedAt: sprite.getUpdatedAt(),
|
|
originX: Number(spriteAction.getOriginX().toString()),
|
|
originY: Number(spriteAction.getOriginY().toString()),
|
|
isAnimated: spriteAction.getIsAnimated(),
|
|
frameRate: spriteAction.getFrameRate(),
|
|
frameWidth: spriteAction.getFrameWidth(),
|
|
frameHeight: spriteAction.getFrameHeight(),
|
|
frameCount: spriteAction.getSprites()?.length
|
|
}))
|
|
|
|
return this.sendSuccess(res, assets)
|
|
}
|
|
|
|
/**
|
|
* Download asset
|
|
* @param req
|
|
* @param res
|
|
*/
|
|
public async downloadAsset(req: Request, res: Response) {
|
|
const { type, spriteId, file } = req.params
|
|
|
|
const assetPath = type === 'sprites' && spriteId ? Storage.getPublicPath(type, spriteId, file) : Storage.getPublicPath(type, file)
|
|
|
|
if (!fs.existsSync(assetPath)) {
|
|
this.logger.error(`File not found: ${assetPath}`)
|
|
return this.sendError(res, 'Asset not found', 404)
|
|
}
|
|
|
|
res.sendFile(assetPath, (err) => {
|
|
if (err) {
|
|
this.logger.error('Error sending file:' + err)
|
|
this.sendError(res, 'Error downloading the asset', 500)
|
|
}
|
|
})
|
|
}
|
|
}
|