Files
Umbraco-CMS/src/Umbraco.ModelsBuilder.Embedded/OutOfDateModelsStatus.cs

67 lines
2.0 KiB
C#
Raw Normal View History

2019-06-24 11:58:36 +02:00
using System.IO;
using Microsoft.Extensions.Options;
using Umbraco.Core.Configuration;
2020-09-02 14:44:01 +02:00
using Umbraco.Core.Hosting;
using Umbraco.Core.Configuration.Models;
2019-06-24 11:58:36 +02:00
using Umbraco.Web.Cache;
namespace Umbraco.ModelsBuilder.Embedded
2019-06-24 11:58:36 +02:00
{
public sealed class OutOfDateModelsStatus
{
private readonly ModelsBuilderSettings _config;
2020-09-02 14:44:01 +02:00
private readonly IHostingEnvironment _hostingEnvironment;
public OutOfDateModelsStatus(IOptions<ModelsBuilderSettings> config, IHostingEnvironment hostingEnvironment)
{
_config = config.Value;
2020-09-02 14:44:01 +02:00
_hostingEnvironment = hostingEnvironment;
}
internal void Install()
2019-06-24 11:58:36 +02:00
{
// just be sure
if (_config.FlagOutOfDateModels == false)
2019-06-24 11:58:36 +02:00
return;
ContentTypeCacheRefresher.CacheUpdated += (sender, args) => Write();
DataTypeCacheRefresher.CacheUpdated += (sender, args) => Write();
}
private string GetFlagPath()
2019-06-24 11:58:36 +02:00
{
2020-09-02 14:44:01 +02:00
var modelsDirectory = _config.ModelsDirectoryAbsolute(_hostingEnvironment);
2019-06-24 11:58:36 +02:00
if (!Directory.Exists(modelsDirectory))
Directory.CreateDirectory(modelsDirectory);
return Path.Combine(modelsDirectory, "ood.flag");
}
private void Write()
2019-06-24 11:58:36 +02:00
{
var path = GetFlagPath();
if (path == null || File.Exists(path)) return;
File.WriteAllText(path, "THIS FILE INDICATES THAT MODELS ARE OUT-OF-DATE\n\n");
}
public void Clear()
2019-06-24 11:58:36 +02:00
{
if (_config.FlagOutOfDateModels == false) return;
2019-06-24 11:58:36 +02:00
var path = GetFlagPath();
if (path == null || !File.Exists(path)) return;
File.Delete(path);
}
public bool IsEnabled => _config.FlagOutOfDateModels;
2019-06-24 11:58:36 +02:00
public bool IsOutOfDate
2019-06-24 11:58:36 +02:00
{
get
{
if (_config.FlagOutOfDateModels == false) return false;
2019-06-24 11:58:36 +02:00
var path = GetFlagPath();
return path != null && File.Exists(path);
}
}
}
}