2009-11-17 03:19:46 +00:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2011-09-25 15:03:01 -07:00
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using System.Web.Routing;
|
2010-11-17 15:36:57 -08:00
|
|
|
using System.Xml.Linq;
|
2010-03-03 23:31:42 -08:00
|
|
|
using JetBrains.Annotations;
|
2009-11-18 05:36:09 +00:00
|
|
|
using Orchard.Core.XmlRpc;
|
|
|
|
|
using Orchard.Core.XmlRpc.Models;
|
2011-09-25 15:03:01 -07:00
|
|
|
using Orchard.Mvc.Extensions;
|
|
|
|
|
using Orchard.Utility.Extensions;
|
2009-11-17 03:19:46 +00:00
|
|
|
using Orchard.Security;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Media.Services {
|
2010-03-03 23:31:42 -08:00
|
|
|
[UsedImplicitly]
|
2009-11-17 03:19:46 +00:00
|
|
|
public class XmlRpcHandler : IXmlRpcHandler {
|
|
|
|
|
private readonly IMembershipService _membershipService;
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
2010-12-08 16:13:09 -08:00
|
|
|
private readonly IMediaService _mediaService;
|
2011-09-25 15:03:01 -07:00
|
|
|
private readonly RouteCollection _routeCollection;
|
2009-11-17 03:19:46 +00:00
|
|
|
|
2010-12-08 16:13:09 -08:00
|
|
|
public XmlRpcHandler(
|
|
|
|
|
IMembershipService membershipService,
|
|
|
|
|
IAuthorizationService authorizationService,
|
2011-09-25 15:03:01 -07:00
|
|
|
IMediaService mediaService,
|
|
|
|
|
RouteCollection routeCollection) {
|
2009-11-17 03:19:46 +00:00
|
|
|
_membershipService = membershipService;
|
|
|
|
|
_authorizationService = authorizationService;
|
2010-12-08 16:13:09 -08:00
|
|
|
_mediaService = mediaService;
|
2011-09-25 15:03:01 -07:00
|
|
|
_routeCollection = routeCollection;
|
2009-11-17 03:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
2010-11-17 15:36:57 -08:00
|
|
|
public void SetCapabilities(XElement options) {
|
|
|
|
|
const string manifestUri = "http://schemas.microsoft.com/wlw/manifest/weblog";
|
|
|
|
|
options.SetElementValue(XName.Get("supportsFileUpload", manifestUri), "Yes");
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-17 03:19:46 +00:00
|
|
|
public void Process(XmlRpcContext context) {
|
2011-09-25 15:03:01 -07:00
|
|
|
var urlHelper = new UrlHelper(context.ControllerContext.RequestContext, _routeCollection);
|
|
|
|
|
|
2009-11-17 03:19:46 +00:00
|
|
|
if (context.Request.MethodName == "metaWeblog.newMediaObject") {
|
|
|
|
|
var result = MetaWeblogNewMediaObject(
|
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
|
Convert.ToString(context.Request.Params[2].Value),
|
2011-09-25 15:03:01 -07:00
|
|
|
(XRpcStruct)context.Request.Params[3].Value,
|
|
|
|
|
urlHelper);
|
2009-11-17 03:19:46 +00:00
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private XRpcStruct MetaWeblogNewMediaObject(
|
|
|
|
|
string userName,
|
|
|
|
|
string password,
|
2011-09-25 15:03:01 -07:00
|
|
|
XRpcStruct file,
|
|
|
|
|
UrlHelper url) {
|
2009-11-17 03:19:46 +00:00
|
|
|
|
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
2010-12-08 12:12:49 -08:00
|
|
|
if (!_authorizationService.TryCheckAccess(Permissions.ManageMedia, user, null)) {
|
2009-11-17 03:19:46 +00:00
|
|
|
//TEMP: return appropriate access-denied response for user
|
|
|
|
|
throw new ApplicationException("Access denied");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var name = file.Optional<string>("name");
|
|
|
|
|
var bits = file.Optional<byte[]>("bits");
|
|
|
|
|
|
2010-12-08 16:13:09 -08:00
|
|
|
string publicUrl = _mediaService.UploadMediaFile(Path.GetDirectoryName(name), Path.GetFileName(name), bits, true);
|
2011-09-25 15:03:01 -07:00
|
|
|
return new XRpcStruct().Set("url", url.MakeAbsolute(publicUrl));
|
2009-11-17 03:19:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-03-03 23:31:42 -08:00
|
|
|
}
|