Esempio n. 1
0
        public async void SaveVideoThumbnailTest()
        {
            List <string> types = new List <string>()
            {
                "T", "R", "M"
            };

            SaveImageThumbnailResult result = await StaticFunctions
                                              .SaveImageThumbnail(idCatalogItemFirstVideo, Db, DbThumbs, types, Settings);

            if (result.Exceptions.Count > 0)
            {
                throw result.Exceptions.First();
            }
        }
        /// <summary>
        /// Generates thumbnails based on parameters
        /// </summary>
        /// <param name="configuration">Json configuration of IDBrowserService as IDBrowserConfiguration class</param>
        /// <param name="siteName">Site name to generate thumbnails</param>
        /// <param name="fromDateTime">From date filter</param>
        /// <param name="toDateTime">To date filter</param>
        /// <param name="fileFilter">File type filter</param>
        /// <param name="imageGuid">Generate single image guid</param>
        /// <param name="overwrite">Overwrite existing thumbnails</param>
        public async static void GenerateThumbnails(IDBrowserConfiguration configuration, string siteName, DateTime fromDateTime,
                                                    DateTime toDateTime, string fileFilter, string imageGuid, bool overwrite)
        {
            SiteSettings siteSettings = configuration.Sites[siteName];

            var optionsBuilder = new DbContextOptionsBuilder <IDImagerDB>();

            if (siteSettings.ConnectionStrings.DBType.Equals("MsSql"))
            {
                optionsBuilder.UseSqlServer(siteSettings.ConnectionStrings.IDImager);
            }
            else if (siteSettings.ConnectionStrings.DBType.Equals("Postgres"))
            {
                optionsBuilder.UseNpgsql(siteSettings.ConnectionStrings.IDImager);
                optionsBuilder.ReplaceService <ISqlGenerationHelper, PostgresSqlGenerationHelper>();
            }

            IDImagerDB db = new IDImagerDB(GetIDImagerDBOptionsBuilder <IDImagerDB>(siteSettings.ConnectionStrings.DBType,
                                                                                    siteSettings.ConnectionStrings.IDImager).Options);
            IDImagerThumbsDB dbThumbs = new IDImagerThumbsDB(GetIDImagerDBOptionsBuilder <IDImagerThumbsDB>(siteSettings.ConnectionStrings.DBType,
                                                                                                            siteSettings.ConnectionStrings.IDImagerThumbs).Options);

            var queryCatalogItem = from catalogItem in db.idCatalogItem.Include("idFilePath")
                                   where configuration.ImageFileExtensions.Contains(catalogItem.idFileType) ||
                                   configuration.VideoFileExtensions.Contains(catalogItem.idFileType)
                                   select catalogItem;

            if (fromDateTime != DateTime.MinValue)
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.idCreated >= fromDateTime);
            }

            if (toDateTime != DateTime.MinValue)
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.idCreated <= toDateTime);
            }

            if (!string.IsNullOrEmpty(fileFilter))
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.idFileType.ToLower().Equals(fileFilter.ToLower()));
            }

            if (!string.IsNullOrEmpty(imageGuid))
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.GUID.Equals(imageGuid, StringComparison.OrdinalIgnoreCase));
            }

            int intCountCatalogItem    = queryCatalogItem.Count();
            int intCatalogItemCounter  = 0;
            int intThumbnailsGenerated = 0;

            foreach (idCatalogItem catalogItem in queryCatalogItem)
            {
                List <String>   typesToGenerate = new List <String>();
                List <idThumbs> idThumbsT       = dbThumbs.idThumbs.Where(x => x.ImageGUID == catalogItem.GUID && x.idType.Equals("T")).ToList();
                List <idThumbs> idThumbsM       = dbThumbs.idThumbs.Where(x => x.ImageGUID == catalogItem.GUID && x.idType.Equals("M")).ToList();
                List <idThumbs> idThumbsR       = dbThumbs.idThumbs.Where(x => x.ImageGUID == catalogItem.GUID && x.idType.Equals("R")).ToList();

                if (idThumbsT.Count() == 0)
                {
                    typesToGenerate.Add("T");
                }

                if (idThumbsM.Count() == 0)
                {
                    typesToGenerate.Add("M");
                }

                if (catalogItem.idHasRecipe > 0 && idThumbsR.Count() == 0)
                {
                    typesToGenerate.Add("R");
                }

                if (overwrite)
                {
                    foreach (idThumbs thumb in idThumbsT)
                    {
                        dbThumbs.idThumbs.Remove(thumb);
                    }

                    foreach (idThumbs thumb in idThumbsM)
                    {
                        dbThumbs.idThumbs.Remove(thumb);
                    }

                    foreach (idThumbs thumb in idThumbsR)
                    {
                        dbThumbs.idThumbs.Remove(thumb);
                    }

                    typesToGenerate.Clear();
                    typesToGenerate.Add("T");
                    typesToGenerate.Add("M");
                    if (catalogItem.idHasRecipe > 0)
                    {
                        typesToGenerate.Add("R");
                    }
                }

                if (typesToGenerate.Count() > 0)
                {
                    try
                    {
                        SaveImageThumbnailResult result = await StaticFunctions.SaveImageThumbnail(catalogItem, db, dbThumbs, typesToGenerate,
                                                                                                   siteSettings.ServiceSettings);

                        foreach (Exception ex in result.Exceptions)
                        {
                            LogGenerateThumbnailsException(ex);
                            Console.WriteLine(ex.ToString());
                        }

                        if (result.Exceptions.Count > 0)
                        {
                            LogGenerateThumbnailsFailedCatalogItem(catalogItem);
                        }

                        intThumbnailsGenerated += result.ImageStreams.Count();
                    }
                    catch (Exception e)
                    {
                        LogGenerateThumbnailsException(e);
                        LogGenerateThumbnailsFailedCatalogItem(catalogItem);
                        Console.WriteLine(e.ToString());
                    }
                }

                intCatalogItemCounter += 1;

                Console.CursorLeft = 0;
                Console.Write(String.Format("{0} of {1} catalogitems checked. {2} thumbnails generated", intCatalogItemCounter, intCountCatalogItem, intThumbnailsGenerated));
            }
        }
