Пример #1
0
        public void SyncFiles()
        {
            if (!_files.Any())
            {
                _communication.SendMessage("Nothing to sync.");
                return;
            }

            var analyser = new Mp3FileAnalyser(_files, _mask);
            var diffs = analyser.GetDifferences();

            if (!diffs.Any())
            {
                _communication.SendMessage("All files synced.");
                return;
            }

            var filesChanged = new List<string>();

            foreach (var fileDifferencese in diffs)
            {
                var fileNameChanger = new Mp3FileNameChanger(fileDifferencese.Mp3File, _mask, _saver, true);
                foreach (var diff in fileDifferencese.Diffs)
                {
                    if (string.IsNullOrWhiteSpace(diff.Value.FileNameValue) ^ string.IsNullOrWhiteSpace(diff.Value.TagValue))
                    {
                        fileDifferencese.Mp3File[diff.Key] = (diff.Value.FileNameValue != "")
                            ? diff.Value.FileNameValue
                            : diff.Value.TagValue;
                        fileNameChanger.AddTagReplacement(diff.Key, fileDifferencese.Mp3File[diff.Key]);
                    }
                    else
                    {
                        var data = new CommunicationWithUser().GetInfoFromUser(diff.Key, diff.Value, fileDifferencese.Mp3File, _communication);
                        switch (data.DataFrom)
                        {
                            case SyncActions.FromFileName:
                                fileDifferencese.Mp3File[diff.Key] = data.Data;
                                break;
                            case SyncActions.FromTags:
                                break;
                            case SyncActions.Manual:
                                fileDifferencese.Mp3File[diff.Key] = data.Data;
                                break;
                        }
                        fileNameChanger.AddTagReplacement(diff.Key, data.Data);
                    }
                }

                var fn = fileNameChanger.GetNewFileName();
                //filesChanged.Add(fileDifferencese.Mp3File.FilePath);
                var file = Path.GetDirectoryName(fileDifferencese.Mp3File.FilePath) + "\\" + fn + ".mp3";
                filesChanged.Add(file);
                var fData = new KeyValuePair<string, Dictionary<TagType, string>>(fileDifferencese.Mp3File.FilePath, fileDifferencese.Mp3File.GetId3Data());
                _data.Add(
                    new KeyValuePair<string, KeyValuePair<string, Dictionary<TagType, string>>>(file, fData));
                fileDifferencese.Mp3File.ChangeFileName(fn);
            }
            _filesChanged = filesChanged;
        }
Пример #2
0
        public void Test_AnalyserWithNotEnoughData()
        {
            var firstItem = new TestMp3File("1. name1 - title1.mp3") { TrackId = "1", Title = "title1", Artist = "name1" };
            var secondItem = new TestMp3File("2.  - title2.mp3") { TrackId = "1", Title = "title2", Artist = "name1" };
            var thirdItem = new TestMp3File("3. .mp3") { TrackId = "1", Title = "title3", Artist = "name3" };

            var files = new TestMp3File[]
            {
                firstItem,
                secondItem,
                thirdItem,
            };

            var analyser = new Mp3FileAnalyser(files, "{id}. {artist} - {title}");
            var diffs = analyser.GetDifferences().ToList();

            var expectedDiff1 = new FileDifferences(secondItem);
            expectedDiff1.Add(TagType.Id, new Diff { FileNameValue = "2", TagValue = "1" });
            expectedDiff1.Add(TagType.Artist, new Diff { FileNameValue = "", TagValue = "name1" });

            var expectedDiff2 = new FileDifferences(thirdItem);
            expectedDiff2.Add(TagType.Id, new Diff { FileNameValue = "3", TagValue = "1" });
            expectedDiff2.Add(TagType.Title, new Diff { FileNameValue = "", TagValue = "title3" });
            expectedDiff2.Add(TagType.Artist, new Diff { FileNameValue = "", TagValue = "name3" });

            var expectedDiffs = new List<FileDifferences>()
            {
                expectedDiff1,
                expectedDiff2,
            };

            Assert.AreEqual(expectedDiffs.Count, diffs.Count);

            for (var i = 0; i < diffs.Count; i++)
            {
                Assert.AreEqual(expectedDiffs[i].Mp3File, diffs[i].Mp3File);

                Assert.AreEqual(expectedDiffs[i].Diffs.Count, diffs[i].Diffs.Count);

                foreach (var diff in expectedDiffs[i].Diffs)
                {
                    Assert.AreEqual(expectedDiffs[i].Diffs[diff.Key].FileNameValue, diffs[i].Diffs[diff.Key].FileNameValue);
                    Assert.AreEqual(expectedDiffs[i].Diffs[diff.Key].TagValue, diffs[i].Diffs[diff.Key].TagValue);
                }
            }
        }
