Example #1
0
        public void ImportIndexItemRemoveEscapedCharactersTest()
        {
            var structuredFileName = "yyyyMMdd_HHmmss_\\d.ext";
            var result             = ImportIndexItem.RemoveEscapedCharacters(structuredFileName);

            Assert.AreEqual("yyyyMMdd_HHmmss_.ext", result);
        }
Example #2
0
        internal async Task AddToQueryAndImportDatabaseAsync(
            ImportIndexItem importIndexItem,
            ImportSettingsModel importSettings)
        {
            if (!importSettings.IndexMode || _importQuery?.TestConnection() != true)
            {
                if (_appSettings.IsVerbose())
                {
                    _logger.LogInformation(" AddToQueryAndImportDatabaseAsync Ignored - " +
                                           $"IndexMode {importSettings.IndexMode} " +
                                           $"TestConnection {_importQuery?.TestConnection()}");
                }
                return;
            }

            // Add to Normal File Index database
            var query = new QueryFactory(new SetupDatabaseTypes(_appSettings), _query,
                                         _memoryCache, _appSettings, _logger).Query();
            await query !.AddItemAsync(importIndexItem.FileIndexItem);

            // Add to check db, to avoid duplicate input
            var importQuery = new ImportQueryFactory(new SetupDatabaseTypes(_appSettings), _importQuery, _console, _logger).ImportQuery();
            await importQuery.AddAsync(importIndexItem, importSettings.IsConsoleOutputModeDefault());

            await query.DisposeAsync();
        }
Example #3
0
        public void ImportIndexItemParse_OverWriteStructureFeature_Test()
        {
            var createAnImageNoExif = new CreateAnImageNoExif();
            var createAnImage       = new CreateAnImage();

            _appSettings.Structure = null;
            // Go to the default structure setting
            _appSettings.StorageFolder = createAnImage.BasePath;

            // Use a strange structure setting to overwrite
            var input = new ImportIndexItem(_appSettings)
            {
                SourceFullFilePath = createAnImageNoExif.FullFilePathWithDate,
                Structure          = "/HHmmss_yyyyMMdd.ext"
            };

            input.ParseDateTimeFromFileName();

            DateTime.TryParseExact(
                "20120101_123300",
                "yyyyMMdd_HHmmss",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out var answerDateTime);

            // Check if those overwrite is accepted
            Assert.AreEqual(answerDateTime, input.DateTime);

            new StorageHostFullPathFilesystem().FileDelete(createAnImageNoExif.FullFilePathWithDate);
        }
Example #4
0
 private async Task CreateSideCarFile(ImportIndexItem importIndexItem, bool xmpExistForThisFileType)
 {
     if (_appSettings.ExifToolImportXmpCreate && !xmpExistForThisFileType)
     {
         var exifCopy = new ExifCopy(_subPathStorage, _thumbnailStorage, _exifTool, new ReadMeta(_subPathStorage, _appSettings));
         await exifCopy.XmpSync(importIndexItem.FileIndexItem.FilePath);
     }
 }
Example #5
0
        public void ParseDateTimeFromFileName_Null()
        {
            var importItem = new ImportIndexItem {
                SourceFullFilePath = null
            };
            var dateTime = importItem.ParseDateTimeFromFileName();

            Assert.AreEqual(new DateTime(), dateTime);
        }
Example #6
0
        private ImportIndexItem ObjectCreateIndexItem(
            string inputFileFullPath,
            ExtensionRolesHelper.ImageFormat imageFormat,
            string fileHashCode,
            FileIndexItem fileIndexItem,
            int colorClassTransformation,
            long size)
        {
            var importIndexItem = new ImportIndexItem(_appSettings)
            {
                SourceFullFilePath = inputFileFullPath,
                DateTime           = fileIndexItem.DateTime,
                FileHash           = fileHashCode,
                FileIndexItem      = fileIndexItem,
                Status             = ImportStatus.Ok,
                FilePath           = fileIndexItem.FilePath,
                ColorClass         = fileIndexItem.ColorClass
            };

            // used for files without a Exif Date for example WhatsApp images
            if (fileIndexItem.DateTime.Year == 1)
            {
                importIndexItem.FileIndexItem.DateTime = importIndexItem.ParseDateTimeFromFileName();
                // used to sync exifTool and to let the user know that the transformation has been applied
                importIndexItem.FileIndexItem.Description = MessageDateTimeBasedOnFilename;
                // only set when date is parsed if not ignore update
                if (importIndexItem.FileIndexItem.DateTime.Year != 1)
                {
                    importIndexItem.DateTimeFromFileName = true;
                }
            }

            // Also add Camera brand to list
            importIndexItem.MakeModel = importIndexItem.FileIndexItem.MakeModel;

            // AddToDatabase is Used by the importer History agent
            importIndexItem.FileIndexItem.AddToDatabase = DateTime.UtcNow;
            importIndexItem.AddToDatabase = DateTime.UtcNow;

            importIndexItem.FileIndexItem.Size        = size;
            importIndexItem.FileIndexItem.FileHash    = fileHashCode;
            importIndexItem.FileIndexItem.ImageFormat = imageFormat;
            importIndexItem.FileIndexItem.Status      = FileIndexItem.ExifStatus.Ok;
            if (colorClassTransformation < 0)
            {
                return(importIndexItem);
            }

            // only when set in ImportSettingsModel
            var colorClass = (ColorClassParser.Color)colorClassTransformation;

            importIndexItem.FileIndexItem.ColorClass = colorClass;
            importIndexItem.ColorClass = colorClass;
            return(importIndexItem);
        }
