Skip to content

Commit

Permalink
Moved embed video import to a dedicated service. (#8827)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaPiovanelli authored Feb 4, 2025
1 parent 5c3d045 commit 3561be6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ namespace Orchard.MediaLibrary.Controllers {
[Admin, Themed(false)]
public class OEmbedController : Controller {
private readonly IMediaLibraryService _mediaLibraryService;
private readonly IOEmbedService _oEmbedService;

public OEmbedController(
IOrchardServices services,
IMediaLibraryService mediaManagerService) {
_mediaLibraryService = mediaManagerService;
IMediaLibraryService mediaManagerService,
IOEmbedService oEmbedService) {

Services = services;
_mediaLibraryService = mediaManagerService;
_oEmbedService = oEmbedService;
T = NullLocalizer.Instance;
}

Expand Down Expand Up @@ -78,39 +82,9 @@ public ActionResult IndexPOST(string folderPath, string url, string type, string
}
}

var webClient = new WebClient { Encoding = Encoding.UTF8 };
try {
// <link rel="alternate" href="http://vimeo.com/api/oembed.xml?url=http%3A%2F%2Fvimeo.com%2F23608259" type="text/xml+oembed">

var source = webClient.DownloadString(url);
viewModel.Content = _oEmbedService.DownloadMediaData(url);

// seek type="text/xml+oembed" or application/xml+oembed
var oembedSignature = source.IndexOf("type=\"text/xml+oembed\"", StringComparison.OrdinalIgnoreCase);
if (oembedSignature == -1) {
oembedSignature = source.IndexOf("type=\"application/xml+oembed\"", StringComparison.OrdinalIgnoreCase);
}
if (oembedSignature != -1) {
var tagStart = source.Substring(0, oembedSignature).LastIndexOf('<');
var tagEnd = source.IndexOf('>', oembedSignature);
var tag = source.Substring(tagStart, tagEnd - tagStart);
var matches = new Regex("href=\"([^\"]+)\"").Matches(tag);
if (matches.Count > 0) {
var href = matches[0].Groups[1].Value;
try {
var content = webClient.DownloadString(Server.HtmlDecode(href));
viewModel.Content = XDocument.Parse(content);
}
catch {
// bubble exception
}
}
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@
<Compile Include="Security\MediaAuthorizationEventHandler.cs" />
<Compile Include="Services\IMediaLibraryService.cs" />
<Compile Include="Providers\IMediaFolderProvider.cs" />
<Compile Include="Services\IOEmbedService.cs" />
<Compile Include="Services\MediaLibraryService.cs" />
<Compile Include="Services\OEmbedService.cs" />
<Compile Include="Services\Shapes.cs" />
<Compile Include="Services\XmlRpcHandler.cs" />
<Compile Include="Settings\MediaLibraryPickerFieldEditorEvents.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Xml.Linq;

namespace Orchard.MediaLibrary.Services {
public interface IOEmbedService : IDependency {
XDocument DownloadMediaData(string url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml.Linq;

namespace Orchard.MediaLibrary.Services {
public class OEmbedService : IOEmbedService {
public XDocument DownloadMediaData(string url) {
var webClient = new WebClient { Encoding = Encoding.UTF8 };
XDocument doc = null;
try {
// <link rel="alternate" href="http://vimeo.com/api/oembed.xml?url=http%3A%2F%2Fvimeo.com%2F23608259" type="text/xml+oembed">
var source = webClient.DownloadString(url);

// seek type="text/xml+oembed" or application/xml+oembed
var oembedSignature = source.IndexOf("type=\"text/xml+oembed\"", StringComparison.OrdinalIgnoreCase);
if (oembedSignature == -1) {
oembedSignature = source.IndexOf("type=\"application/xml+oembed\"", StringComparison.OrdinalIgnoreCase);
}
if (oembedSignature != -1) {
var tagStart = source.Substring(0, oembedSignature).LastIndexOf('<');
var tagEnd = source.IndexOf('>', oembedSignature);
var tag = source.Substring(tagStart, tagEnd - tagStart);
var matches = new Regex("href=\"([^\"]+)\"").Matches(tag);
if (matches.Count > 0) {
var href = matches[0].Groups[1].Value;
try {
var content = webClient.DownloadString(HttpUtility.HtmlDecode(href));
doc = XDocument.Parse(content);
} catch {
// bubble exception
}
}
}
} catch {
doc = null;
}

if (doc == null) {
doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("oembed")
);
}

return doc;
}
}
}

0 comments on commit 3561be6

Please sign in to comment.