2018-02-27 15:47:47 +01:00
using System ;
2019-01-29 15:30:25 +01:00
using System.Linq ;
2018-09-10 11:34:31 +10:00
using System.Web ;
2018-02-27 15:47:47 +01:00
using System.Web.Mvc ;
2018-09-14 00:14:03 +10:00
using System.Web.UI ;
2018-03-14 12:55:34 +11:00
using Umbraco.Core ;
using Umbraco.Core.Configuration ;
2020-01-21 17:03:46 -08:00
using Umbraco.Core.Configuration.UmbracoSettings ;
2020-02-13 07:56:50 +01:00
using Umbraco.Core.Hosting ;
2020-02-10 11:23:23 +01:00
using Umbraco.Core.IO ;
2019-01-29 15:30:25 +01:00
using Umbraco.Core.Services ;
2018-09-10 11:34:31 +10:00
using Umbraco.Web.Composing ;
2018-02-27 15:47:47 +01:00
using Umbraco.Web.Features ;
2019-01-29 23:05:59 +11:00
using Umbraco.Web.JavaScript ;
2018-03-14 13:56:49 +11:00
using Umbraco.Web.Models.ContentEditing ;
2018-02-27 15:47:47 +01:00
using Umbraco.Web.Mvc ;
2018-09-10 11:34:31 +10:00
using Umbraco.Web.PublishedCache ;
2020-02-10 11:23:23 +01:00
using Umbraco.Web.Trees ;
2018-09-10 11:34:31 +10:00
using Constants = Umbraco . Core . Constants ;
2018-02-27 15:47:47 +01:00
namespace Umbraco.Web.Editors
{
[DisableBrowserCache]
public class PreviewController : Controller
{
2018-04-05 12:50:00 +02:00
private readonly UmbracoFeatures _features ;
2018-04-06 13:51:54 +10:00
private readonly IGlobalSettings _globalSettings ;
2018-09-10 11:34:31 +10:00
private readonly IPublishedSnapshotService _publishedSnapshotService ;
2019-02-14 12:40:45 +01:00
private readonly IUmbracoContextAccessor _umbracoContextAccessor ;
2019-01-29 15:30:25 +01:00
private readonly ILocalizationService _localizationService ;
2019-11-20 10:50:15 +01:00
private readonly IUmbracoVersion _umbracoVersion ;
2020-01-21 17:03:46 -08:00
private readonly IUmbracoSettingsSection _umbracoSettingsSection ;
2020-02-10 11:23:23 +01:00
private readonly IIOHelper _ioHelper ;
private readonly TreeCollection _treeCollection ;
2020-02-13 07:56:50 +01:00
private readonly IHttpContextAccessor _httpContextAccessor ;
private readonly IHostingEnvironment _hostingEnvironment ;
2018-04-05 12:50:00 +02:00
2019-01-29 15:30:25 +01:00
public PreviewController (
UmbracoFeatures features ,
IGlobalSettings globalSettings ,
IPublishedSnapshotService publishedSnapshotService ,
2019-02-14 12:40:45 +01:00
IUmbracoContextAccessor umbracoContextAccessor ,
2019-11-20 10:50:15 +01:00
ILocalizationService localizationService ,
2020-01-21 17:03:46 -08:00
IUmbracoVersion umbracoVersion ,
2020-02-10 11:23:23 +01:00
IUmbracoSettingsSection umbracoSettingsSection ,
IIOHelper ioHelper ,
2020-02-13 07:56:50 +01:00
TreeCollection treeCollection ,
IHttpContextAccessor httpContextAccessor ,
IHostingEnvironment hostingEnvironment )
2018-04-05 12:50:00 +02:00
{
_features = features ;
2018-04-06 13:51:54 +10:00
_globalSettings = globalSettings ;
2018-09-10 11:34:31 +10:00
_publishedSnapshotService = publishedSnapshotService ;
2019-02-14 12:40:45 +01:00
_umbracoContextAccessor = umbracoContextAccessor ;
2019-01-29 15:30:25 +01:00
_localizationService = localizationService ;
2019-11-20 10:50:15 +01:00
_umbracoVersion = umbracoVersion ;
2020-01-21 17:03:46 -08:00
_umbracoSettingsSection = umbracoSettingsSection ? ? throw new ArgumentNullException ( nameof ( umbracoSettingsSection ) ) ;
2020-02-10 11:23:23 +01:00
_ioHelper = ioHelper ? ? throw new ArgumentNullException ( nameof ( ioHelper ) ) ;
_treeCollection = treeCollection ;
2020-02-13 07:56:50 +01:00
_httpContextAccessor = httpContextAccessor ;
_hostingEnvironment = hostingEnvironment ;
2018-04-05 12:50:00 +02:00
}
2018-03-22 19:55:55 +11:00
[UmbracoAuthorize(redirectToUmbracoLogin: true)]
2018-09-14 00:14:03 +10:00
[DisableBrowserCache]
2018-02-27 15:47:47 +01:00
public ActionResult Index ( )
{
2019-01-29 15:30:25 +01:00
var availableLanguages = _localizationService . GetAllLanguages ( ) ;
2020-02-13 07:56:50 +01:00
var model = new BackOfficePreviewModel ( _features , _globalSettings , _umbracoVersion , availableLanguages , _umbracoSettingsSection , _ioHelper , _treeCollection , _httpContextAccessor , _hostingEnvironment ) ;
2018-03-14 13:56:49 +11:00
2018-03-14 15:24:30 +11:00
if ( model . PreviewExtendedHeaderView . IsNullOrWhiteSpace ( ) = = false )
2018-03-14 13:56:49 +11:00
{
2018-03-14 15:24:30 +11:00
var viewEngineResult = ViewEngines . Engines . FindPartialView ( ControllerContext , model . PreviewExtendedHeaderView ) ;
2018-03-14 13:56:49 +11:00
if ( viewEngineResult . View = = null )
{
2018-03-14 15:24:30 +11:00
throw new InvalidOperationException ( "Could not find the view " + model . PreviewExtendedHeaderView + ", the following locations were searched: " + Environment . NewLine + string . Join ( Environment . NewLine , viewEngineResult . SearchedLocations ) ) ;
2018-03-14 13:56:49 +11:00
}
2018-02-28 13:41:49 +01:00
}
2018-03-14 13:56:49 +11:00
2018-04-06 13:51:54 +10:00
return View ( _globalSettings . Path . EnsureEndsWith ( '/' ) + "Views/Preview/" + "Index.cshtml" , model ) ;
2018-02-27 15:47:47 +01:00
}
2018-09-14 00:14:03 +10:00
/// <summary>
/// Returns the JavaScript file for preview
/// </summary>
/// <returns></returns>
[MinifyJavaScriptResult(Order = 0)]
[OutputCache(Order = 1, VaryByParam = "none", Location = OutputCacheLocation.Server, Duration = 5000)]
public JavaScriptResult Application ( )
{
var files = JsInitialization . OptimizeScriptFiles ( HttpContext , JsInitialization . GetPreviewInitialization ( ) ) ;
2020-02-10 11:23:23 +01:00
var result = JsInitialization . GetJavascriptInitialization ( HttpContext , files , "umbraco.preview" , _globalSettings , _ioHelper ) ;
2018-09-14 00:14:03 +10:00
return JavaScript ( result ) ;
}
2018-09-10 11:34:31 +10:00
/// <summary>
/// The endpoint that is loaded within the preview iframe
/// </summary>
/// <returns></returns>
[UmbracoAuthorize]
2018-09-14 00:44:36 +10:00
public ActionResult Frame ( int id , string culture )
2018-09-10 11:34:31 +10:00
{
2019-02-14 12:40:45 +01:00
var user = _umbracoContextAccessor . UmbracoContext . Security . CurrentUser ;
2018-09-10 11:34:31 +10:00
var previewToken = _publishedSnapshotService . EnterPreview ( user , id ) ;
Response . Cookies . Set ( new HttpCookie ( Constants . Web . PreviewCookieName , previewToken ) ) ;
// use a numeric url because content may not be in cache and so .Url would fail
2018-09-14 00:44:36 +10:00
var query = culture . IsNullOrWhiteSpace ( ) ? string . Empty : $"?culture={culture}" ;
Response . Redirect ( $"../../{id}.aspx{query}" , true ) ;
2018-09-10 11:34:31 +10:00
return null ;
}
2018-12-14 13:11:36 +00:00
public ActionResult End ( string redir = null )
{
var previewToken = Request . GetPreviewCookieValue ( ) ;
var service = Current . PublishedSnapshotService ;
service . ExitPreview ( previewToken ) ;
System . Web . HttpContext . Current . ExpireCookie ( Constants . Web . PreviewCookieName ) ;
if ( Uri . IsWellFormedUriString ( redir , UriKind . Relative )
& & redir . StartsWith ( "//" ) = = false
& & Uri . TryCreate ( redir , UriKind . Relative , out Uri url ) )
{
return Redirect ( url . ToString ( ) ) ;
}
return Redirect ( "/" ) ;
}
2018-02-27 15:47:47 +01:00
}
}