Example #7
0
        public void ImportIndexItemParse_Structure_Fallback()
        {
            _appSettings.Structure = null;
            var input = new ImportIndexItem(_appSettings)
            {
                SourceFullFilePath = ".jpg"
            };
            var result = input.ParseDateTimeFromFileName();

            Assert.AreEqual(result, new DateTime());
        }
Example #8
0
 private void DeleteFileAfter(ImportSettingsModel importSettings,
                              ImportIndexItem importIndexItem)
 {
     // to move files
     if (!importSettings.DeleteAfter)
     {
         return;
     }
     if (_appSettings.IsVerbose())
     {
         _console.WriteLine($"🚮 Delete file: {importIndexItem.SourceFullFilePath}");
     }
     _filesystemStorage.FileDelete(importIndexItem.SourceFullFilePath);
 }
Example #9
0
        public Task <List <ImportIndexItem> > Preflight(List <string> inputFileFullPaths, ImportSettingsModel importSettings)
        {
            var results = new List <ImportIndexItem>();

            foreach (var inputFileFullPath in inputFileFullPaths)
            {
                // if the item fails
                var importIndexFileError = new ImportIndexItem {
                    FilePath           = "/" + FilenamesHelper.GetFileName(inputFileFullPath),
                    SourceFullFilePath = "~/temp/test",
                    FileHash           = "FAKE",
                    MakeModel          = "added if the item fails",
                    Status             = ImportStatus.FileError
                };

                // Check if extension is correct
                if (!ExtensionRolesHelper.IsExtensionSyncSupported(inputFileFullPath))
                {
                    results.Add(importIndexFileError);
                }

                // Check if the file is correct
                var imageFormat = ExtensionRolesHelper.GetImageFormat(
                    _selectorStorage.Get(SelectorStorage.StorageServices.HostFilesystem)
                    .ReadStream(inputFileFullPath, 160));

                if (!ExtensionRolesHelper.ExtensionSyncSupportedList.Contains($"{imageFormat}"))
                {
                    results.Add(importIndexFileError);
                }

                results.Add(new ImportIndexItem
                {
                    Id = 4,
                    SourceFullFilePath = inputFileFullPath,
                    FilePath           = inputFileFullPath,
                    Status             = ImportStatus.Ok,
                    FileHash           = "FAKE",
                    MakeModel          = "added okay",
                    FileIndexItem      = new FileIndexItem()
                    {
                        FileHash = "FAKE_OK", FilePath = inputFileFullPath
                    }
                });
            }
            PreflightList.AddRange(results);
            return(Task.FromResult(results));
        }
Example #10
0
        /// <summary>
        /// Add a new item to the imported database
        /// </summary>
        /// <param name="updateStatusContent">import database item</param>
        /// <param name="writeConsole">add icon to console</param>
        /// <returns>fail or success</returns>
        public virtual async Task <bool> AddAsync(ImportIndexItem updateStatusContent, bool writeConsole = true)
        {
            var dbContext = GetDbContext();

            updateStatusContent.AddToDatabase = DateTime.UtcNow;
            await dbContext.ImportIndex.AddAsync(updateStatusContent);

            await dbContext.SaveChangesAsync();

            if (writeConsole)
            {
                _console.Write("⬆️");
            }
            // removed MySqlException catch
            return(true);
        }
