uploading game icons

This commit is contained in:
tmont 2011-02-24 10:55:08 +00:00
parent ff3499b3b9
commit e2bb88498d
6 changed files with 38 additions and 5 deletions

View File

@ -91,4 +91,14 @@ namespace VideoGameQuotes.Api {
public IEnumerable<PublisherDto> Publishers { get; set; }
public IEnumerable<string> Regions { get; set; }
}
public static class GameExtensions {
public static string GetBase64EncodedIcon(this Game game) {
if (game.Icon == null || game.Icon.Length == 0) {
return "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHBSURBVDjLlVM9aMJQEP6eNEF0sbiUouLgoLRkKXS1IG4dC6Xg2LXQRXATHbqVzq4iQjc3sVscnUSnYIdIB9GC4L/xL333IEFsBj04jpf77nt3l+8x0zRxaMViMbTdbtXVahVer9dYLBY/0+k0mcvltEPsGRzMMIyPQCAQ9ng8IAJd14OdTuedp+4PsS4ngslkctFoNNBsNgWB2+3GaDQKOWEdCTgY2WyW9Xo9QbBcLoUfTSDLsoiMMUFgkRxNwHeAdDpt+nw+8EUKp29O5rhEvnEoigJJktBqteD3+0/rgINNulHTNCjzGR5++1Bvb67x+vLF/dmxg3K5HOZB2+12MncxfzAYxJ25wcXjE5ixZCu9m/wufybfUqnLUqmUtwmomAtKi0ajcrVaxWAwQKFQEHOfK1dQajUwrwdSrw8ZEiKRSC4ej0NV1TwjJXI2IxaLyZwA4/FYFHL12T6fz+3o9XrhcrmQyWTQbreZ6IAnZS5dVCoVEpFYmFVEPpvNxJm+0zmRSIhoj0AJunU4HNogq3C/EwtHuqBfaxNQkhJ8NpGwAPtxs9n8c5ug2+2iXq/bojl0S41URKPuv2Dm9JxPsT8W0mO2IJm2EgAAAABJRU5ErkJggg==";
}
return Convert.ToBase64String(game.Icon);
}
}
}

View File

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Web.Mvc;
using Portoa.Persistence;
using Portoa.Util;
@ -47,15 +48,24 @@ namespace VideoGameQuotes.Web.Controllers {
ModelState.AddModelError("GameId", "Invalid game ID");
}
var icon = new byte[0];
if (model.GameIcon != null) {
icon = Convert.FromBase64String(model.GameIcon);
if (icon.Length > 1024) {
ModelState.AddModelError("GameIcon", "Icon must be smaller than 1KB");
}
}
if (!ModelState.IsValid) {
return Json(this.CreateJsonErrorResponse("Some errors occurred"));
}
var game = gameService.FindById(model.GameId);
game.Name = model.GameName;
game.Icon = icon;
game.Region = model.GameRegions;
game.Website = model.GameWebsite;
game.ClearPublishers();
game.ClearSystems();
(model.PublisherIds ?? Enumerable.Empty<int>()).Walk(id => game.AddPublisher(new Publisher { Id = id }));

View File

@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
using VideoGameQuotes.Api;
namespace VideoGameQuotes.Web.Models {
@ -16,7 +15,7 @@ namespace VideoGameQuotes.Web.Models {
public List<int> PublisherIds { get; set; }
[Required(ErrorMessage = "At least one system must be specified, uglyface")]
public List<int> SystemIds { get; set; }
public HttpPostedFileBase GameIcon { get; set; }
public string GameIcon { get; set; }
public int GameId { get; set; }
}
}

View File

@ -48,6 +48,7 @@ namespace VideoGameQuotes.Web.Models {
public IEnumerable<Publisher> AllPublishers { get; set; }
public IEnumerable<Category> AllCategories { get; set; }
public IEnumerable<SelectListItem> GetGameList() {
return AllGames.Select(game => new SelectListItem { Value = game.Id.ToString(), Text = game.Name });
}

View File

@ -38,6 +38,14 @@
<%= Html.TextBox("GameWebsite") %>
</p>
<% if (Model.CurrentUser != null && Model.CurrentUser.Group >= UserGroup.Admin) { %>
<p>
<%= Html.Label("Icon", "GameIcon", new { @class = "label" })%>
<br />
<%= Html.TextBox("GameIcon") %>
</p>
<% } %>
<p>
<span id="GameRegions" class="label">Regions</span>
<br />

View File

@ -43,7 +43,12 @@
<div class="quote-categories">
<ul class="menu clearfix">
<li><%= Html.ActionLink(Model.Quote.Game.Name, "browse", "Quote", new { qualifiers = "game/" + Model.Quote.Game.Id }, new { @class = "quote-game-link", title = string.Format("browse quotes from the game \"{0}\"", Model.Quote.Game.Name) }) %></li>
<li>
<a class="game-link" href="<%= Url.Action("browse", "quote", new { qualifiers = "game/" + Model.Quote.Game.Id }) %>" title="browse quotes from the game &quot;<%: Model.Quote.Game.Name %>&quot;">
<img src="data:image/png;base64,<%= Model.Quote.Game.GetBase64EncodedIcon() %>" alt="icon" />
<%: Model.Quote.Game.Name %>
</a>
</li>
<% foreach (var category in Model.Quote.Categories.OrderBy(category => category.Name)) { %>
<li><%= Html.ActionLink(category.Name, "browse", "Quote", new { qualifiers = "category/" + category.Id }, new { title = string.Format("browse quotes categorized as \"{0}\"", category.Name) })%></li>
<% } %>