Пример #3
0
        public void SyncFiles()
        {
            if (!_files.Any())
            {
                _communication.SendMessage("Nothing to sync.");
                return;
            }

            var analyser = new Mp3FileAnalyser(_files, _mask);
            var diffs    = analyser.GetDifferences();

            if (!diffs.Any())
            {
                _communication.SendMessage("All files synced.");
                return;
            }

            var filesChanged = new List <string>();

            foreach (var fileDifferencese in diffs)
            {
                var fileNameChanger = new Mp3FileNameChanger(fileDifferencese.Mp3File, _mask, _saver, true);
                foreach (var diff in fileDifferencese.Diffs)
                {
                    if (string.IsNullOrWhiteSpace(diff.Value.FileNameValue) ^ string.IsNullOrWhiteSpace(diff.Value.TagValue))
                    {
                        fileDifferencese.Mp3File[diff.Key] = (diff.Value.FileNameValue != "")
                                                        ? diff.Value.FileNameValue
                                                        : diff.Value.TagValue;
                        fileNameChanger.AddTagReplacement(diff.Key, fileDifferencese.Mp3File[diff.Key]);
                    }
                    else
                    {
                        var data = new CommunicationWithUser().GetInfoFromUser(diff.Key, diff.Value, fileDifferencese.Mp3File, _communication);
                        switch (data.DataFrom)
                        {
                        case SyncActions.FromFileName:
                            fileDifferencese.Mp3File[diff.Key] = data.Data;
                            break;

                        case SyncActions.FromTags:
                            break;

                        case SyncActions.Manual:
                            fileDifferencese.Mp3File[diff.Key] = data.Data;
                            break;
                        }
                        fileNameChanger.AddTagReplacement(diff.Key, data.Data);
                    }
                }

                var fn = fileNameChanger.GetNewFileName();
                //filesChanged.Add(fileDifferencese.Mp3File.FilePath);
                var file = Path.GetDirectoryName(fileDifferencese.Mp3File.FilePath) + "\\" + fn + ".mp3";
                filesChanged.Add(file);
                var fData = new KeyValuePair <string, Dictionary <TagType, string> >(fileDifferencese.Mp3File.FilePath, fileDifferencese.Mp3File.GetId3Data());
                _data.Add(
                    new KeyValuePair <string, KeyValuePair <string, Dictionary <TagType, string> > >(file, fData));
                fileDifferencese.Mp3File.ChangeFileName(fn);
            }
            _filesChanged = filesChanged;
        }
Пример #4
0
        public static void Main(string[] args)
        {
            /*try
            {*/
            var argsManager = new ArgsManager(args);
            argsManager.CheckArgsValidity();

            var data = argsManager.ExtactArgs();
            Mp3File mp3;
            IEnumerable<IMp3File> mp3Files;
            switch (data.Action)
            {
                case ProgramAction.Analyse:
                    mp3Files = Directory.GetFiles(data.Path, "*.mp3").Select(file => new Mp3File(file, new FileSaver(file))).ToArray();
                    var pathAnalyser = new Mp3FileAnalyser(mp3Files, data.Mask);

                    var differences = pathAnalyser.GetDifferences();
                    new CommunicationWithUser().ShowDifferences(differences, new ConsoleCommunication());

                    foreach (var mp3File in mp3Files)
                    {
                        mp3File?.Dispose();
                    }

                    break;

                case ProgramAction.Mp3Edit:
                    using (mp3 = new Mp3File(data.Path, new FileSaver(data.Path)))
                    {
                        var tagsChanger = new Mp3TagChanger(mp3, data.Mask, new FileSaver(RollbackData));
                        tagsChanger.ChangeTags();
                    }
                    break;

                case ProgramAction.FileRename:
                    using (mp3 = new Mp3File(data.Path, new FileSaver(data.Path)))
                    {
                        var renamer = new Mp3FileNameChanger(mp3, data.Mask, new FileSaver(RollbackData));
                        var newFileName = renamer.GetNewFileName();
                        mp3.ChangeFileName(newFileName);
                    }
                    break;

                case ProgramAction.Sync:
                    mp3Files = Directory.GetFiles(data.Path, "*.mp3").Select(file => new Mp3File(file, new FileSaver(file))).ToArray();
                    var syncer = new Mp3Syncing(mp3Files, data.Mask, new ConsoleCommunication(), new FileSaver(RollbackData));
                    syncer.SyncFiles();

                    foreach (var mp3File in mp3Files)
                    {
                        mp3File?.Dispose();
                    }

                    break;

                case ProgramAction.Rollback:

                    using (var rollbackSystem = new RollbackManager(new FileSaver(RollbackData)))
                    {
                        var state = rollbackSystem.Rollback();

                        Console.WriteLine(
                            state.State == RollbackState.Fail
                                ? $"Error occured while trying doing rollback. Details : \n\tError:{state.FailReason}"
                                : $"Rollback success.");
                    }

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }

            Console.WriteLine("Done!");
            /*}
            catch (Exception e)
            {
            #if DEBUG
                Console.WriteLine("Exeption: \r\n{0} \r\n\nAt:\n{1}", e.Message, e.StackTrace);
            #else
                Console.WriteLine("Error occured: {0}", e.Message);
            #endif
            }
            finally
            {
                Console.ReadKey();
            }*/
        }