Exemple #1
0
        public static string SaveFinz(CatalogScheme catalogScheme, DatabaseFin fin, string filename, bool forceFilename = true)
        {
            if (catalogScheme == null)
            {
                throw new ArgumentNullException(nameof(catalogScheme));
            }

            if (fin == null)
            {
                throw new ArgumentNullException(nameof(fin));
            }

            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException(nameof(filename));
            }

            if (!filename.ToLower().EndsWith(".finz"))
            {
                filename += ".finz";
            }

            string uniqueDirectoryName = Path.GetFileName(filename).Replace(".", string.Empty) + "_" + Guid.NewGuid().ToString().Replace("-", string.Empty);
            string fullDirectoryName   = Path.Combine(Path.GetTempPath(), uniqueDirectoryName);

            try
            {
                Directory.CreateDirectory(fullDirectoryName);

                string originalDestination = Path.Combine(fullDirectoryName, Path.GetFileName(fin.OriginalImageFilename));

                if (File.Exists(fin.OriginalImageFilename))
                {
                    File.Copy(fin.OriginalImageFilename, originalDestination);
                }
                else if (fin.OriginalFinImage != null)
                {
                    var imageFormat = BitmapHelper.GetImageFormatFromExtension(originalDestination);

                    if (imageFormat == ImageFormat.Png)
                    {
                        fin.OriginalFinImage.SaveAsCompressedPng(originalDestination);
                    }
                    else
                    {
                        fin.OriginalFinImage.Save(originalDestination, imageFormat);
                    }
                }

                fin.OriginalImageFilename = originalDestination;

                // replace ".finz" with "_wDarwinMods.png" for modified image filename

                fin.ImageFilename = Path.Combine(fullDirectoryName, Path.GetFileNameWithoutExtension(filename) + AppSettings.DarwinModsFilenameAppendPng);

                //fin.FinImage.SaveAsCompressedPng(fin.ImageFilename);

                // Saving a thumbnail to save disk space.  We'll reconstruct this based on image mods when we open
                // it back up.
                var finImageThumbnail = BitmapHelper.ResizeKeepAspectRatio(fin.FinImage, FinzThumbnailMaxDim, FinzThumbnailMaxDim);
                finImageThumbnail.SaveAsCompressedPng(fin.ImageFilename);

                string dbFilename = Path.Combine(fullDirectoryName, "database.db");

                if (catalogScheme.Categories == null)
                {
                    catalogScheme.Categories = new ObservableCollection <Category>();
                }

                if (!catalogScheme.Categories.ToList().Exists(c => c != null && c.Name?.ToUpper() == fin.DamageCategory.ToUpper()))
                {
                    catalogScheme.Categories.Add(new Category(fin.DamageCategory));
                }

                SQLiteDatabase db = new SQLiteDatabase(dbFilename, catalogScheme, true);
                db.Add(fin);

                // The below before we try to create a ZIP is because SQLite tries to hold onto the database file
                // even after the connections are closed
                SQLiteConnection.ClearAllPools();

                GC.Collect();
                GC.WaitForPendingFinalizers();

                string realFilename = filename;
                if (!forceFilename)
                {
                    realFilename = FileHelper.FindUniqueFilename(realFilename);
                }
                else if (File.Exists(realFilename))
                {
                    File.Delete(realFilename);
                }

                ZipFile.CreateFromDirectory(fullDirectoryName, realFilename);

                return(realFilename);
            }
            finally
            {
                try
                {
                    Trace.WriteLine("Trying to remove temporary files for finz file.");

                    SQLiteConnection.ClearAllPools();

                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    if (Directory.Exists(fullDirectoryName))
                    {
                        Directory.Delete(fullDirectoryName, true);
                    }
                }
                catch (Exception ex)
                {
                    Trace.Write("Couldn't remove temporary files:");
                    Trace.WriteLine(ex);
                }
            }
        }