Moved embed video import to a dedicated service. (#8827)
Some checks failed
Compile / Compile .NET solution (push) Has been cancelled
Compile / Compile Client-side Assets (push) Has been cancelled

This commit is contained in:
Andrea Piovanelli 2025-02-04 12:55:39 +01:00 committed by GitHub
parent 5c3d045aa1
commit 3561be6e37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 67 additions and 33 deletions

View File

@ -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;
}
@ -78,39 +82,9 @@ namespace Orchard.MediaLibrary.Controllers {
}
}
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">
viewModel.Content = _oEmbedService.DownloadMediaData(url);
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(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);

View File

@ -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" />

View File

@ -0,0 +1,7 @@
using System.Xml.Linq;
namespace Orchard.MediaLibrary.Services {
public interface IOEmbedService : IDependency {
XDocument DownloadMediaData(string url);
}
}

View File

@ -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;
}
}
}