Exemple #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;
        }
        public void Test_NewFileName()
        {
            var mp3 = new TestMp3File("C:/test/mp3s/1. asd - qwe.mp3") {Artist = "art", TrackId = "1", Title = "ttl"};
            var renamer = new Mp3FileNameChanger(mp3, "{id}. {artist} - {title}", new TestRollbackSaver(new [] {""}));

            Assert.AreEqual("1. art - ttl", renamer.GetNewFileName());
        }
        public void Test_ManualFileNameReplacements()
        {
            var mp3 = new TestMp3File("C:/test/mp3s/1. asd - qwe.mp3") { Artist = "art", TrackId = "1", Title = "ttl" };
            var renamer = new Mp3FileNameChanger(mp3, "{id}. {artist} - {title}", new TestRollbackSaver(new[] { "" }));
            renamer.AddTagReplacement(TagType.Id, mp3.TrackId);
            renamer.AddTagReplacement(TagType.Artist, mp3.Artist);
            renamer.AddTagReplacement(TagType.Title, mp3.Title);

            Assert.AreEqual("1. art - ttl", renamer.GetNewFileName());
        }
Exemple #4
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;
        }
Exemple #5
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();
            }*/
        }