2009-11-17 03:19:46 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Web;
|
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;
|
2009-11-17 03:19:46 +00:00
|
|
|
using Orchard.Security;
|
2010-05-17 11:59:58 -07:00
|
|
|
using Orchard.Utility.Extensions;
|
2009-11-17 03:19:46 +00:00
|
|
|
|
|
|
|
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-03-03 23:31:42 -08:00
|
|
|
public XmlRpcHandler(IMembershipService membershipService, IAuthorizationService authorizationService) {
|
2009-11-17 03:19:46 +00:00
|
|
|
_membershipService = membershipService;
|
|
|
|
_authorizationService = authorizationService;
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-05-17 11:59:58 -07:00
|
|
|
var uriBuilder = new UriBuilder(context.HttpContext.Request.ToUrlString()) {
|
2009-11-17 03:19:46 +00:00
|
|
|
Path = context.HttpContext.Request.ApplicationPath,
|
|
|
|
Query = string.Empty
|
|
|
|
};
|
|
|
|
|
|
|
|
if (context.Request.MethodName == "metaWeblog.newMediaObject") {
|
|
|
|
var result = MetaWeblogNewMediaObject(
|
|
|
|
uriBuilder,
|
|
|
|
Convert.ToString(context.Request.Params[0].Value),
|
|
|
|
Convert.ToString(context.Request.Params[1].Value),
|
|
|
|
Convert.ToString(context.Request.Params[2].Value),
|
|
|
|
(XRpcStruct)context.Request.Params[3].Value);
|
|
|
|
context.Response = new XRpcMethodResponse().Add(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private XRpcStruct MetaWeblogNewMediaObject(
|
|
|
|
UriBuilder uriBuilder,
|
|
|
|
string blogId,
|
|
|
|
string userName,
|
|
|
|
string password,
|
|
|
|
XRpcStruct file) {
|
|
|
|
|
|
|
|
var user = _membershipService.ValidateUser(userName, password);
|
2010-01-22 22:11:10 +00:00
|
|
|
if (!_authorizationService.TryCheckAccess(Permissions.UploadMediaFiles, 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");
|
|
|
|
|
2009-11-25 02:09:52 +00:00
|
|
|
var target = HttpContext.Current.Server.MapPath("~/Media/" + name);
|
2009-11-17 03:19:46 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(target));
|
|
|
|
using (var stream = new FileStream(target, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) {
|
|
|
|
stream.Write(bits, 0, bits.Length);
|
|
|
|
}
|
|
|
|
|
2009-11-25 02:09:52 +00:00
|
|
|
uriBuilder.Path = uriBuilder.Path.TrimEnd('/') + "/Media/" + name.TrimStart('/');
|
2009-11-17 03:19:46 +00:00
|
|
|
return new XRpcStruct().Set("url", uriBuilder.Uri.AbsoluteUri);
|
|
|
|
}
|
|
|
|
}
|
2010-03-03 23:31:42 -08:00
|
|
|
}
|