Beispiel #1
0
        public string FormatTemplate(RenameInfo renameInfo)
        {
            if (!IsInitialized)
            {
                InitializeTemplate();
            }

            string newName = template;

            foreach (var templateElement in templateElements)
            {
                string elementKey             = templateElement.Key;
                RenameTemplateElement element = templateElement.Value;

                string value = null;
                switch (element.Entry)
                {
                case ValidElementEntry.Extension:
                    value = renameInfo.Extension;
                    break;

                case ValidElementEntry.Language:
                    value = renameInfo.Language;
                    break;

                case ValidElementEntry.MovieFileName:
                    value = renameInfo.MovieFileName;
                    break;

                case ValidElementEntry.SubtitleGroup:
                    value = renameInfo.SubtitleGroup;
                    break;
                }

                if (element.Required && string.IsNullOrEmpty(value))
                {
                    throw new ArgumentException($"The template cannot be formated since required element {elementKey} is not specified");
                }

                if (!element.Required && string.IsNullOrEmpty(value))
                {
                    value = string.Empty;
                }
                else
                {
                    if (element.StartWithDot)
                    {
                        value = value.Insert(0, ".");
                    }
                }

                newName = newName.Replace(elementKey, value);
            }
            return(newName);
        }
Beispiel #2
0
        public static FileList GenerateRenameList(RenameOptions renameOptions)
        {
            FileList finalFileList = new FileList();

            if (renameOptions.SubtitleFileList.Count == 0 || renameOptions.MovieFileList.Count == 0)
            {
                throw new InvalidOperationException("Both list cannot be blank");
            }

            if (renameOptions.SubtitleFileList.Count % renameOptions.MovieFileList.Count != 0 || renameOptions.SubtitleFileList.Count < renameOptions.MovieFileList.Count)
            {
                throw new InvalidOperationException("File count error, the file count of subtitle / movie must be an integer");
            }

            RenameTemplate renameTemplate = new RenameTemplate(renameOptions.Template);

            renameTemplate.InitializeTemplate();

            // Create list
            int ratio = renameOptions.SubtitleFileList.Count / renameOptions.MovieFileList.Count;

            for (int i = 0; i < renameOptions.MovieFileList.Count; i++)
            {
                FileDataInfo movieFile = renameOptions.MovieFileList[i];
                for (int j = 0; j < ratio; j++)
                {
                    FileDataInfo subtitleFile = renameOptions.SubtitleFileList[i * ratio + j];
                    RenameInfo   renameInfo   = new RenameInfo()
                    {
                        MovieFileName = movieFile.FileName,
                        SubtitleGroup = renameOptions.SpecifySubtitleGroup ? renameOptions.SubtitleGroup : subtitleFile.ParseSubtitleGroup(),
                        Language      = renameOptions.SpecifyLanguage ? renameOptions.Language : subtitleFile.ParseLanguage(),
                        Extension     = renameOptions.SpecifyExtension ? renameOptions.Extension : subtitleFile.FileExtension
                    };

                    // Set folder to movie folder or original folder
                    string finalFilePath =
                        Path.Combine(
                            renameOptions.MoveToMovieFolder ? movieFile.FileFolder : subtitleFile.FileFolder,
                            renameTemplate.FormatTemplate(renameInfo));

                    if (finalFileList.Contains(finalFilePath))
                    {
                        throw new InvalidOperationException($"Duplicated final file path {subtitleFile.FileFullName} TO {finalFilePath}");
                    }
                    finalFileList.AddVirtualItem(finalFilePath);
                }
            }

            return(finalFileList);
        }