-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved embed video import to a dedicated service. (#8827)
- Loading branch information
1 parent
5c3d045
commit 3561be6
Showing
4 changed files
with
67 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Orchard.Web/Modules/Orchard.MediaLibrary/Services/IOEmbedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Orchard.Web/Modules/Orchard.MediaLibrary/Services/OEmbedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |