add basic ability to upload images for items

This commit is contained in:
Luke Pulverenti 2013-05-04 17:20:27 -04:00
parent 055a6eb4cc
commit 00222c8493
6 changed files with 111 additions and 2 deletions

View File

@ -160,6 +160,25 @@ namespace MediaBrowser.Api.Images
public Stream RequestStream { get; set; }
}
[Route("/Items/{Id}/Images/{Type}", "POST")]
[Route("/Items/{Id}/Images/{Type}/{Index}", "POST")]
[Api(Description = "Posts an item image")]
public class PostItemImage : DeleteImageRequest, IRequiresRequestStream, IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Id { get; set; }
/// <summary>
/// The raw Http Request Input Stream
/// </summary>
/// <value>The request stream.</value>
public Stream RequestStream { get; set; }
}
/// <summary>
/// Class ImageService
/// </summary>
@ -288,6 +307,24 @@ namespace MediaBrowser.Api.Images
Task.WaitAll(task);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(PostItemImage request)
{
var pathInfo = PathInfo.Parse(RequestContext.PathInfo);
var id = new Guid(pathInfo.GetArgumentValue<string>(1));
request.Type = (ImageType)Enum.Parse(typeof(ImageType), pathInfo.GetArgumentValue<string>(3), true);
var item = _libraryManager.GetItemById(id);
var task = PostImage(item, request.RequestStream, request.Type, RequestContext.ContentType);
Task.WaitAll(task);
}
/// <summary>
/// Deletes the specified request.
/// </summary>

View File

@ -197,7 +197,7 @@ namespace MediaBrowser.Controller.Providers
item.SetImage(ImageType.Thumb, image.FullName);
}
// Thumbnail Image
// Box Image
image = GetImage(item, "box");
if (image != null)
@ -205,6 +205,14 @@ namespace MediaBrowser.Controller.Providers
item.SetImage(ImageType.Box, image.FullName);
}
// BoxRear Image
image = GetImage(item, "boxrear");
if (image != null)
{
item.SetImage(ImageType.BoxRear, image.FullName);
}
// Thumbnail Image
image = GetImage(item, "menu");

View File

@ -455,6 +455,7 @@ namespace MediaBrowser.WebDashboard.Api
"boxsets.js",
"clientsettings.js",
"dashboardpage.js",
"edititemimages.js",
"edituserpage.js",
"gamesrecommendedpage.js",
"gamesystemspage.js",

View File

@ -839,6 +839,63 @@ MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
return deferred.promise();
};
self.uploadImage = function (itemId, imageType, file) {
if (!itemId) {
throw new Error("null itemId");
}
if (!imageType) {
throw new Error("null imageType");
}
if (!file) {
throw new Error("File must be an image.");
}
if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
throw new Error("File must be an image.");
}
var deferred = $.Deferred();
var reader = new FileReader();
reader.onerror = function () {
deferred.reject();
};
reader.onabort = function () {
deferred.reject();
};
// Closure to capture the file information.
reader.onload = function (e) {
var data = window.btoa(e.target.result);
var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
self.ajax({
type: "POST",
url: url,
data: data,
contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
}).done(function (result) {
deferred.resolveWith(null, [result]);
}).fail(function () {
deferred.reject();
});
};
// Read in the image file as a data URL.
reader.readAsBinaryString(file);
return deferred.promise();
};
/**
* Gets the list of installed plugins on the server
*/

View File

@ -210,6 +210,9 @@
<Content Include="dashboard-ui\css\userimage.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\edititemimages.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\gamegenres.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
@ -270,6 +273,9 @@
<Content Include="dashboard-ui\musicrecommended.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\scripts\edititemimages.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="dashboard-ui\scripts\musicrecommended.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MediaBrowser.ApiClient.Javascript" version="3.0.93" targetFramework="net45" />
<package id="MediaBrowser.ApiClient.Javascript" version="3.0.94" targetFramework="net45" />
<package id="ServiceStack.Common" version="3.9.43" targetFramework="net45" />
<package id="ServiceStack.Text" version="3.9.43" targetFramework="net45" />
</packages>