public UnitTest1() { ImagePropertyGuids = new List <String>(); imageFileExtensions = new List <string>() { "JPG", "JPEG", "TIF", "PNG", "GIF", "BMP" }; ImageGuids = Task.Run(() => ValuesController.GetRandomImageGuids(imageFileExtensions, 500)).Result.Value; idCatalogItemFirstImage = Db.idCatalogItem .Include(x => x.idFilePath) .Where(x => x.FileName.EndsWith(".JPG")) .OrderBy(x => x.DateTimeStamp) .First(); idCatalogItemFirstVideo = Db.idCatalogItem .Include(x => x.idFilePath) .Where(x => x.FileName.EndsWith(".MP4")) .OrderBy(x => x.DateTimeStamp) .First(); idPropFirst = Db.idProp.First(); List <ImageProperty> ImageProperties = Task.Run(() => ValuesController.GetImageProperties(null)).Result.Value; foreach (ImageProperty imageProperty in ImageProperties) { ImagePropertyGuids.Add(imageProperty.GUID); } }
public string DeleteCatalogItemDefinition(string propertyGuid, string catalogItemGUID) { try { idCatalogItemDefinition currentIdCatalogItemDefinition = db.idCatalogItemDefinition.Single(x => x.GUID == propertyGuid && x.CatalogItemGUID == catalogItemGUID); idCatalogItem currentIdCatalogItem = db.idCatalogItem.Single(x => x.GUID == catalogItemGUID); idImageVersion currentIdImageVersion = db.idImageVersion.SingleOrDefault(x => x.MainImageGUID == catalogItemGUID); db.idCatalogItemDefinition.Remove(currentIdCatalogItemDefinition); currentIdCatalogItem.idInSync = currentIdCatalogItem.idInSync >> 2; if (currentIdImageVersion != null) { currentIdImageVersion.idInSync = currentIdImageVersion.idInSync >> 2; } db.SaveChanges(); log.Info(String.Format("Client {0}:{1} called DeleteCatalogItemDefinition with propertyGuid: {2} and catalogItemGUID {3}", clientEndpoint.Address, clientEndpoint.Port, propertyGuid, catalogItemGUID)); return("OK"); } catch (Exception ex) { log.Error(ex.ToString()); throw ex; } }
private static String GetCatalogItemFilePath(String guid) { IDImagerDB db = new IDImagerDB(); idCatalogItem catalogItem = db.idCatalogItem.Include("idFilePath").Single(x => x.GUID.Equals(guid)); db.Dispose(); return(StaticFunctions.GetImageFilePath(catalogItem)); }
private Stream GetImageStream(string imageGuid, string width = null, string height = null) { idCatalogItem catalogItem = null; Boolean keepAspectRatio = Boolean.Parse(ConfigurationManager.AppSettings["KeepAspectRatio"]); catalogItem = db.idCatalogItem.Include("idFilePath").Single(x => x.GUID == imageGuid); if (catalogItem == null) { throw new Exception("CatalogItem not found"); } Stream imageStream = StaticFunctions.GetImageFileStream(StaticFunctions.GetImageFilePath(catalogItem)); BitmapSource bitmapSource = StaticFunctions.GetBitmapFrameFromImageStream(imageStream, catalogItem.idFileType); System.Xml.Linq.XDocument recipeXDocument = null; try { recipeXDocument = StaticFunctions.GetRecipeXDocument(db, catalogItem); } catch (Exception ex) { log.Error(String.Format("Error in 'GetImageStream' when applying recipe on imageGuid {0}: {1}", catalogItem.GUID, ex.ToString())); } TransformGroup transformGroup = new TransformGroup(); if (width != null && height != null) { StaticFunctions.Resize(ref bitmapSource, ref transformGroup, int.Parse(width), int.Parse(height)); } Rotation rotation = StaticFunctions.Rotate(ref bitmapSource, ref transformGroup); if (Recipe.ApplyXmpRecipe(recipeXDocument, ref bitmapSource, transformGroup)) { BitmapFrame transformedBitmapFrame = BitmapFrame.Create(bitmapSource); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(transformedBitmapFrame); imageStream = new System.IO.MemoryStream(); encoder.Save(imageStream); } imageStream.Position = 0; return(imageStream); }
public string AddCatalogItemDefinition(string propertyGuid, string catalogItemGUID) { try { var query = from tbl in db.idCatalogItemDefinition where tbl.GUID == propertyGuid & tbl.CatalogItemGUID == catalogItemGUID select tbl; if (query.Count() > 0) { throw new Exception("CatalogItemDefinition already exists"); } idCatalogItem currentIdCatalogItem = db.idCatalogItem.Single(x => x.GUID == catalogItemGUID); idImageVersion currentIdImageVersion = db.idImageVersion.SingleOrDefault(x => x.MainImageGUID == catalogItemGUID); idCatalogItemDefinition newIdCatalogItemDefinition = new idCatalogItemDefinition(); newIdCatalogItemDefinition.GUID = propertyGuid; newIdCatalogItemDefinition.CatalogItemGUID = catalogItemGUID; newIdCatalogItemDefinition.idAssigned = DateTime.Now; db.idCatalogItemDefinition.Add(newIdCatalogItemDefinition); currentIdCatalogItem.idInSync = currentIdCatalogItem.idInSync >> 2; if (currentIdImageVersion != null) { currentIdImageVersion.idInSync = currentIdImageVersion.idInSync >> 2; } db.SaveChanges(); log.Info(String.Format("Client {0}:{1} called AddCatalogItemDefinition with propertyGuid: {2} and catalogItemGUID {3}", clientEndpoint.Address, clientEndpoint.Port, propertyGuid, catalogItemGUID)); return("OK"); } catch (Exception ex) { log.Error(ex.ToString()); throw ex; } }
private async Task <IActionResult> PlayInternal(string guid, string videosize) { using (LogContext.PushProperty(nameof(guid), guid)) using (LogContext.PushProperty(nameof(videosize), videosize)) { FileInfo transcodeFileInfo = null; try { if (string.IsNullOrEmpty(guid)) { return(BadRequest("Missing guid parameter")); } idCatalogItem catalogItem = await db.idCatalogItem.Include(x => x.idFilePath).SingleAsync(x => x.GUID.Equals(guid)); string strFilePath = StaticFunctions.GetImageFilePath(catalogItem, serviceSettings.FilePathReplace); string mimeType = GetMimeNameFromExt(catalogItem.FileName); if (videosize != null && !videosize.Equals("Original")) { if (string.IsNullOrEmpty(serviceSettings.TranscodeDirectory)) { return(BadRequest("Missing TranscodeDirectory setting")); } catalogItem.GetHeightAndWidth(out int originalVideoWidth, out int originalVideoHeight); Engine ffmpegEngine = StaticFunctions.GetFFmpegEngine(); ffmpegEngine.Error += (sender, eventArgs) => { logger.LogError(eventArgs.Exception.ToString()); }; string strTranscodeFilePath = StaticFunctions.GetTranscodeFilePath(guid, serviceSettings.TranscodeDirectory, videosize); await StaticFunctions.TranscodeVideo(ffmpegEngine, default, strFilePath, strTranscodeFilePath,
public static System.Xml.Linq.XDocument GetRecipeXDocument(IDImagerDB db, idCatalogItem catalogItem) { if (catalogItem.idHasRecipe > 0) { idImageData imageData = db.idImageData.SingleOrDefault(x => x.ImageGUID.Equals(catalogItem.GUID) && x.DataName.Equals("XMP")); if (imageData != null) { MemoryStream compressedXmpStream = new MemoryStream(imageData.idData); MemoryStream decompressedXmpStream = new MemoryStream(); ZOutputStream outZStream = new ZOutputStream(decompressedXmpStream); compressedXmpStream.CopyTo(outZStream); decompressedXmpStream.Position = 0; System.Xml.Linq.XDocument xdocument = System.Xml.Linq.XDocument.Load(decompressedXmpStream); compressedXmpStream.Close(); decompressedXmpStream.Close(); return xdocument; } } return null; }
public static String GetImageFilePath(idCatalogItem catalogItem, List <FilePathReplaceSettings> filePathReplaceSettings) { string strFilePath = catalogItem.idFilePath.FilePath; if (filePathReplaceSettings != null) { foreach (FilePathReplaceSettings settings in filePathReplaceSettings) { string strPathMatch = settings.PathMatch; string strPathReplace = settings.PathReplace; if (!string.IsNullOrEmpty(strPathMatch) && !string.IsNullOrEmpty(strPathReplace)) { strFilePath = strFilePath.Replace(strPathMatch, strPathReplace, StringComparison.CurrentCultureIgnoreCase); } if (Path.DirectorySeparatorChar != '\\') { strFilePath = strFilePath.Replace('\\', Path.DirectorySeparatorChar); } } } return(Path.Combine(strFilePath, catalogItem.FileName)); }
public async static Task <XDocument> GetRecipeXDocument(IDImagerDB db, idCatalogItem catalogItem) { if (catalogItem.idHasRecipe > 0) { idImageData imageData = await db.idImageData.SingleOrDefaultAsync(x => x.ImageGUID.Equals(catalogItem.GUID) && x.DataName.Equals("XMP")); if (imageData != null) { MemoryStream compressedXmpStream = new MemoryStream(imageData.idData); MemoryStream decompressedXmpStream = new MemoryStream(); ZOutputStream outZStream = new ZOutputStream(decompressedXmpStream); compressedXmpStream.CopyTo(outZStream); decompressedXmpStream.Position = 0; XDocument xdocument = XDocument.Load(decompressedXmpStream); compressedXmpStream.Close(); decompressedXmpStream.Close(); return(xdocument); } } return(null); }
private static void LogGenerateThumbnailsFailedCatalogItem(idCatalogItem catalogItem) { File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ThumbnailGeneratorFailedCatalogItems.txt"), catalogItem.GUID + "\r\n"); }
public static SaveImageThumbnailResult SaveImageThumbnail(idCatalogItem catalogItem, ref IDImagerDB db, ref IDImagerDB dbThumbs, List<String> types, Boolean keepAspectRatio, Boolean setGenericVideoThumbnailOnError) { SaveImageThumbnailResult result = new SaveImageThumbnailResult(); Stream imageStream = null; String filePath = null; try { filePath = GetImageFilePath(catalogItem); BitmapFrame bitmapFrame; if (ImageFileExtensions.Contains(catalogItem.idFileType)) { imageStream = GetImageFileStream(filePath); bitmapFrame = GetBitmapFrameFromImageStream(imageStream, catalogItem.idFileType); } else if (VideoFileExtensions.Contains(catalogItem.idFileType)) { try { bitmapFrame = BitmapFrame.Create((BitmapSource)GenerateVideoThumbnail(filePath, new TimeSpan(0, 0, 0))); } catch (Exception ex) { if (setGenericVideoThumbnailOnError) { result.Exceptions.Add(new Exception(String.Format("Video thumbnail generation for imageGUID {0} file {1} failed. Generic thumbnails has been set.", catalogItem.GUID, filePath), ex)); Assembly assembly = Assembly.GetExecutingAssembly(); Stream genericVideoThumbnailStream = assembly.GetManifestResourceStream(@"IDBrowserServiceCode.Images.image_ph2.png"); bitmapFrame = BitmapFrame.Create(genericVideoThumbnailStream); } else { result.Exceptions.Add(new Exception(String.Format("Video thumbnail generation for imageGUID {0} file {1} failed.", catalogItem.GUID, filePath), ex)); return result; } } } else { throw new Exception(String.Format("File type {0} not supported", catalogItem.idFileType)); } foreach (String type in types) { int imageWidth; int imageHeight; if (type.Equals("T")) { imageWidth = 160; imageHeight = 120; } else { imageWidth = Int32.Parse(ConfigurationManager.AppSettings["MThumbmailWidth"]); imageHeight = Int32.Parse(ConfigurationManager.AppSettings["MThumbnailHeight"]); } XDocument recipeXDocument = null; if (type.Equals("T") || type.Equals("R")) { if (catalogItem.idHasRecipe > 0) recipeXDocument = GetRecipeXDocument(db, catalogItem); } TransformGroup transformGroup = new TransformGroup(); if (bitmapFrame.PixelWidth > imageWidth && bitmapFrame.PixelHeight > imageHeight) { double scaleX; double scaleY; foreach (ScaleTransform existingScaleTransform in transformGroup.Children.OfType<ScaleTransform>().ToList()) transformGroup.Children.Remove(existingScaleTransform); if (bitmapFrame.PixelWidth > bitmapFrame.PixelHeight) { scaleX = (double)imageWidth / (double)bitmapFrame.PixelWidth; scaleY = (double)imageHeight / (double)bitmapFrame.PixelHeight; } else { scaleX = (double)imageHeight / (double)bitmapFrame.PixelHeight; scaleY = (double)imageWidth / (double)bitmapFrame.PixelWidth; } ScaleTransform scaleTransform = new ScaleTransform(scaleX, scaleY, 0, 0); transformGroup.Children.Add(scaleTransform); } Rotation rotation = StaticFunctions.GetRotation(bitmapFrame); if (rotation != Rotation.Rotate0) { RotateTransform rotateTransform = new RotateTransform(); switch (rotation) { case Rotation.Rotate90: rotateTransform.Angle = 90; break; case Rotation.Rotate180: rotateTransform.Angle = 180; break; case Rotation.Rotate270: rotateTransform.Angle = 270; break; } transformGroup.Children.Add(rotateTransform); } Stream resizedImageStream = imageStream; BitmapSource bitmapSource = bitmapFrame; if (Recipe.ApplyXmpRecipe(recipeXDocument, ref bitmapSource, transformGroup)) { BitmapFrame transformedBitmapFrame = BitmapFrame.Create(bitmapSource); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(transformedBitmapFrame); resizedImageStream = new System.IO.MemoryStream(); encoder.Save(resizedImageStream); resizedImageStream.Position = 0; } lock (dbThumbs) { idThumbs newThumb = new idThumbs(); newThumb.GUID = Guid.NewGuid().ToString().ToUpper(); newThumb.ImageGUID = catalogItem.GUID; newThumb.idThumb = StreamToByteArray(resizedImageStream); newThumb.idType = type; dbThumbs.idThumbs.Add(newThumb); } result.ImageStreams.Add(resizedImageStream); } if (imageStream != null) { imageStream.Close(); } dbThumbs.SaveChanges(); } catch (Exception ex) { if (imageStream != null) { imageStream.Close(); } result.Exceptions.Add(new Exception(String.Format("Error generating thumbnail for imageGUID {0} file {1}", catalogItem.GUID, filePath), ex)); } return result; }
private Stream GetImageThumbnailStream(string type, string imageGuid, string width = null, string height = null) { if (type != "T" && type != "R" && type != "M") { throw new Exception("Unsupported image type"); } idCatalogItem catalogItem = null; Stream imageStream = null; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, readUncommittedTransactionOptions)) { catalogItem = db.idCatalogItem.Include("idFilePath").Single(x => x.GUID == imageGuid); if (catalogItem == null) { throw new Exception("CatalogItem not found"); } if (type == "R" && catalogItem.idHasRecipe == 0) { throw new Exception("Image has no recipe"); } //Check if CatalogItem has a recipe, if yes try to get the recipe image if (type == "M" && catalogItem.idHasRecipe > 0) { type = "R"; } Boolean keepAspectRatio = Boolean.Parse(ConfigurationManager.AppSettings["KeepAspectRatio"]); Boolean setGenericVideoThumbnailOnError = Boolean.Parse(ConfigurationManager.AppSettings["SetGenericVideoThumbnailOnError"]); idThumbs thumb = null; lock (dbThumbs) { //Searching with FirstOrDefault because PhotoSupreme sometimes stores the Thumbnail twice thumb = dbThumbs.idThumbs.FirstOrDefault(x => x.ImageGUID == imageGuid && x.idType == type); //If recipe image is not found, return the M image, //because the programm cannot yet generate the recipe image if (thumb == null && type == "R") { type = "M"; //Searching with FirstOrDefault because PhotoSupreme sometimes stores the Thumbnail twice thumb = dbThumbs.idThumbs.FirstOrDefault(x => x.ImageGUID == imageGuid && x.idType == type); } } if (thumb == null && Boolean.Parse(ConfigurationManager.AppSettings["CreateThumbnails"])) { SaveImageThumbnailResult result = StaticFunctions.SaveImageThumbnail(catalogItem, ref db, ref dbThumbs, new List <String>() { type }, keepAspectRatio, setGenericVideoThumbnailOnError); foreach (Exception ex in result.Exceptions) { log.Error(ex.ToString()); } if (result.ImageStreams.Count > 0) { imageStream = result.ImageStreams.First(); if (IsRequestRest()) { WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; } log.Info(String.Format("Client {0}:{1} called GetImageThumbnail with type: {2} imageGuid: {3} (returned resizedImageStream)", clientEndpoint.Address, clientEndpoint.Port, type, imageGuid)); } else { log.Info(String.Format("Client {0}:{1} called GetImageThumbnail with type: {2} imageGuid: {3} (returned null)", clientEndpoint.Address, clientEndpoint.Port, type, imageGuid)); } } else { imageStream = new MemoryStream(thumb.idThumb); if (IsRequestRest()) { WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; } log.Info(String.Format("Client {0}:{1} called GetImageThumbnail with type: {2} imageGuid: {3} (returned imageStream)", clientEndpoint.Address, clientEndpoint.Port, type, imageGuid)); } scope.Complete(); } if (width != null && height != null) { TransformGroup transformGroup = new TransformGroup(); BitmapSource bitmapSource = StaticFunctions.GetBitmapFrameFromImageStream(imageStream, "JPG"); StaticFunctions.Resize(ref bitmapSource, ref transformGroup, int.Parse(width), int.Parse(height)); if (transformGroup != null && transformGroup.Children.Count > 0) { TransformedBitmap tb = new TransformedBitmap(); tb.BeginInit(); tb.Source = bitmapSource; tb.Transform = transformGroup; tb.EndInit(); bitmapSource = tb; BitmapFrame transformedBitmapFrame = BitmapFrame.Create(bitmapSource); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(transformedBitmapFrame); imageStream = new System.IO.MemoryStream(); encoder.Save(imageStream); imageStream.Position = 0; } } return(imageStream); }
public async static Task <SaveImageThumbnailResult> SaveImageThumbnail(idCatalogItem catalogItem, IDImagerDB db, IDImagerThumbsDB dbThumbs, List <string> types, ServiceSettings serviceSettings) { SaveImageThumbnailResult result = new SaveImageThumbnailResult(); Stream imageStream = null; String filePath = null; try { filePath = GetImageFilePath(catalogItem, serviceSettings.FilePathReplace); imageStream = GetImageFileStream(filePath); foreach (String type in types) { int imageWidth; int imageHeight; if (type.Equals("T")) { imageWidth = 160; imageHeight = 120; } else { imageWidth = serviceSettings.MThumbmailWidth; imageHeight = serviceSettings.MThumbnailHeight; } XmpRecipeContainer xmpRecipeContainer = null; if (type.Equals("T") || type.Equals("R")) { if (catalogItem.idHasRecipe > 0) { XDocument recipeXDocument = await GetRecipeXDocument(db, catalogItem); xmpRecipeContainer = XmpRecipeHelper.ParseXmlRecepie(recipeXDocument); } } MemoryStream resizedImageStream = new MemoryStream(); MagickReadSettings magickReadSettings = null; if (Enum.TryParse <MagickFormat>(catalogItem.idFileType, true, out MagickFormat magickFormat)) { magickReadSettings = new MagickReadSettings { Format = magickFormat }; } imageStream.Position = 0; MagickImage image = new MagickImage(imageStream, magickReadSettings) { Format = MagickFormat.Jpeg, }; image.Resize(imageWidth, imageHeight); if (xmpRecipeContainer != null) { XmpRecipeHelper.ApplyXmpRecipe(xmpRecipeContainer, image); } image.Write(resizedImageStream); resizedImageStream.Position = 0; bool boolThumbExists = await dbThumbs.idThumbs .AnyAsync(x => x.ImageGUID == catalogItem.GUID && x.idType == type); if (!boolThumbExists) { idThumbs newThumb = new idThumbs { GUID = Guid.NewGuid().ToString().ToUpper(), ImageGUID = catalogItem.GUID, idThumb = StreamToByteArray(resizedImageStream), idType = type }; dbThumbs.idThumbs.Add(newThumb); } result.ImageStreams.Add(resizedImageStream); } if (imageStream != null) { imageStream.Close(); } await dbThumbs.SaveChangesAsync(); } catch (Exception ex) { if (imageStream != null) { imageStream.Close(); } result.Exceptions.Add(new Exception(String.Format("Error generating thumbnail for imageGUID {0} file {1}", catalogItem.GUID, filePath), ex)); } return(result); }