feat: Roms can now be downloaded via the API

This commit is contained in:
Michael Green
2023-06-16 23:56:12 +10:00
parent 4f40d04d30
commit 8658689ac4
5 changed files with 144 additions and 20 deletions

View File

@@ -30,7 +30,7 @@ namespace gaseous_server.Classes.Metadata
returnValue = new Game returnValue = new Game
{ {
Id = 0, Id = 0,
Name = "Unknown" Name = "Unknown Title"
}; };
Storage.NewCacheValue(returnValue); Storage.NewCacheValue(returnValue);

View File

@@ -32,7 +32,7 @@ namespace gaseous_server.Classes.Metadata
returnValue = new Platform returnValue = new Platform
{ {
Id = 0, Id = 0,
Name = "Unknown" Name = "Unknown Platform"
}; };
Storage.NewCacheValue(returnValue); Storage.NewCacheValue(returnValue);

View File

@@ -6,6 +6,30 @@ namespace gaseous_server.Classes
{ {
public class Roms public class Roms
{ {
public static List<RomItem> GetRoms(long GameId)
{
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "SELECT * FROM games_roms WHERE gameid = @id ORDER BY `name`";
Dictionary<string, object> dbDict = new Dictionary<string, object>();
dbDict.Add("id", GameId);
DataTable romDT = db.ExecuteCMD(sql, dbDict);
if (romDT.Rows.Count > 0)
{
List<RomItem> romItems = new List<RomItem>();
foreach (DataRow romDR in romDT.Rows)
{
romItems.Add(BuildRom(romDR));
}
return romItems;
}
else
{
throw new Exception("Unknown Game Id");
}
}
public static RomItem GetRom(long RomId) public static RomItem GetRom(long RomId)
{ {
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString); Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
@@ -17,6 +41,17 @@ namespace gaseous_server.Classes
if (romDT.Rows.Count > 0) if (romDT.Rows.Count > 0)
{ {
DataRow romDR = romDT.Rows[0]; DataRow romDR = romDT.Rows[0];
RomItem romItem = BuildRom(romDR);
return romItem;
}
else
{
throw new Exception("Unknown ROM Id");
}
}
private static RomItem BuildRom(DataRow romDR)
{
RomItem romItem = new RomItem RomItem romItem = new RomItem
{ {
Id = (long)romDR["id"], Id = (long)romDR["id"],
@@ -36,11 +71,6 @@ namespace gaseous_server.Classes
}; };
return romItem; return romItem;
} }
else
{
throw new Exception("Unknown ROM Id");
}
}
public class RomItem public class RomItem
{ {

View File

@@ -433,6 +433,97 @@ namespace gaseous_server.Controllers
} }
} }
[HttpGet]
[Route("{GameId}/roms")]
[ProducesResponseType(typeof(List<Classes.Roms.RomItem>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult GameRom(long GameId)
{
try
{
Game gameObject = Classes.Metadata.Games.GetGame(GameId, false, false);
List<Classes.Roms.RomItem> roms = Classes.Roms.GetRoms(GameId);
return Ok(roms);
}
catch
{
return NotFound();
}
}
[HttpGet]
[Route("{GameId}/roms/{RomId}")]
[ProducesResponseType(typeof(Classes.Roms.RomItem), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult GameRom(long GameId, long RomId)
{
try
{
Game gameObject = Classes.Metadata.Games.GetGame(GameId, false, false);
Classes.Roms.RomItem rom = Classes.Roms.GetRom(RomId);
if (rom.GameId == GameId)
{
return Ok(rom);
}
else
{
return NotFound();
}
}
catch
{
return NotFound();
}
}
[HttpGet]
[Route("{GameId}/roms/{RomId}/file")]
[ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult GameRomFile(long GameId, long RomId)
{
try
{
IGDB.Models.Game gameObject = Classes.Metadata.Games.GetGame(GameId, false, false);
Classes.Roms.RomItem rom = Classes.Roms.GetRom(RomId);
if (rom.GameId != GameId)
{
return NotFound();
}
string romFilePath = rom.Path;
if (System.IO.File.Exists(romFilePath))
{
string filename = Path.GetFileName(romFilePath);
string filepath = romFilePath;
byte[] filedata = System.IO.File.ReadAllBytes(filepath);
string contentType = "application/octet-stream";
var cd = new System.Net.Mime.ContentDisposition
{
FileName = filename,
Inline = false,
};
Response.Headers.Add("Content-Disposition", cd.ToString());
return File(filedata, contentType);
}
else
{
return NotFound();
}
}
catch
{
return NotFound();
}
}
[HttpGet] [HttpGet]
[Route("{GameId}/screenshots")] [Route("{GameId}/screenshots")]
[ProducesResponseType(typeof(List<Screenshot>), StatusCodes.Status200OK)] [ProducesResponseType(typeof(List<Screenshot>), StatusCodes.Status200OK)]

View File

@@ -35,7 +35,10 @@ builder.Services.AddControllers().AddJsonOptions(x =>
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
});
builder.Services.AddHostedService<TimedHostedService>(); builder.Services.AddHostedService<TimedHostedService>();
var app = builder.Build(); var app = builder.Build();