Improve oEmbed story by enabling the construction of oEmbed items, and the edition of the parameters that came from the oEmbed provider.

This commit is contained in:
Bertrand Le Roy
2014-12-29 16:09:39 -08:00
parent 767aa42456
commit 5fdaeaf519
2 changed files with 128 additions and 59 deletions

View File

@@ -30,11 +30,11 @@ namespace Orchard.MediaLibrary.Controllers {
[HttpPost]
[ActionName("Index")]
public ActionResult IndexPOST(string folderPath, string url, string type) {
[ValidateInput(false)]
public ActionResult IndexPOST(string folderPath, string url, string type, string title, string html, string thumbnail, string width, string height, string description) {
var viewModel = new OEmbedViewModel {
Url = url,
FolderPath = folderPath,
Type = type
FolderPath = folderPath
};
var webClient = new WebClient {Encoding = Encoding.UTF8};
@@ -64,6 +64,38 @@ namespace Orchard.MediaLibrary.Controllers {
}
}
}
if (viewModel.Content == null) {
viewModel.Content = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("oembed")
);
}
var root = viewModel.Content.Root;
if (!String.IsNullOrWhiteSpace(url)) {
root.El("url", url);
}
if (!String.IsNullOrWhiteSpace(type)) {
root.El("type", type.ToLowerInvariant());
}
if (!String.IsNullOrWhiteSpace(title)) {
root.El("title", title);
}
if (!String.IsNullOrWhiteSpace(html)) {
root.El("html", html);
}
if (!String.IsNullOrWhiteSpace(thumbnail)) {
root.El("thumbnail", thumbnail);
}
if (!String.IsNullOrWhiteSpace(width)) {
root.El("width", width);
}
if (!String.IsNullOrWhiteSpace(height)) {
root.El("height", height);
}
if (!String.IsNullOrWhiteSpace(description)) {
root.El("description", description);
}
Response.AddHeader("X-XSS-Protection", "0"); // Prevents Chrome from freaking out over embedded preview
}
catch {
return View(viewModel);
@@ -73,15 +105,11 @@ namespace Orchard.MediaLibrary.Controllers {
}
[HttpPost, ValidateInput(false)]
public ActionResult MediaPost(string folderPath, string url, string document, string type) {
public ActionResult MediaPost(string folderPath, string url, string document) {
var content = XDocument.Parse(document);
var oembed = content.Root;
if (String.IsNullOrEmpty(type)) {
type = "OEmbed";
}
var part = Services.ContentManager.New<MediaPart>(type);
var part = Services.ContentManager.New<MediaPart>("OEmbed");
part.MimeType = "text/html";
part.FolderPath = folderPath;
@@ -111,8 +139,7 @@ namespace Orchard.MediaLibrary.Controllers {
}
var viewModel = new OEmbedViewModel {
FolderPath = folderPath,
Type = type
FolderPath = folderPath
};
return View("Index", viewModel);

View File

@@ -1,6 +1,7 @@
@model Orchard.MediaLibrary.ViewModels.OEmbedViewModel
@using Orchard.ContentManagement
@model Orchard.MediaLibrary.ViewModels.OEmbedViewModel
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
@{
@@ -12,6 +13,31 @@
Style.Require("FontAwesome");
Style.Include("orchard-oembed-admin.css");
string type = null;
string url = null;
string html = null;
string title = null;
string width = null;
string height = null;
string thumbnail = null;
string description = null;
if (Model.Content != null) {
var oembed = Model.Content.Root;
type = oembed.El("type");
url = oembed.El("url");
html = oembed.El("html");
title = oembed.El("title");
width = oembed.El("width");
height = oembed.El("height");
thumbnail = oembed.Element("thumbnail") != null
? oembed.Element("thumbnail").Value
: oembed.Element("thumbnail_url") != null
? oembed.Element("thumbnail_url").Value
: null;
description = oembed.El("description");
}
}
@Display.Metas()
@@ -20,80 +46,96 @@
@Display.StyleSheetLinks()
</head>
<body>
<div id="oembed-main">
<div class="query-container">
@using (Html.BeginFormAntiForgeryPost()) {
<div id="icon">
<button type="submit" class="icon-download"></button>
</div>
<div id="query">
<input name="url" type="text" autofocus placeholder="@T("media url")" value="@Model.Url"/>
</div>
@using (Html.BeginFormAntiForgeryPost()) {
<fieldset>
<legend>@T("Please enter the URL of the embeddable media you want to integrate (requires an oEmbed compatible media provider such as YouTube):")</legend>
<div id="icon">
<button type="submit" class="icon-download">@T("Preview")</button>
</div>
<div id="query">
<input name="url" type="text" autofocus=autofocus autofocus=autofocus placeholder="@T("media url")" value="@Model.Url" />
</div>
</fieldset>
<fieldset>
<legend>@T("If that is not enough to extract the embedding information, you may manually enter the embed code:")</legend>
<div>
<label for="type">@T("Type")</label>
<select id="type" name="type">
<option value="video" @(type == "video" ? "selected" : "")>@T("Video")</option>
<option value="photo" @(type == "photo" ? "selected" : "")>@T("Photo")</option>
<option value="link" @(type == "link" ? "selected" : "")>@T("Link")</option>
<option value="rich" @(type == "rich" ? "selected" : "")>@T("Rich")</option>
</select>
</div>
<div>
<label for="title">@T("Title")</label>
<input class="text large" type="text" name="title" id="title" value="@title" />
</div>
<div>
<label for="description">@T("Description or caption")</label>
<input class="text large" type="text" name="description" id="description" value="@description" />
</div>
<div>
<label for="html">@T("Embed HTML")</label>
<input class="text large" type="text" name="html" id="html" value="@html" />
</div>
<div>
<label for="thumbnail">@T("Thumbnail URL")</label>
<input class="text large" type="text" name="thumbnail" id="thumbnail" value="@thumbnail" />
</div>
<br/>
<div>
<label for="width">@T("Width")</label>
<input class="text small" type="text" name="width" id="width" value="@width" /> @T("px")
<label for="thumbnail">@T("Height")</label>
<input class="text small" type="text" name="haight" id="height" value="@height" /> @T("px")
</div>
<br/>
<button type="submit">@T("Preview")</button>
</fieldset>
}
</div>
@if (Model.Content != null) {
var oembed = Model.Content.Root;
var type = oembed.Element("type").Value;
switch (oembed.Element("type").Value) {
switch (type) {
case "photo":
<img src="@oembed.Element("url").Value" width="@oembed.Element("width").Value" height="@oembed.Element("height").Value" />
<img src="@url" width="@width" height="@height" />
break;
case "video":
@Html.Raw(oembed.Element("html").Value)
@Html.Raw(html)
break;
case "link":
if (oembed.Element("url") != null){
<a href="@oembed.Element("url").Value">@oembed.Element("title").Value</a>
}
else {
@oembed.Element("title").Value
}
if (!String.IsNullOrWhiteSpace(url)) {
<a href="@url">@title</a>
}
else {
@title
}
break;
case "rich":
@Html.Raw(oembed.Element("html").Value)
@Html.Raw(html)
break;
}
string thumbnail = string.Empty;
if (oembed.Element("thumbnail") != null) {
thumbnail = oembed.Element("thumbnail").Value;
}
else if (oembed.Element("thumbnail_url") != null) {
thumbnail = oembed.Element("thumbnail_url").Value;
}
if (oembed.Element("html") == null && !String.IsNullOrEmpty(thumbnail)) {
if (String.IsNullOrWhiteSpace(html) && !String.IsNullOrEmpty(thumbnail)) {
<div class="media-thumbnail">
<img src="@thumbnail" />
</div>
}
using (Html.BeginFormAntiForgeryPost(Url.Action("MediaPost"))) {
using (Html.BeginFormAntiForgeryPost(Url.Action("MediaPost"))) {
@Html.Hidden("url", Model.Url)
@Html.Hidden("folderPath", Model.FolderPath)
@Html.Hidden("type", Model.Type)
@Html.Hidden("document", Model.Content.ToString())
<br/>
<button type="submit">@("Import")</button>
}
}
}
</div>
@using (Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
$(function () {
})
//]]>
</script>
}
@Display.FootScripts()
</body>
</html>