public void TestFileNameIncrementation()
        {
            var incrementalRename = new IncrementalRename();
            var incrementedName   = incrementalRename.GetNewFileName("Fooo1.jpg");

            Assert.AreEqual("Fooo1(1).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo1(1).jpg");
            Assert.AreEqual("Fooo1(2).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo1(2).jpg");
            Assert.AreEqual("Fooo1(3).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo1(9).jpg");
            Assert.AreEqual("Fooo1(10).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo(_a)_1.jpg");
            Assert.AreEqual("Fooo(_a)_1(1).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo(a).jpg");
            Assert.AreEqual("Fooo(a)(1).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo_(1.jpg");
            Assert.AreEqual("Fooo_(1(1).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo1).jpg");
            Assert.AreEqual("Fooo1)(1).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo(1_1).jpg");
            Assert.AreEqual("Fooo(1_1)(1).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo(11_).jpg");
            Assert.AreEqual("Fooo(11_)(1).jpg", incrementedName);

            incrementedName = incrementalRename.GetNewFileName("Fooo(11a).jpg");
            Assert.AreEqual("Fooo(11a)(1).jpg", incrementedName);
        }
Esempio n. 2
0
        public void Sort(IEnumerable <string> imageFiles, string outputDirectory, IFileOperation fileOperation)
        {
            Logger.Trace("void Sort({outputDirectory})", outputDirectory);
            var totalNumberOfFiles  = imageFiles.Count();
            var currentFileNumber   = 0;
            var totalNumberOfPhotos = imageFiles.Count();

            foreach (var image in imageFiles)
            {
                currentFileNumber++;
                var classification = imageClassification.GetClassifiedFilePath(image);
                if (!classification.Success)
                {
                    Logger.Warn("Cannot classify file: {image}. File skipped.", image);
                    continue;
                }
                try
                {
                    int safetyBreak = 0;
                    while (true)
                    {
                        if (++safetyBreak > 10000)
                        {
                            Logger.Fatal("Cannot find new file name for file: {image}", image);
                            throw new InvalidOperationException($"Cannot find new file name for file: {image}");
                        }

                        string directoryPath = Path.Combine(outputDirectory,
                                                            classification.ClassifiedPath.Year, classification.ClassifiedPath.Month, classification.ClassifiedPath.Day);
                        if (!Directory.Exists(directoryPath))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }

                        Logger.Info("Copy file {currentFileNumber}/{totalNumberOfFiles} {sourceImage} to {destinationPath}", currentFileNumber, totalNumberOfFiles, Path.GetFileName(image), classification.ClassifiedPath.RelativePath);

                        var progressEventArgs = new SortingProgressChangedEventArgs
                        {
                            CurrentPhotoNumber  = currentFileNumber,
                            TotalNumberOfPhotos = totalNumberOfPhotos,
                            CancelSorting       = false
                        };
                        SortingProgressChanged?.Invoke(this, progressEventArgs);
                        if (progressEventArgs.CancelSorting)
                        {
                            Logger.Info("Sorting process cancelled!");
                            return;
                        }

                        var destinationFilePath = Path.Combine(outputDirectory, classification.ClassifiedPath.RelativePath);
                        if (!File.Exists(destinationFilePath))
                        {
                            if (!fileOperation.Process(image, destinationFilePath))
                            {
                                Logger.Error("Cannot process file: {filePath}", image);
                            }
                            break;
                        }

                        if (imageEquality.Equals(image, destinationFilePath))
                        {
                            Logger.Info("File {filePath} already exists at location {destinationFilePath}. Skip", image, destinationFilePath);
                            break;
                        }
                        var rename = new IncrementalRename();
                        classification.ClassifiedPath.FileName = rename.GetNewFileName(classification.ClassifiedPath.FileName);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error("Cannot copy image. Source: {sourcePath}. Destination: {destinationPath}", image, classification.ClassifiedPath.RelativePath);
                    Logger.Error(ex);
                }
            }
        }