Esempio n. 1
0
        public static void UpdateFinFieldsFromImage(string basePath, DatabaseFin fin)
        {
            // TODO:
            // Probably not the best "flag" to look at.  We're saving OriginalImageFilename in the database for newer fins
            if (string.IsNullOrEmpty(fin.OriginalImageFilename))
            {
                List <ImageMod> imageMods;
                bool            thumbOnly;
                string          originalFilename;
                float           normScale;

                string fullFilename = Path.Combine(basePath, fin.ImageFilename);

                PngHelper.ParsePngText(fullFilename, out normScale, out imageMods, out thumbOnly, out originalFilename);

                fin.ImageMods = imageMods;
                fin.Scale     = normScale;

                // This is a little hacky, but we're going to get the bottom directory name, and append that to
                // the filename below.
                var bottomDirectoryName = Path.GetFileName(Path.GetDirectoryName(fullFilename));

                if (!string.IsNullOrEmpty(originalFilename))
                {
                    originalFilename = Path.Combine(bottomDirectoryName, originalFilename);

                    // TODO Original isn't right -- need to replay imagemods, maybe?
                    fin.OriginalImageFilename = originalFilename;
                    fin.ImageFilename         = originalFilename;
                }
                // TODO: Save these changes back to the database?
            }
        }
Esempio n. 2
0
        public static string BackupDatabase(DarwinDatabase database)
        {
            string backupFileName = Options.CurrentUserOptions.CurrentSurveyArea
                                    + "_"
                                    + Path.GetFileNameWithoutExtension(database.Filename)
                                    + "_"
                                    + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss")
                                    + ".zip";

            // If the directories exist, CreateDirectory does nothing.
            Directory.CreateDirectory(Options.CurrentUserOptions.CurrentDarwinHome);
            Directory.CreateDirectory(Options.CurrentUserOptions.CurrentBackupsPath);

            string fullBackupName = Path.Combine(Options.CurrentUserOptions.CurrentBackupsPath, backupFileName);

            using (var zipToOpen = new FileStream(fullBackupName, FileMode.CreateNew))
            {
                using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
                {
                    ZipArchiveEntry databaseFile = archive.CreateEntryFromFile(database.Filename, Path.GetFileName(database.Filename));

                    foreach (var fin in database.AllFins)
                    {
                        DatabaseFin finToSave = new DatabaseFin(fin);
                        //UpdateFinFieldsFromImage(Options.CurrentUserOptions.CurrentSurveyAreaPath, finToSave);

                        var imageFilename = Path.Combine(Options.CurrentUserOptions.CurrentSurveyAreaPath, finToSave.ImageFilename);
                        if (File.Exists(imageFilename))
                        {
                            archive.CreateEntryFromFile(imageFilename, Path.GetFileName(imageFilename));
                        }


                        string originalImageFilename = (string.IsNullOrEmpty(fin.OriginalImageFilename)) ? GetOriginalImageFilenameFromPng(imageFilename) : fin.OriginalImageFilename;

                        if (!string.IsNullOrEmpty(originalImageFilename))
                        {
                            var fullOriginalImageFilename = Path.Combine(Options.CurrentUserOptions.CurrentSurveyAreaPath, originalImageFilename);
                            if (File.Exists(fullOriginalImageFilename))
                            {
                                archive.CreateEntryFromFile(fullOriginalImageFilename, Path.GetFileName(fullOriginalImageFilename));
                            }
                        }
                    }
                }
            }

            return(fullBackupName);
        }