Example #11
0
        internal bool ExistXmpSidecarForThisFileType(ImportIndexItem importIndexItem)
        {
            if (string.IsNullOrEmpty(importIndexItem.SourceFullFilePath))
            {
                return(false);
            }

            // Support for include sidecar files
            var xmpSourceFullFilePath =
                ExtensionRolesHelper.ReplaceExtensionWithXmp(importIndexItem
                                                             .SourceFullFilePath);

            return(ExtensionRolesHelper.IsExtensionForceXmp(importIndexItem
                                                            .SourceFullFilePath) &&
                   _filesystemStorage.ExistFile(xmpSourceFullFilePath));
        }
Example #12
0
        public async Task AddAsync()
        {
            var expectedResult = new ImportIndexItem {
                FileHash = "TEST3"
            };
            var serviceScopeFactory = CreateNewScope();
            var scope     = serviceScopeFactory.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();

            await new ImportQuery(serviceScopeFactory, new FakeConsoleWrapper(), new FakeIWebLogger()).AddAsync(expectedResult);

            var queryFromDb = await dbContext.ImportIndex.FirstOrDefaultAsync(
                p => p.FileHash == expectedResult.FileHash);

            Assert.AreEqual(expectedResult.FileHash, queryFromDb.FileHash);
        }
Example #13
0
        public void ParseDateTimeFromFileNameWithSpaces_Test()
        {
            var input = new ImportIndexItem(new AppSettings())
            {
                SourceFullFilePath = Path.DirectorySeparatorChar + "2018 08 20 19 03 00.jpg"
            };

            input.ParseDateTimeFromFileName();

            DateTime.TryParseExact(
                "20180820_190300",
                "yyyyMMdd_HHmmss",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out var answerDateTime);

            Assert.AreEqual(answerDateTime, input.DateTime);
        }
Example #14
0
        public void ImportIndexItemParse_ParseDateTimeFromFileNameWithExtraFileNameBase_Test()
        {
            _appSettings.Structure = "/yyyyMMdd_HHmmss_{filenamebase}.ext";

            var input = new ImportIndexItem(_appSettings)
            {
                SourceFullFilePath = Path.DirectorySeparatorChar + "2018-07-26 19.45.23.jpg"
            };

            input.ParseDateTimeFromFileName();

            DateTime.TryParseExact(
                "20180726_194523",
                "yyyyMMdd_HHmmss",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out var answerDateTime);

            Assert.AreEqual(answerDateTime, input.DateTime);
        }
Example #15
0
        public void ImportIndexItemParse_ParseDateTimeFromFileName_AppendixUsedInConfig()
        {
            _appSettings.Structure = "/yyyyMMdd_HHmmss_\\d\\e\\f\\g.ext";

            var input = new ImportIndexItem(_appSettings)
            {
                SourceFullFilePath = Path.DirectorySeparatorChar + "20180726_194523.jpg"
            };

            input.ParseDateTimeFromFileName();

            DateTime.TryParseExact(
                "20180726_194523",
                "yyyyMMdd_HHmmss",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out var answerDateTime);

            Assert.AreEqual(answerDateTime, input.DateTime);
        }
Example #16
0
        public void ParseDateTimeFromFileName_Test()
        {
            _appSettings.Structure = "/yyyyMMdd_HHmmss.ext";

            var input = new ImportIndexItem(_appSettings)
            {
                SourceFullFilePath = Path.DirectorySeparatorChar + "20180101_011223.jpg"
            };

            input.ParseDateTimeFromFileName();

            DateTime.TryParseExact(
                "20180101_011223",
                "yyyyMMdd_HHmmss",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out var answerDateTime);

            Assert.AreEqual(answerDateTime, input.DateTime);
        }
Example #17
0
        public async Task History()
        {
            var expectedResult = new ImportIndexItem
            {
                AddToDatabase = DateTime.UtcNow,
                FileHash      = "TEST8"
            };
            var serviceScopeFactory = CreateNewScope();

            await new ImportQuery(serviceScopeFactory, new FakeConsoleWrapper(), new FakeIWebLogger()).AddAsync(expectedResult);

            var historyResult = new ImportQuery(serviceScopeFactory, new FakeConsoleWrapper(), new FakeIWebLogger()).History();

            if (!historyResult.Any())
            {
                throw new ArgumentNullException("should not be 0");
            }

            Assert.IsTrue(historyResult.Any(p => p.FileHash == "TEST8"));
        }
Example #18
0
        public void ImportIndexItemParse_ParseDateTimeFromFileName_WithExtraDotsInName_Test()
        {
            _appSettings.Structure = "/yyyyMMdd_HHmmss.ext";

            var input = new ImportIndexItem(_appSettings)
            {
                SourceFullFilePath = Path.DirectorySeparatorChar + "2018-02-03 18.47.35.jpg"
            };

            input.ParseDateTimeFromFileName();

            Regex pattern = new Regex("-|_| |;|\\.|:");
            var   output  = pattern.Replace("2018-02-03 18.47.35.jpg", string.Empty);

            DateTime.TryParseExact(
                "20180203_184735",
                "yyyyMMdd_HHmmss",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out var answerDateTime);

            Assert.AreEqual(answerDateTime, input.DateTime);
        }
