Skip to content

Instantly share code, notes, and snippets.

@sniffdk
Created July 11, 2012 19:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sniffdk/3092401 to your computer and use it in GitHub Desktop.
Save sniffdk/3092401 to your computer and use it in GitHub Desktop.
Umbraco - GetTinyMceMediaStringFromId
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