Esempio n. 3
0
        public static DatabaseFin FullyLoadFin(DatabaseFin fin)
        {
            DatabaseFin finCopy = new DatabaseFin(fin);

            // TODO: Cache images?
            if (!string.IsNullOrEmpty(finCopy.ImageFilename))
            {
                CatalogSupport.UpdateFinFieldsFromImage(Options.CurrentUserOptions.CurrentSurveyAreaPath, finCopy);

                string fullImageFilename = Path.Combine(Options.CurrentUserOptions.CurrentSurveyAreaPath,
                                                        (string.IsNullOrEmpty(finCopy.OriginalImageFilename)) ? finCopy.ImageFilename : finCopy.OriginalImageFilename);

                if (File.Exists(fullImageFilename))
                {
                    var img = System.Drawing.Image.FromFile(fullImageFilename);

                    var bitmap = new Bitmap(img);
                    // TODO: Hack for HiDPI -- this should be more intelligent.
                    bitmap.SetResolution(96, 96);

                    finCopy.OriginalFinImage = new Bitmap(bitmap);

                    // TODO: Refactor this so we're not doing it every time, which is a little crazy
                    if (finCopy.ImageMods != null && finCopy.ImageMods.Count > 0)
                    {
                        bitmap = ModificationHelper.ApplyImageModificationsToOriginal(bitmap, finCopy.ImageMods);
                        // TODO: HiDPI hack
                        bitmap.SetResolution(96, 96);
                    }

                    finCopy.FinImage = bitmap;
                }
            }

            if (!string.IsNullOrEmpty(finCopy.OriginalImageFilename) && !File.Exists(finCopy.OriginalImageFilename))
            {
                finCopy.OriginalImageFilename = Path.Combine(Options.CurrentUserOptions.CurrentSurveyAreaPath, finCopy.OriginalImageFilename);
            }

            return(finCopy);
        }
Esempio n. 4
0
 public abstract void Delete(DatabaseFin fin);
Esempio n. 5
0
 public abstract void UpdateOutline(DatabaseFin data, bool preventInvalidate = false);
Esempio n. 6
0
 public abstract void UpdateIndividual(DatabaseFin data);
Esempio n. 7
0
 public abstract void Update(DatabaseFin data);
Esempio n. 8
0
 public abstract long Add(DatabaseFin data);
Esempio n. 9
0
        public static void SaveToDatabase(DarwinDatabase database, DatabaseFin databaseFin)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

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

            if (string.IsNullOrEmpty(databaseFin.OriginalImageFilename) || databaseFin.OriginalFinImage == null)
            {
                throw new ArgumentOutOfRangeException(nameof(databaseFin));
            }

            // First, copy the images to the catalog folder

            // Check the original image.  If we still have the actual original image file, just copy it.  If
            // not, then save the one we have in memory to the folder.

            string originalImageSaveAs = Path.Combine(Options.CurrentUserOptions.CurrentCatalogPath, Path.GetFileName(databaseFin.OriginalImageFilename));

            // If we already have an item in the database with the same filename, try a few others
            if (File.Exists(originalImageSaveAs))
            {
                originalImageSaveAs = FileHelper.FindUniqueFilename(originalImageSaveAs);
            }

            if (File.Exists(databaseFin.OriginalImageFilename))
            {
                File.Copy(databaseFin.OriginalImageFilename, originalImageSaveAs);
            }
            else
            {
                var imageFormat = BitmapHelper.GetImageFormatFromExtension(originalImageSaveAs);

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

            // Now save the modified image (or the original if for some reason we don't have the modified one)
            string modifiedImageSaveAs = Path.Combine(Options.CurrentUserOptions.CurrentCatalogPath,
                                                      Path.GetFileNameWithoutExtension(originalImageSaveAs) + AppSettings.DarwinModsFilenameAppendPng);

            if (databaseFin.FinImage != null)
            {
                databaseFin.FinImage.SaveAsCompressedPng(modifiedImageSaveAs);
            }
            else
            {
                databaseFin.OriginalFinImage.SaveAsCompressedPng(modifiedImageSaveAs);
            }

            // Now let's overwrite the filenames without any paths
            databaseFin.OriginalImageFilename = Path.GetFileName(originalImageSaveAs);
            databaseFin.ImageFilename         = Path.GetFileName(modifiedImageSaveAs);

            // Finally, add it to the database
            database.Add(databaseFin);
        }
Esempio n. 10
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);
                }
            }
        }