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,23 +41,7 @@ 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 = new RomItem RomItem romItem = BuildRom(romDR);
{
Id = (long)romDR["id"],
PlatformId = (long)romDR["platformid"],
GameId = (long)romDR["gameid"],
Name = (string)romDR["name"],
Size = (long)romDR["size"],
CRC = (string)romDR["crc"],
MD5 = (string)romDR["md5"],
SHA1 = (string)romDR["sha1"],
DevelopmentStatus = (string)romDR["developmentstatus"],
Flags = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>((string)romDR["flags"]),
RomType = (int)romDR["romtype"],
RomTypeMedia = (string)romDR["romtypemedia"],
MediaLabel = (string)romDR["medialabel"],
Path = (string)romDR["path"]
};
return romItem; return romItem;
} }
else else
@@ -42,6 +50,28 @@ namespace gaseous_server.Classes
} }
} }
private static RomItem BuildRom(DataRow romDR)
{
RomItem romItem = new RomItem
{
Id = (long)romDR["id"],
PlatformId = (long)romDR["platformid"],
GameId = (long)romDR["gameid"],
Name = (string)romDR["name"],
Size = (long)romDR["size"],
CRC = (string)romDR["crc"],
MD5 = (string)romDR["md5"],
SHA1 = (string)romDR["sha1"],
DevelopmentStatus = (string)romDR["developmentstatus"],
Flags = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>((string)romDR["flags"]),
RomType = (int)romDR["romtype"],
RomTypeMedia = (string)romDR["romtypemedia"],
MediaLabel = (string)romDR["medialabel"],
Path = (string)romDR["path"]
};
return romItem;
}
public class RomItem public class RomItem
{ {
public long Id { get; set; } public long Id { get; set; }

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();