Some more work on being able to add media from the (TinyMCE) editor

- TinyMCE module takes a dependency on the current media module api with the addmedia plugin which is functional to the point of being able to add media*

* it still needs a setting of where the media should go (current spec and xmlrpc api say <blog slug>/<post slug> for a blog post but (1) I don't even have that context from the editor and (2) what about other content types)
* it (the plugin) still needs to do an async post and handle the returned JSON appropriatly (insert image tag into the editor using returned url and close the dialog otherwise present any given error)

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-03-04 02:58:49 -08:00
parent d1f9fad499
commit f2df4df51c
10 changed files with 62 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Orchard.Localization;
@@ -162,18 +163,27 @@ namespace Orchard.Media.Controllers {
var viewModel = new MediaItemAddViewModel();
try {
UpdateModel(viewModel);
if (!Services.Authorizer.Authorize(Permissions.UploadMediaFiles, T("Couldn't upload media file")))
return Json(T("ERROR: You don't have permission to upload media files"));
if (!Services.Authorizer.Authorize(Permissions.UploadMediaFiles))
return Json(new { error = T("ERROR: You don't have permission to upload media files").ToString() });
foreach (string fileName in Request.Files) {
HttpPostedFileBase file = Request.Files[fileName];
_mediaService.UploadMediaFile(viewModel.MediaPath, file);
if (Request.Files.Count < 1 || Request.Files[0].ContentLength == 0)
return Json(new { error = T("HEY: You didn't give me a file to upload").ToString() });
try {
_mediaService.GetMediaFiles(viewModel.MediaPath);
}
catch //media api needs a little work, like everything else of course ;)
{
_mediaService.CreateFolder(viewModel.MediaPath, "");
}
return Json(viewModel.MediaPath);
var file = Request.Files[0];
_mediaService.UploadMediaFile(viewModel.MediaPath, file);
return Json(new { url = Path.Combine(_mediaService.GetRootUrl(), string.Format("{0}/{1}", viewModel.MediaPath, file.FileName)).Replace("\\", "/") });
}
catch (Exception exception) {
return Json("ERROR: Uploading media file failed: " + exception.Message);
return Json(new { error = T("ERROR: Uploading media file failed: {0}", exception.Message).ToString() });
}
}

View File

@@ -4,6 +4,7 @@ using Orchard.Media.Models;
namespace Orchard.Media.Services {
public interface IMediaService : IDependency {
string GetRootUrl();
IEnumerable<MediaFolder> GetMediaFolders(string path);
IEnumerable<MediaFile> GetMediaFiles(string path);
void CreateFolder(string path, string name);

View File

@@ -14,16 +14,22 @@ namespace Orchard.Media.Services {
public class MediaService : IMediaService {
private readonly IStorageProvider _storageProvider;
private readonly string _rootPath;
private readonly string _rootUrl;
public MediaService (
IStorageProvider storageProvider) {
_storageProvider = storageProvider;
_rootPath = HttpContext.Current.Server.MapPath("~/Media");
_rootUrl = Path.Combine(HttpContext.Current.Request.ApplicationPath, "Media").Replace("\\", "/");
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
public string GetRootUrl() {
return _rootUrl;
}
public IEnumerable<MediaFolder> GetMediaFolders(string path) {
var mediaFolders = new List<MediaFolder>();
var folders = (

View File

@@ -6,7 +6,7 @@
<script type="text/javascript" src="js/addmedia.js"></script>
</head>
<body>
<form action="/Admin/Media/Add" enctype="multipart/form-data" method="post">
<form enctype="multipart/form-data" method="post">
<div class="tabs">
<ul>
<li id="general_tab" class="current"><span><a href="#general_tab">{#addmedia_dlg.title}</a></span></li>
@@ -16,15 +16,14 @@
<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td class="nowrap"><label for="pageTitle">{#addmedia_dlg.path_label}</label></td>
<td><input id="MediaItemPath" name="MediaItemPath" type="file" class="text" value="Browse" size="64"/></td>
<td><input id="MediaItemPath" name="MediaItemPath" type="file" class="text" size="64" /></td>
</tr>
</table>
<input type="hidden" id="FolderName" name="FolderName" value="foo" />
<input type="hidden" id="MediaPath" name="MediaPath" value="foo" />
<input type="hidden" id="MediaPath" name="MediaPath" />
</div>
<div class="mceActionPanel">
<div style="float:left">
<input name="__RequestVerificationToken" type="hidden" value="7lDAM51YOmke7sRNx/GFjVMKaPf8QZNh7qZiEdQyNQXKKbw1DYehU/g6jNcAx6I3OGD/KDuaNlmyNIeQ/69CFpDizom9bROgo5keYyj+HcAKHtjQTRV5kNJm6SztNLwO" />
<input name="__RequestVerificationToken" type="hidden" />
<input type="submit" name="insert" value="{#insert}" id="insert" />
</div>
<div style="float:right">

View File

@@ -1,6 +1,6 @@
(function() {
// Load plugin specific language pack
tinymce.PluginManager.requireLangPack('dlg');
tinymce.PluginManager.requireLangPack("addmedia");
tinymce.create('tinymce.plugins.Orchard.AddMedia', {
/**
@@ -16,7 +16,7 @@
ed.addCommand('mceAddMedia', function() {
ed.windowManager.open({
file: url + '/addmedia.htm',
width: 480 + parseInt(ed.getLang('addmedia.delta_width', 0)),
width: 550 + parseInt(ed.getLang('addmedia.delta_width', 0)),
height: 110 + parseInt(ed.getLang('addmedia.delta_height', 0)),
inline: 1
}, {
@@ -26,7 +26,7 @@
// Register example button
ed.addButton('addmedia', {
title: 'addmedia_desc',
title: ed.getLang('addmedia.title'),
cmd: 'mceAddMedia',
image: url + '/img/picture_add.png'
});

View File

@@ -1 +1,12 @@
tinyMCEPopup.requireLangPack();
tinyMCEPopup.requireLangPack("addmedia");
var AddMediaDialog = {
init: function() {
var form = document.forms[0];
form.action = tinyMCE.activeEditor.getParam('addmedia_action');
form.MediaPath.value = tinyMCE.activeEditor.getParam('addmedia_path');
form.__RequestVerificationToken.value = tinyMCE.activeEditor.getParam('request_verification_token');
}
};
tinyMCEPopup.onInit.add(AddMediaDialog.init, AddMediaDialog);

View File

@@ -0,0 +1,6 @@
tinyMCE.addI18n({ en: {
addmedia: {
title: "Add Media"
}
}
});

View File

@@ -1,4 +1,9 @@
tinyMCE.addI18n('en.addmedia_dlg',{
title:"Add Media",
path_label:"Media File Path"
tinyMCE.addI18n({ en: {
addmedia_dlg: {
button_title: "Add Media",
title: "Add Media",
path_label: "Media File Path",
browse_button_text: "Browse"
}
}
});

View File

@@ -140,6 +140,7 @@
<Content Include="Scripts\plugins\addmedia\img\picture_add.png" />
<Content Include="Scripts\plugins\addmedia\js\addmedia.js" />
<Content Include="Scripts\plugins\addmedia\langs\en_dlg.js" />
<Content Include="Scripts\plugins\addmedia\langs\en.js" />
<Content Include="Scripts\plugins\searchreplace\css\searchreplace.css" />
<Content Include="Scripts\plugins\searchreplace\editor_plugin.js" />
<Content Include="Scripts\plugins\searchreplace\editor_plugin_src.js" />

View File

@@ -12,7 +12,10 @@
theme_advanced_toolbar_align: "left",
theme_advanced_buttons1: "search,replace,|,cut,copy,paste,|,undo,redo,|,image,addmedia,|,link,unlink,charmap,emoticon,codeblock,|,bold,italic,|,numlist,bullist,formatselect,|,code,fullscreen",
theme_advanced_buttons2: "",
theme_advanced_buttons3: ""
theme_advanced_buttons3: "",
addmedia_action: "<%=Url.Action("AddFromClient", "Admin", new {area = "Orchard.Media"}) %>",
addmedia_path: "some/folder",
request_verification_token: "<%=Html.AntiForgeryTokenValueOrchard() %>"
});
</script>
<%}%>