Esempio n. 3
0
        //private static void GenerateThumbnails(String type)
        //{
        //    Boolean keepAspectRatio = Boolean.Parse(ConfigurationManager.AppSettings["KeepAspectRatio"]);
        //    Boolean setGenericVideoThumbnailOnError = Boolean.Parse(ConfigurationManager.AppSettings["SetGenericVideoThumbnailOnError"]);

        //    IDImagerEntities db = new IDBrowserServiceCode.IDImagerEntities();
        //    IDImagerEntities dbThumbs = new IDBrowserServiceCode.IDImagerEntities(ConfigurationManager.ConnectionStrings["IDImagerThumbsEntities"].ConnectionString);

        //    List<String> imageFileExtensions = ConfigurationManager.AppSettings["ImageFileExtensions"].Split(new char[] {char.Parse(",")}).ToList();
        //    List<String> videoFileExtensions = ConfigurationManager.AppSettings["VideoFileExtensions"].Split(new char[] { char.Parse(",") }).ToList();

        //    List<String> thumbImageGuids = dbThumbs.idThumbs.Where(x => x.idType.Equals(type)).Select(x => x.ImageGUID).ToList();

        //    Console.WriteLine("Getting images without thumbnails....");
        //    var queryThumbnails = from catalogItem in db.idCatalogItem.Include("idFilePath")
        //                          where !thumbImageGuids.Contains(catalogItem.GUID) && (imageFileExtensions.Contains(catalogItem.idFileType) || videoFileExtensions.Contains(catalogItem.idFileType))
        //                          select catalogItem;

        //    int intCountCatalogItem = queryThumbnails.Count();
        //    int intCatalogItemCounter = 0;

        //    foreach (idCatalogItem catalogItem in queryThumbnails)
        //    {
        //        try
        //        {
        //            SaveImageThumbnailResult result = StaticFunctions.SaveImageThumbnail(catalogItem, ref db, ref dbThumbs, new List<String>() { type }, keepAspectRatio, setGenericVideoThumbnailOnError);
        //            foreach (Exception ex in result.Exceptions)
        //            {
        //                LogException(ex);
        //                Console.WriteLine(ex.ToString());
        //            }

        //            if (result.Exceptions.Count > 0)
        //            {
        //                LogFailedCatalogItem(catalogItem);
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            LogException(ex);
        //            LogFailedCatalogItem(catalogItem);
        //            Console.WriteLine(ex.ToString());
        //        }

        //        intCatalogItemCounter += 1;
        //        Console.CursorLeft = 0;
        //        Console.Write(String.Format("{0} of {1} T thumbnails generated", intCatalogItemCounter, intCountCatalogItem));
        //    }
        //}

        private static void GenerateThumbnails()
        {
            Boolean keepAspectRatio = Boolean.Parse(ConfigurationManager.AppSettings["KeepAspectRatio"]);
            Boolean setGenericVideoThumbnailOnError = Boolean.Parse(ConfigurationManager.AppSettings["SetGenericVideoThumbnailOnError"]);

            var commandLineArguments  = new CommandLineArguments();
            ICommandLineParser parser = new CommandLineParser();

            parser.ParseArguments(Environment.GetCommandLineArgs(), commandLineArguments);

            IDImagerDB db       = new IDImagerDB();
            IDImagerDB dbThumbs = new IDImagerDB();

            dbThumbs.Database.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["IDImagerThumbsEntities"].ConnectionString;

            List <String> imageFileExtensions = ConfigurationManager.AppSettings["ImageFileExtensions"].Split(new char[] { char.Parse(",") }).ToList();
            List <String> videoFileExtensions = ConfigurationManager.AppSettings["VideoFileExtensions"].Split(new char[] { char.Parse(",") }).ToList();

            var queryCatalogItem = from catalogItem in db.idCatalogItem.Include("idFilePath")
                                   where imageFileExtensions.Contains(catalogItem.idFileType) || videoFileExtensions.Contains(catalogItem.idFileType)
                                   select catalogItem;

            if (commandLineArguments.FromDateTime != DateTime.MinValue)
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.idCreated >= commandLineArguments.FromDateTime);
            }

            if (commandLineArguments.ToDateTime != DateTime.MinValue)
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.idCreated <= commandLineArguments.ToDateTime);
            }

            if (commandLineArguments.FileFilter != null)
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.idFileType.ToLower().Equals(commandLineArguments.FileFilter.ToLower()));
            }

            if (commandLineArguments.ImageGuid != null)
            {
                queryCatalogItem = queryCatalogItem.Where(x => x.GUID.Equals(commandLineArguments.ImageGuid, StringComparison.OrdinalIgnoreCase));
            }

            int intCountCatalogItem    = queryCatalogItem.Count();
            int intCatalogItemCounter  = 0;
            int intThumbnailsGenerated = 0;

            foreach (idCatalogItem catalogItem in queryCatalogItem)
            {
                List <String>   typesToGenerate = new List <String>();
                List <idThumbs> idThumbsT       = dbThumbs.idThumbs.Where(x => x.ImageGUID == catalogItem.GUID && x.idType.Equals("T")).ToList();
                List <idThumbs> idThumbsM       = dbThumbs.idThumbs.Where(x => x.ImageGUID == catalogItem.GUID && x.idType.Equals("M")).ToList();
                List <idThumbs> idThumbsR       = dbThumbs.idThumbs.Where(x => x.ImageGUID == catalogItem.GUID && x.idType.Equals("R")).ToList();

                if (idThumbsT.Count() == 0)
                {
                    typesToGenerate.Add("T");
                }

                if (idThumbsM.Count() == 0)
                {
                    typesToGenerate.Add("M");
                }

                if (catalogItem.idHasRecipe > 0 && idThumbsR.Count() == 0)
                {
                    typesToGenerate.Add("R");
                }

                if (commandLineArguments.Overwrite)
                {
                    foreach (idThumbs thumb in idThumbsT)
                    {
                        dbThumbs.idThumbs.Remove(thumb);
                    }

                    foreach (idThumbs thumb in idThumbsM)
                    {
                        dbThumbs.idThumbs.Remove(thumb);
                    }

                    foreach (idThumbs thumb in idThumbsR)
                    {
                        dbThumbs.idThumbs.Remove(thumb);
                    }

                    typesToGenerate.Clear();
                    typesToGenerate.Add("T");
                    typesToGenerate.Add("M");
                    if (catalogItem.idHasRecipe > 0)
                    {
                        typesToGenerate.Add("R");
                    }
                }

                if (typesToGenerate.Count() > 0)
                {
                    try
                    {
                        SaveImageThumbnailResult result = StaticFunctions.SaveImageThumbnail(catalogItem, ref db, ref dbThumbs, typesToGenerate, keepAspectRatio, setGenericVideoThumbnailOnError);
                        foreach (Exception ex in result.Exceptions)
                        {
                            LogException(ex);
                            Console.WriteLine(ex.ToString());
                        }

                        if (result.Exceptions.Count > 0)
                        {
                            LogFailedCatalogItem(catalogItem);
                        }

                        intThumbnailsGenerated += result.ImageStreams.Count();
                    }
                    catch (Exception e)
                    {
                        LogException(e);
                        LogFailedCatalogItem(catalogItem);
                        Console.WriteLine(e.ToString());
                    }
                }

                intCatalogItemCounter += 1;

                Console.CursorLeft = 0;
                Console.Write(String.Format("{0} of {1} catalogitems checked. {2} thumbnails generated", intCatalogItemCounter, intCountCatalogItem, intThumbnailsGenerated));
            }
        }
        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);
        }