public void FindBinaryWithoutExtension() { IBinary binary = BinaryFactory.FindBinary("/media/image"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); }
public void FindBinaryPublishedNoDateAndAlreadyOnFS() { ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageWidth = 300; ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageHeight = 400; int currentBinaryExpiration = TestConfiguration.OverrideBinaryExpiration; TestConfiguration.OverrideBinaryExpiration = 0; IBinary binary = BinaryFactory.FindBinary("nodate"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); Image img = GetImageFromBytes(binary.BinaryData); Assert.IsTrue(img.Width == 300); Assert.IsTrue(img.Height == 400); // change the generated image dimensions in the provider // this should NOT affect the results because the binary should be cached ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageWidth = 400; ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageHeight = 200; binary = BinaryFactory.FindBinary("nodate"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); img = GetImageFromBytes(binary.BinaryData); Assert.IsTrue(img.Width == 400); Assert.IsTrue(img.Height == 200); ResetImageDimensions(); TestConfiguration.OverrideBinaryExpiration = currentBinaryExpiration; }
public void FindBinary() { IBinary binary = BinaryFactory.FindBinary("/media/image.png"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); }
public void ResizeImageToWidth() { IBinary binary = BinaryFactory.FindBinary("/media/image_w160.png"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); Image img = GetImageFromBytes(binary.BinaryData); Assert.IsTrue(img.Width == 160); Assert.IsTrue(img.Height == 55); }
public void BinaryCacheIsInvalidated() { MockMessageProvider messageProvider = new MockMessageProvider(); messageProvider.Start(); ((DefaultCacheAgent)((FactoryBase)BinaryFactory).CacheAgent).Subscribe(messageProvider); ResetImageDimensions(); TestConfiguration.OverrideBinaryExpiration = 180; IBinary binary = BinaryFactory.FindBinary("/media/image.png"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); Image img = GetImageFromBytes(binary.BinaryData); Assert.IsTrue(img.Width == 320); Assert.IsTrue(img.Height == 110); ICacheEvent cacheEvent = new CacheEvent() { Key = "1:2", RegionPath = "Binaries", Type = 0 }; messageProvider.BroadcastCacheEvent(cacheEvent); // change the generated image dimensions in the provider // this should NOT affect the results because the binary should be cached ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageWidth = 400; ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageHeight = 200; binary = BinaryFactory.FindBinary("/media/image.png"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); img = GetImageFromBytes(binary.BinaryData); Assert.IsTrue(img.Width == 400); Assert.IsTrue(img.Height == 200); ResetImageDimensions(); }
public void FindBinaryPublishedStaleAndAlreadyOnFS() { if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPVEYOR_BUILD_FOLDER"))) { return; // for some unknown reason, this test fails on AppVeyor (we need to look into this but it has a low prio) } ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageWidth = 300; ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageHeight = 400; int currentBinaryExpiration = TestConfiguration.OverrideBinaryExpiration; TestConfiguration.OverrideBinaryExpiration = 0; IBinary binary = BinaryFactory.FindBinary("/media/image2.png"); Assert.IsNotNull(binary); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); Image img = GetImageFromBytes(binary.BinaryData); Assert.IsTrue(img.Width == 300, "width is not 300 when image is retrieved for the first time"); Assert.IsTrue(img.Height == 400, "height is not 400 when image is retrieved for the first time"); // change the generated image dimensions in the provider // this should NOT affect the results because the binary should be cached ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageWidth = 400; ((TridionBinaryProvider)BinaryFactory.BinaryProvider).GeneratedImageHeight = 200; binary = BinaryFactory.FindBinary("/media/image2.png"); Assert.IsNotNull(binary, "binary is null"); Assert.IsTrue(binary.BinaryData.Length > 100, "byte array is too small, something went wrong"); Assert.IsFalse(string.IsNullOrEmpty(binary.Id), "binary.Id is missing"); img = GetImageFromBytes(binary.BinaryData); Assert.IsTrue(img.Width == 400, "width is not 400 when image is retrieved for the second time"); Assert.IsTrue(img.Height == 200, "height is not 200 when image is retrieved for the second time"); ResetImageDimensions(); TestConfiguration.OverrideBinaryExpiration = currentBinaryExpiration; }
public void ProcessRequest(HttpContext context) { IBinary binary = null; string url = context.Request.Path; Cache cache = HttpContext.Current.Cache; if (cache[url] != null) { binary = (IBinary)cache[url]; } else { binary = BinaryFactory.FindBinary(context.Request.Path); int cacheSetting = Convert.ToInt32(ConfigurationManager.AppSettings[BinaryHandlerCachingKey]); cache.Insert(url, binary, null, DateTime.Now.AddSeconds(cacheSetting), TimeSpan.Zero); } string etagDate = "\"" + binary.LastPublishedDate.ToString("s", DateTimeFormatInfo.InvariantInfo) + "\""; string incomingEtag = context.Request.Headers["If-None-Match"]; context.Response.Cache.SetETag(etagDate); context.Response.Cache.SetCacheability(HttpCacheability.Public); if (etagDate.Equals(incomingEtag)) { context.Response.StatusCode = 304; return; } var localPath = ConvertUrl(context.Request.Path, context); if (binary == null) { // there appears to be no binary present in Tridion // now we must check if there is still an (old) cached copy on the local file system object fileLock = LockFile(binary.Url); lock (fileLock) { RemoveFromDB(binary); lock (locks) { locks.Remove(binary.Url); } } // that's all for us, the file is not on the FS, so // the default FileHandler will cause a 404 exception context.Response.StatusCode = 404; context.Response.End(); return; } using (var db = new BinariesEntities()) { bool exists = false; var binData = db.Binaries.Where(bin => bin.ComponentUri == binary.Id).FirstOrDefault(); TimeSpan ts = TimeSpan.Zero; if (binData != null) { ts = binary.LastPublishedDate.Subtract(binData.LastPublishedDate); exists = true; } if (!exists || ts.TotalMilliseconds > 0) { WriteToDb(binary); } } FillResponse(context.Response, binary); }