SenseNet.Preview by kavics,aniko,lajos,tusmester

<PackageReference Include="SenseNet.Preview" Version="7.2.1" />

 PreviewImageGenerator

using SenseNet.Tools; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace SenseNet.Preview { public abstract class PreviewImageGenerator : IPreviewImageGenerator { private static readonly object LoaderSync = new object(); private static Dictionary<string, IPreviewImageGenerator> _providers; public abstract string[] KnownExtensions { get; } private static Dictionary<string, IPreviewImageGenerator> Providers { get { if (_providers == null) { lock (LoaderSync) { if (_providers == null) _providers = CreateProviderPrototypes(); } } return _providers; } } public abstract Task GeneratePreviewAsync(Stream docStream, IPreviewGenerationContext context, CancellationToken cancellationToken); public virtual string GetTaskNameByExtension(string extension) { return null; } public virtual string GetTaskTitleByExtension(string extension) { return null; } public virtual string[] GetSupportedTaskNames() { return null; } private static Dictionary<string, IPreviewImageGenerator> CreateProviderPrototypes() { Dictionary<string, IPreviewImageGenerator> dictionary = new Dictionary<string, IPreviewImageGenerator>(); Type[] typesByInterface = TypeResolver.GetTypesByInterface(typeof(IPreviewImageGenerator)); foreach (Type type in typesByInterface) { if (!type.IsAbstract) { IPreviewImageGenerator previewImageGenerator = (IPreviewImageGenerator)Activator.CreateInstance(type); string[] knownExtensions = previewImageGenerator.KnownExtensions; for (int j = 0; j < knownExtensions.Length; j++) { string key = knownExtensions[j].ToLowerInvariant(); if (!dictionary.TryGetValue(key, out IPreviewImageGenerator value) || !type.IsInstanceOfType(value)) dictionary[key] = previewImageGenerator; } } } return dictionary; } public static string GetTaskNameByFileNameExtension(string extension) { if (!Providers.TryGetValue(extension.ToLowerInvariant(), out IPreviewImageGenerator value)) throw new ApplicationException(SR.F(SR.UnknownProvider_1, extension)); return value.GetTaskNameByExtension(extension); } public static string GetTaskTitleByFileNameExtension(string extension) { if (!Providers.TryGetValue(extension.ToLowerInvariant(), out IPreviewImageGenerator value)) throw new ApplicationException(SR.F(SR.UnknownProvider_1, extension)); return value.GetTaskTitleByExtension(extension); } public static string[] GetSupportedCustomTaskNames() { return (from tn in (from tn in (from pig in Providers.Values select pig.GetSupportedTaskNames() into tnames where tnames != null select tnames).SelectMany((string[] tnames) => tnames) where !string.IsNullOrEmpty(tn) select tn).Distinct() orderby tn select tn).ToArray(); } public static bool IsSupportedExtension(string extension) { return Providers.ContainsKey(extension.ToLowerInvariant()); } public static Task GeneratePreviewAsync(string extension, Stream docStream, IPreviewGenerationContext context, CancellationToken cancellationToken) { if (!Providers.TryGetValue(extension.ToLowerInvariant(), out IPreviewImageGenerator value)) throw new ApplicationException(SR.F(SR.UnknownProvider_1, extension)); return value.GeneratePreviewAsync(docStream, context, cancellationToken); } } }