Example #19
0
        private ImportIndexItem ApplyStructure(ImportIndexItem importIndexItem, string overwriteStructure)
        {
            importIndexItem.Structure = _appSettings.Structure;

            // Feature to overwrite structures when importing using a header
            // Overwrite the structure in the ImportIndexItem
            if (!string.IsNullOrWhiteSpace(overwriteStructure))
            {
                importIndexItem.Structure = overwriteStructure;
            }

            var structureService = new StructureService(_subPathStorage, importIndexItem.Structure);

            importIndexItem.FileIndexItem.ParentDirectory = structureService.ParseSubfolders(
                importIndexItem.FileIndexItem.DateTime, importIndexItem.FileIndexItem.FileCollectionName,
                FilenamesHelper.GetFileExtensionWithoutDot(importIndexItem.FileIndexItem.FileName));

            importIndexItem.FileIndexItem.FileName = structureService.ParseFileName(
                importIndexItem.FileIndexItem.DateTime, importIndexItem.FileIndexItem.FileCollectionName,
                FilenamesHelper.GetFileExtensionWithoutDot(importIndexItem.FileIndexItem.FileName));
            importIndexItem.FilePath = importIndexItem.FileIndexItem.FilePath;

            return(importIndexItem);
        }
Example #20
0
        internal async Task <ImportIndexItem> Importer(ImportIndexItem importIndexItem,
                                                       ImportSettingsModel importSettings)
        {
            if (importIndexItem.Status != ImportStatus.Ok)
            {
                return(importIndexItem);
            }

            // True when exist and file type is raw
            var xmpExistForThisFileType = ExistXmpSidecarForThisFileType(importIndexItem);

            if (xmpExistForThisFileType || (_appSettings.ExifToolImportXmpCreate &&
                                            ExtensionRolesHelper.IsExtensionForceXmp(importIndexItem.FilePath)))
            {
                // When a xmp file already exist (only for raws)
                // AND when this created afterwards with the ExifToolImportXmpCreate setting  (only for raws)
                importIndexItem.FileIndexItem.AddSidecarExtension("xmp");
            }

            // Add item to database
            await AddToQueryAndImportDatabaseAsync(importIndexItem, importSettings);

            // Copy
            if (_appSettings.IsVerbose())
            {
                _logger.LogInformation("[Import] Next Action = Copy" +
                                       $" {importIndexItem.SourceFullFilePath} {importIndexItem.FilePath}");
            }
            using (var sourceStream = _filesystemStorage.ReadStream(importIndexItem.SourceFullFilePath))
                await _subPathStorage.WriteStreamAsync(sourceStream, importIndexItem.FilePath);

            // Copy the sidecar file
            if (xmpExistForThisFileType)
            {
                var xmpSourceFullFilePath  = ExtensionRolesHelper.ReplaceExtensionWithXmp(importIndexItem.SourceFullFilePath);
                var destinationXmpFullPath = ExtensionRolesHelper.ReplaceExtensionWithXmp(importIndexItem.FilePath);
                _filesystemStorage.FileCopy(xmpSourceFullFilePath, destinationXmpFullPath);
            }

            await CreateSideCarFile(importIndexItem, xmpExistForThisFileType);

            // Run Exiftool to Update for example colorClass
            UpdateImportTransformations.QueryUpdateDelegate?updateItemAsync = null;
            if (importSettings.IndexMode)
            {
                updateItemAsync = new QueryFactory(
                    new SetupDatabaseTypes(_appSettings), _query,
                    _memoryCache, _appSettings, _logger).Query() !.UpdateItemAsync;
            }

            importIndexItem.FileIndexItem = await _updateImportTransformations.UpdateTransformations(updateItemAsync, importIndexItem.FileIndexItem,
                                                                                                     importSettings.ColorClass, importIndexItem.DateTimeFromFileName, importSettings.IndexMode);

            DeleteFileAfter(importSettings, importIndexItem);

            if (_appSettings.IsVerbose())
            {
                _console.Write("+");
            }
            return(importIndexItem);
        }
Example #21
0
 public async Task <bool> AddAsync(ImportIndexItem updateStatusContent, bool console = true)
 {
     _exist.Add(updateStatusContent.FileHash);
     return(true);
 }