private void MediaService_Saving(IMediaService sender, Umbraco.Core.Events.SaveEventArgs <Umbraco.Core.Models.IMedia> e) { IEnumerable <string> supportedTypes = _contentSection.ImageFileTypes.ToList(); foreach (IMedia media in e.SavedEntities) { if (media.HasProperty("umbracoFile")) { string info = media.GetValue <string>(Constants.Conventions.Media.File); string path = JsonConvert.DeserializeObject <ImageCropperValue>(info).Src; string extension = Path.GetExtension(path).Substring(1); if (supportedTypes.InvariantContains(extension)) { string fullPath = _mediaFileSystem.GetFullPath(path); Stream inStream = null; int c = 0; do { c++; inStream = sender.GetMediaFileContentStream(fullPath); if (inStream == null) { Thread.Sleep(1000); } else { c = 6; } } while (c < 6); if (inStream != null) { using (var outStream = new MemoryStream()) { using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false)) { var image = imageFactory.Load(inStream).AutoRotate().Save(outStream); sender.SetMediaFileContent(fullPath, outStream); } } } } } } }
public OptimiseResponse Save(SaveModel model) { var media = _mediaService.GetById(model.Id); if (media == null) { return new OptimiseResponse("Media Not Found") { ResultType = Enums.ResultType.Error } } ; string path = media.GetUrl(StaticValues.Properties.UmbracoFile, Logger); string fullPath = _mediaFileSystem.GetFullPath(path); string extension = _mediaFileSystem.GetExtension(path)?.Substring(1); bool updated = false; byte[] data = null; using (Stream inStream = _mediaFileSystem.OpenFile(fullPath)) { if (Reduce(extension)) { data = _imageService.ReduceQuality(inStream); updated = true; } if (Shrink(extension)) { data = _shrinkService.ShrinkImage(inStream); updated = true; } } if (data == null || updated) { using (var outStream = new MemoryStream(data)) { outStream.Position = 0; try { media.SetValue(_contentTypeBaseServiceProvider, StaticValues.Properties.UmbracoFile, Path.GetFileName(path), outStream); _mediaService.Save(media); } catch (Exception ex) { Logger.Error(typeof(OptimiseController), "Save", ex); throw; } } return(new OptimiseResponse("Media optimised without any errors") { ResultType = Enums.ResultType.Success }); } else { return new OptimiseResponse("Media optimising was unsuccessful") { ResultType = Enums.ResultType.Error } }; }
public IEnumerable <DiagnosticGroup> GetDiagnosticGroups() { var groups = new List <DiagnosticGroup>(); int id = 0; var group = new DiagnosticGroup(id++, "Umbraco Configuration"); var sections = new List <DiagnosticSection>(); var section = new DiagnosticSection("Umbraco Version"); section.Diagnostics.Add(new Diagnostic("Version", runtimeState.Version)); section.Diagnostics.Add(new Diagnostic("Semantic Version", runtimeState.SemanticVersion.ToSemanticString())); section.Diagnostics.Add(new Diagnostic("Application URL", runtimeState.ApplicationUrl.ToString())); section.Diagnostics.Add(new Diagnostic("Application Virtual Path", runtimeState.ApplicationVirtualPath)); section.Diagnostics.Add(new Diagnostic("Debug?", runtimeState.Debug)); section.Diagnostics.Add(new Diagnostic("Is Main Dom?", runtimeState.IsMainDom)); section.Diagnostics.Add(new Diagnostic("Runtime Level", runtimeState.Level)); section.Diagnostics.Add(new Diagnostic("Runtime Reason", runtimeState.Reason)); section.Diagnostics.Add(new Diagnostic("Server Role", runtimeState.ServerRole)); section.Diagnostics.Add(new Diagnostic("Current Migration State", runtimeState.CurrentMigrationState)); section.Diagnostics.Add(new Diagnostic("Final Migration State", runtimeState.FinalMigrationState)); sections.Add(section); section = new DiagnosticSection("Umbraco Settings"); section.Diagnostics.Add(new Diagnostic("Debug Mode?", GlobalSettings.DebugMode)); section.AddDiagnostics(ConfigurationManager.AppSettings, false, key => key.StartsWith("umbraco", StringComparison.OrdinalIgnoreCase)); sections.Add(section); var configs = new Umbraco.Core.Configuration.Configs(); configs.AddCoreConfigs(); IUmbracoSettingsSection settings = configs.GetConfig <IUmbracoSettingsSection>(); sections.Add(DiagnosticSection.AddDiagnosticSectionFrom("Content Setting", settings.Content, true)); sections.Add(DiagnosticSection.AddDiagnosticSectionFrom("Request Handler Settings", settings.RequestHandler, true)); sections.Add(DiagnosticSection.AddDiagnosticSectionFrom("Tour Settings", settings.BackOffice.Tours, true)); sections.Add(DiagnosticSection.AddDiagnosticSectionFrom("Web Routing Settings", settings.WebRouting, true)); sections.Add(DiagnosticSection.AddDiagnosticSectionFrom("Security Settings", settings.Security, true)); sections.Add(DiagnosticSection.AddDiagnosticSectionFrom("Logging Settings", settings.Logging, true)); IHealthChecks health = configs.GetConfig <IHealthChecks>(); sections.Add(DiagnosticSection.AddDiagnosticSectionFrom("Health Checks", health.NotificationSettings, true)); IGridConfig grid = configs.GetConfig <IGridConfig>(); section = new DiagnosticSection("Grid Settings", grid.EditorsConfig.Editors.Select(e => new Diagnostic(e.Name, e.View))); sections.Add(section); section = new DiagnosticSection("Media File System"); section.Diagnostics.Add(new Diagnostic("Type", mediaFileSystem.GetType())); section.Diagnostics.Add(new Diagnostic("Can Add Physical?", mediaFileSystem.CanAddPhysical)); section.Diagnostics.Add(new Diagnostic("Full Root Path", mediaFileSystem.GetFullPath("~/"))); sections.Add(section); group.Add(sections); groups.Add(group); group = new DiagnosticGroup(id++, "Database Values"); sections = new List <DiagnosticSection>(); try { var conn = ConfigurationManager.ConnectionStrings[Constants.System.UmbracoConnectionName]; if (conn != null) { section = new DiagnosticSection("Database Settings"); section.Diagnostics.Add(new Diagnostic("Provider", conn.ProviderName)); section.Diagnostics.Add(new Diagnostic("Name", conn.Name)); section.Diagnostics.Add(new Diagnostic("Connection String", Regex.Replace(conn.ConnectionString, @"password(\W*)=(.+)(;|$)", "*******", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase))); sections.Add(section); } } catch { // deliberate } try { IEnumerable <ServerModel> servers = databaseService.GetRegistredServers(); if (servers != null && servers.Any()) { section = new DiagnosticSection("Server Registration"); foreach (ServerModel server in servers) { section.Diagnostics.Add(new Diagnostic(server.ComputerName, server.ToDiagnostic())); } sections.Add(section); } } catch { // deliberate } try { IEnumerable <UmbracoKeyValue> migrations = databaseService.GetKeyValues(); if (migrations != null && migrations.Any()) { section = new DiagnosticSection("Key Value Table"); foreach (UmbracoKeyValue migration in migrations) { section.Diagnostics.Add(new Diagnostic(migration.Value, migration.ToDiagnostic())); } sections.Add(section); } } catch { // deliberate } group.Add(sections); groups.Add(group); group = new DiagnosticGroup(id++, "Server Configuration"); sections = new List <DiagnosticSection>(); section = new DiagnosticSection("Server Settings"); section.Diagnostics.Add(new Diagnostic("Machine Name", Environment.MachineName)); section.Diagnostics.Add(new Diagnostic("OS Version", Environment.OSVersion)); section.Diagnostics.Add(new Diagnostic("64 Bit OS?", Environment.Is64BitOperatingSystem)); section.Diagnostics.Add(new Diagnostic("Processor Count", Environment.ProcessorCount)); section.Diagnostics.Add(new Diagnostic("Network Domain", Environment.UserDomainName)); section.Diagnostics.Add(new Diagnostic("ASP.NET Version", Environment.Version)); if (httpContext != null && httpContext.Request != null) { HttpRequestBase request = httpContext.Request; if (request != null) { section.Diagnostics.Add(new Diagnostic("Web Server", request["SERVER_SOFTWARE"])); section.Diagnostics.Add(new Diagnostic("Host", request["HTTP_HOST"])); section.Diagnostics.Add(new Diagnostic("App Pool ID", request["APP_POOL_ID"])); } } section.Diagnostics.Add(new Diagnostic("Current Directory", Environment.CurrentDirectory)); section.Diagnostics.Add(new Diagnostic("64 Bit Process?", Environment.Is64BitProcess)); section.Diagnostics.Add(new Diagnostic("Framework Bits", IntPtr.Size * 8)); section.Diagnostics.Add(new Diagnostic("Process Physical Memory", string.Format("{0:n} MB", Environment.WorkingSet / 1048576))); section.Diagnostics.Add(new Diagnostic("System Up Time", TimeSpan.FromTicks(Environment.TickCount))); try { object currentProcess = typeof(Process).GetMethod("GetCurrentProcess", Type.EmptyTypes).Invoke(null, null); if (currentProcess != null) { object processModule = typeof(Process).GetProperty("MainModule").GetValue(currentProcess, null); section.Diagnostics.Add(new Diagnostic("Current Process", (string)typeof(ProcessModule).GetProperty("ModuleName").GetValue(processModule, null))); } } catch { // deliberate } section.Diagnostics.Add(new Diagnostic("Current Culture", System.Threading.Thread.CurrentThread.CurrentCulture)); section.Diagnostics.Add(new Diagnostic("Current Thread State", System.Threading.Thread.CurrentThread.ThreadState)); sections.Add(section); section = new DiagnosticSection("Application Settings"); section.Diagnostics.Add(new Diagnostic("Application ID", HostingEnvironment.ApplicationID)); section.Diagnostics.Add(new Diagnostic("Site Name", HostingEnvironment.SiteName)); section.Diagnostics.Add(new Diagnostic("Development Env?", HostingEnvironment.IsDevelopmentEnvironment)); section.Diagnostics.Add(new Diagnostic("On UNC Share?", HttpRuntime.IsOnUNCShare)); section.Diagnostics.Add(new Diagnostic("Bin Directory", HttpRuntime.BinDirectory)); section.Diagnostics.Add(new Diagnostic("Code Gen Dir", HttpRuntime.CodegenDir)); section.Diagnostics.Add(new Diagnostic("Target Framework", HttpRuntime.TargetFramework)); section.Diagnostics.Add(new Diagnostic("App Domain ID", HttpRuntime.AppDomainId)); section.Diagnostics.Add(new Diagnostic("App Domain Path", HttpRuntime.AppDomainAppPath)); if (HostingEnvironment.Cache != null) { section.Diagnostics.Add(new Diagnostic("Cached Items", HostingEnvironment.Cache.Count.ToString())); section.Diagnostics.Add(new Diagnostic("Cache Memory Limit ", HostingEnvironment.Cache.EffectivePercentagePhysicalMemoryLimit + "%")); } section.Diagnostics.Add(new Diagnostic("Max Requests per CPU", HostingEnvironment.MaxConcurrentRequestsPerCPU)); section.Diagnostics.Add(new Diagnostic("Max Threads per CPU", HostingEnvironment.MaxConcurrentThreadsPerCPU)); section.Diagnostics.Add(new Diagnostic("Current Server Time", DateTime.Now)); sections.Add(section); if (httpContext != null && httpContext.Request != null) { section = new DiagnosticSection("Server Variables"); section.AddDiagnostics(httpContext.Request.ServerVariables, true, key => !ServerVarsToSkip.Contains(key)); } sections.Add(section); section = new DiagnosticSection("None Umbraco App Settings"); section.AddDiagnostics(ConfigurationManager.AppSettings, false, key => !key.StartsWith("umbraco", StringComparison.OrdinalIgnoreCase)); sections.Add(section); try { section = new DiagnosticSection("Mail Settings"); var smtp = new System.Net.Mail.SmtpClient(); section.Diagnostics.Add(new Diagnostic("SMTP Host", smtp.Host)); section.Diagnostics.Add(new Diagnostic("SMTP Delivery Method", smtp.DeliveryMethod)); section.Diagnostics.Add(new Diagnostic("SMTP Port", smtp.Port)); section.Diagnostics.Add(new Diagnostic("SMTP Service Point", smtp.ServicePoint.ConnectionName)); section.Diagnostics.Add(new Diagnostic("SMTP Delivery Format", smtp.DeliveryFormat)); sections.Add(section); } catch { // deliberate } group.Add(sections); groups.Add(group); group = new DiagnosticGroup(id++, "Umbraco Constants"); sections = new List <DiagnosticSection> { DiagnosticSection.AddDiagnosticSectionFromConstant("Applications", typeof(Constants.Applications)), DiagnosticSection.AddDiagnosticSectionFromConstant("Composing", typeof(Constants.Composing)), DiagnosticSection.AddDiagnosticSectionFromConstant("Conventions", typeof(Constants.Conventions)), DiagnosticSection.AddDiagnosticSectionFromConstant("Icons", typeof(Constants.Icons)), DiagnosticSection.AddDiagnosticSectionFromConstant("Object Types", typeof(Constants.ObjectTypes)), DiagnosticSection.AddDiagnosticSectionFromConstant("Property Editors", typeof(Constants.PropertyEditors)), DiagnosticSection.AddDiagnosticSectionFromConstant("Property Type Groups", typeof(Constants.PropertyTypeGroups)), DiagnosticSection.AddDiagnosticSectionFromConstant("Security", typeof(Constants.Security)), DiagnosticSection.AddDiagnosticSectionFromConstant("System", typeof(Constants.System)), DiagnosticSection.AddDiagnosticSectionFromConstant("Trees", typeof(Constants.Trees)), DiagnosticSection.AddDiagnosticSectionFromConstant("UDI Entity Type", typeof(Constants.UdiEntityType)), DiagnosticSection.AddDiagnosticSectionFromConstant("Umbraco Indexes", typeof(Constants.UmbracoIndexes)), DiagnosticSection.AddDiagnosticSectionFromConstant("Web", typeof(Constants.Web)) }; group.Add(sections); groups.Add(group); group = new DiagnosticGroup(id++, "MVC Configuration"); sections = new List <DiagnosticSection>(); try { var mvc = Assembly.Load(new AssemblyName("System.Web.Mvc")); AssemblyName name = mvc.GetName(); section = new DiagnosticSection("MVC Version"); section.Diagnostics.Add(new Diagnostic("MVC Version", name.Version)); section.Diagnostics.Add(new Diagnostic("Full Name", mvc.FullName)); section.Diagnostics.Add(new Diagnostic("Architecture", name.ProcessorArchitecture)); sections.Add(section); } catch { // deliberate } section = new DiagnosticSection("MVC Routes"); section.Diagnostics.AddRange(RouteTable.Routes.Select(r => (Route)r).Select(r => new Diagnostic(r.RouteHandler.GetType().Name, r.Url))); sections.Add(section); section = new DiagnosticSection("MVC Action Filters"); section.AddDiagnosticsFrom(typeof(IActionFilter)); sections.Add(section); section = new DiagnosticSection("MVC Authorization Filters"); section.AddDiagnosticsFrom(typeof(IAuthorizationFilter)); sections.Add(section); section = new DiagnosticSection("MVC Model Binders"); section.AddDiagnosticsFrom(typeof(IModelBinder)); sections.Add(section); section = new DiagnosticSection("MVC Controller Factories"); section.AddDiagnosticsFrom(typeof(IControllerFactory)); sections.Add(section); section = new DiagnosticSection("MVC Controllers"); section.AddDiagnosticsFrom(typeof(IController)); sections.Add(section); group.Add(sections); groups.Add(group); return(groups); }