Exemple #1
0
        public static void CloseDatabase(DarwinDatabase db)
        {
            //db = null;
            SQLiteConnection.ClearAllPools();

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
Exemple #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);
        }
Exemple #3
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);
        }