public async static Task RenderAsync(FrontContext context) { var image = await context.SiteDb.ImagePool.GetAsync(context.Route.objectId); if (image == null || image.ContentBytes == null) { image = await context.SiteDb.Images.GetAsync(context.Route.objectId); } //var image = context.SiteDb.Images.Get(context.Route.objectId); if (context.RenderContext.WebSite.EnableImageLog) { if (context.RenderContext.Request.Channel == Data.Context.RequestChannel.Default) { Kooboo.Data.Models.ImageLog log = new Data.Models.ImageLog(); log.ClientIP = context.RenderContext.Request.IP; log.Url = context.RenderContext.Request.RawRelativeUrl; log.StartTime = DateTime.Now; if (image != null) { log.Size = image.Size; log.ImageId = image.Id; } context.RenderContext.WebSite.SiteDb().ImageLog.Add(log); } } if (image == null) { return; } RenderImage(context, image); context.RenderContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); context.RenderContext.Response.Headers.Add("Access-Control-Allow-Headers", "*"); VersionRenderer.ImageVersion(context); }
//file only contains column data. public static async Task RenderFile(FrontContext context, Models.CmsFile file) { var sitedb = context.WebSite.SiteDb(); string contentType = null; if (!string.IsNullOrEmpty(file.ContentType)) { contentType = file.ContentType; } else if (!string.IsNullOrWhiteSpace(file.Name)) { contentType = IOHelper.MimeType(file.Name); } if (string.IsNullOrWhiteSpace(contentType)) { if (context.Route != null) { contentType = IOHelper.MimeType(context.Route.Name); } } context.RenderContext.Response.ContentType = contentType; if (file.Size > BigSize) { var partinfo = sitedb.Files.Store.GetFieldPart(file.Id, nameof(file.ContentBytes)); context.RenderContext.Response.FilePart = partinfo; } else { // read whole content. CmsFile FileContent = null; if (file.ContentBytes == null) { FileContent = await sitedb.FilePool.GetAsync(file.Id); } else { FileContent = file; } if (FileContent.ContentBytes != null) { context.RenderContext.Response.Body = FileContent.ContentBytes; } else if (!string.IsNullOrEmpty(FileContent.ContentString)) { context.RenderContext.Response.Body = DataConstants.DefaultEncoding.GetBytes(FileContent.ContentString); } } // cache for font. if (contentType != null) { string lower = contentType.ToLower(); if (lower.Contains("font")) { VersionRenderer.FontVersion(context); } else if (lower.Contains("image")) { context.RenderContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); context.RenderContext.Response.Headers.Add("Access-Control-Allow-Headers", "*"); VersionRenderer.ImageVersion(context); } else if (lower.Contains("css")) { VersionRenderer.ScriptStyleVersion(context); } else if (lower.Contains("javascript")) { VersionRenderer.ScriptStyleVersion(context); } else if (lower.Contains("video")) { VersionRenderer.VideoVersion(context); } } }
//public static void Render(FrontContext context) //{ // var image = context.SiteDb.ImagePool.Get(context.Route.objectId); // if (image == null || image.ContentBytes == null) // { // image = context.SiteDb.Images.Get(context.Route.objectId); // } // if (context.RenderContext.WebSite.EnableImageLog) // { // if (context.RenderContext.Request.Channel == Data.Context.RequestChannel.Default) // { // Kooboo.Data.Models.ImageLog log = new Data.Models.ImageLog(); // log.ClientIP = context.RenderContext.Request.IP; // log.Url = context.RenderContext.Request.RawRelativeUrl; // log.StartTime = DateTime.Now; // if (image != null) // { // log.Size = image.Size; // log.ImageId = image.Id; // } // context.RenderContext.WebSite.SiteDb().ImageLog.Add(log); // } // } // if (image == null) // { // return; // } // RenderImage(context, image); //} public static void RenderImage(FrontContext context, Models.Image image) { context.RenderContext.Response.ContentType = "image"; if (Kooboo.Lib.Helper.UrlHelper.GetFileType(image.Extension) == Lib.Helper.UrlHelper.UrlFileType.Image) { string imagetype = image.Extension; if (imagetype.StartsWith(".")) { imagetype = imagetype.Substring(1); } context.RenderContext.Response.ContentType = context.RenderContext.Response.ContentType + "/" + imagetype; } //"image/svg+xml" if (image.Extension == "svg" || image.Extension == ".svg") { context.RenderContext.Response.ContentType += "+xml"; } var bytes = image.ContentBytes; var width = context.RenderContext.Request.Get("width"); var height = context.RenderContext.Request.Get("height"); if (!string.IsNullOrEmpty(width)) { EnsureImageSize(image); if (!string.IsNullOrWhiteSpace(height)) { int intwidth = 0; int intheight = 0; if (int.TryParse(width, out intwidth) && int.TryParse(height, out intheight)) { bytes = GetImageThumbnail(context.RenderContext, bytes, intwidth, intheight, image.Version); } } else { int intwidth = 0; if (int.TryParse(width, out intwidth)) { if (image.Height > 0 && image.Width > 0) { int intheight = (int)intwidth * image.Height / image.Width; bytes = GetImageThumbnail(context.RenderContext, bytes, intwidth, intheight, image.Version); } } } } else if (!string.IsNullOrEmpty(height)) { EnsureImageSize(image); if (!string.IsNullOrWhiteSpace(width)) { int intwidth = 0; int intheight = 0; if (int.TryParse(width, out intwidth) && int.TryParse(height, out intheight)) { bytes = GetImageThumbnail(context.RenderContext, bytes, intwidth, intheight, image.Version); } } else { // opposite, has height. //int intwidth = 0; //if (int.TryParse(width, out intwidth)) //{ // if (image.Height > 0 && image.Width > 0) // { // int intheight = (int)intwidth * image.Height / image.Width; // bytes = GetImageThumbnail(context.RenderContext, bytes, intwidth, intheight, image.Version); // } //} int intheight = 0; if (int.TryParse(height, out intheight)) { if (image.Height > 0 && image.Width > 0) { int intwidth = (int)intheight * image.Width / image.Height; bytes = GetImageThumbnail(context.RenderContext, bytes, intwidth, intheight, image.Version); } } } } context.RenderContext.Response.Body = bytes; context.RenderContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); context.RenderContext.Response.Headers.Add("Access-Control-Allow-Headers", "*"); VersionRenderer.ImageVersion(context); }