Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to import Vimeo videos #8804

Closed
wants to merge 7 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,54 @@ public ActionResult IndexPOST(string folderPath, string url, string type, string
try {
// <link rel="alternate" href="http://vimeo.com/api/oembed.xml?url=http%3A%2F%2Fvimeo.com%2F23608259" type="text/xml+oembed">

Comment on lines 83 to 84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// <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(Server.HtmlDecode(href));
viewModel.Content = XDocument.Parse(content);
}
catch {
// bubble exception
var source = "";

// Get the proper uri the provider redirects to
var uri = GetRedirectUri(url);

// Vimeo doesn't consent anymore the scraping of web pages, so the direct api call has to be enforced.
// In this case, the downloaded string is already the expected xml, in the format that needs to be parsed.
// Legacy process is done for non-Vimeo content.
// First of all, url domain is checked.
var vimeo = uri.Host.Equals("vimeo.com", StringComparison.OrdinalIgnoreCase);

// Youtube changed the markup of the page of its videos, so the direct api call has to be enforced
// Api url is built based on the requested video
var youtube = uri.Host.Equals("www.youtube.com", StringComparison.OrdinalIgnoreCase);

if (vimeo) {
// Add api url to original url provided as a parameter
url = "https://" + uri.Host + "/api/oembed.xml?url=" + url;
source = webClient.DownloadString(url);

viewModel.Content = XDocument.Parse(source);
} else if (youtube) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (youtube) {
}
else if (youtube) {

// Add api url to original url provided as a parameter
url = "https://" + uri.Host + "/oembed?format=xml&url=" + url;
source = webClient.DownloadString(url);

viewModel.Content = XDocument.Parse(source);
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
}
else {

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch {
}
catch {

// bubble exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// bubble exception
throw;

}
}
}
}
Expand Down Expand Up @@ -137,8 +165,7 @@ public ActionResult IndexPOST(string folderPath, string url, string type, string
root.El("description", description);
}
Response.AddHeader("X-XSS-Protection", "0"); // Prevents Chrome from freaking out over embedded preview
}
catch {
} catch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch {
}
catch {

return View(viewModel);
}

Expand Down Expand Up @@ -167,8 +194,7 @@ public ActionResult Import(string folderPath, string url, string document) {

if (oembed.Element("title") != null) {
part.Title = oembed.Element("title").Value;
}
else {
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
}
else {

part.Title = oembed.Element("url").Value;
}
if (oembed.Element("description") != null) {
Expand Down Expand Up @@ -203,7 +229,7 @@ public ActionResult Replace(int replaceId, string url, string document) {
return HttpNotFound();

// Check permission
if (!(_mediaLibraryService.CheckMediaFolderPermission(Permissions.EditMediaContent, replaceMedia.FolderPath) && _mediaLibraryService.CheckMediaFolderPermission(Permissions.ImportMediaContent, replaceMedia.FolderPath))
if (!(_mediaLibraryService.CheckMediaFolderPermission(Permissions.EditMediaContent, replaceMedia.FolderPath) && _mediaLibraryService.CheckMediaFolderPermission(Permissions.ImportMediaContent, replaceMedia.FolderPath))
&& !_mediaLibraryService.CanManageMediaFolder(replaceMedia.FolderPath)) {
return new HttpUnauthorizedResult();
}
Expand All @@ -213,8 +239,7 @@ public ActionResult Replace(int replaceId, string url, string document) {

if (oembed.Element("title") != null) {
replaceMedia.Title = oembed.Element("title").Value;
}
else {
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
}
else {

replaceMedia.Title = oembed.Element("url").Value;
}
if (oembed.Element("description") != null) {
Expand All @@ -238,5 +263,22 @@ public ActionResult Replace(int replaceId, string url, string document) {

return RedirectToAction("Index", new { folderPath = replaceMedia.FolderPath, replaceId = replaceMedia.Id });
}

private Uri GetRedirectUri(string url) {
Uri myUri = new Uri(url);
// Create a 'HttpWebRequest' object for the specified url.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
// Send the request and wait for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

if (!myUri.Equals(myHttpWebResponse.ResponseUri)) {
myUri = myHttpWebResponse.ResponseUri;
}

// Release resources of response object.
myHttpWebResponse.Close();
Comment on lines +270 to +279
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
// Send the request and wait for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
if (!myUri.Equals(myHttpWebResponse.ResponseUri)) {
myUri = myHttpWebResponse.ResponseUri;
}
// Release resources of response object.
myHttpWebResponse.Close();
var myHttpWebRequest = WebRequest.Create(myUri) as HttpWebRequest;
// Send the request and wait for response.
using (var myHttpWebResponse = myHttpWebRequest.GetResponse() as HttpWebResponse) {
if (!myUri.Equals(myHttpWebResponse.ResponseUri)) {
myUri = myHttpWebResponse.ResponseUri;
}
}


return myUri;
}
}
}