Created
July 11, 2012 19:03
-
-
Save sniffdk/3092401 to your computer and use it in GitHub Desktop.
Umbraco - GetTinyMceMediaStringFromId
This file contains hidden or 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
public static class StaticRegexes | |
{ | |
... | |
public static readonly Regex LocalMediaRegex = new Regex(@"/{localMedia:(\d+?)}", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); | |
} | |
public class NodeServiceController : Controller | |
{ | |
[HttpGet] | |
public JsonResult GetTinyMceMediaStringFromId(string id) | |
{ | |
int nodeId; | |
if (!int.TryParse(id, out nodeId)) | |
{ | |
return Json(id, JsonRequestBehavior.AllowGet); | |
} | |
var node = LuceneNode.Load(nodeId, NodeVersion.Published); | |
if (node == null) | |
{ | |
return Json("", JsonRequestBehavior.AllowGet); | |
} | |
var mediaItem = new MediaItem(node); | |
var mediaString = "{localMedia:" + nodeId + "}|" + mediaItem.Path + "|" + mediaItem.Name; | |
return Json(mediaString, JsonRequestBehavior.AllowGet); | |
} | |
[HttpGet] | |
public JsonResult GetTinyMceMediaStringFromLink(string link) | |
{ | |
var matchLocalMedia = StaticRegexes.LocalMediaRegex.Match(link); | |
if (matchLocalMedia.Success) | |
{ | |
int id; | |
if (int.TryParse(matchLocalMedia.Groups[1].Value, out id)) | |
{ | |
var node = LuceneNode.Load(id); | |
if (node != null) | |
{ | |
var media = new MediaItem(node); | |
return Json(new | |
{ | |
media.Path, | |
Url = "/{localMedia:" + id + "}" | |
}, JsonRequestBehavior.AllowGet); | |
} | |
} | |
} | |
return Json("", JsonRequestBehavior.AllowGet); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment