public string Parse(string destinationPattern, string destinationPath, MetaData meta) { int index; if (!indexes.TryGetValue(meta, out index)) throw new InvalidOperationException("Meta data hasn't been preloaded"); string[] pattern = destinationPattern.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); string subPath = String.Empty; foreach (string subPattern in pattern) { Dictionary<string, string> replacements = new Dictionary<string, string>(); // Gather replacements string regex = @"(?<!%)%[a-zA-Z]"; foreach (Match match in Regex.Matches(subPattern, regex)) { if (replacements.ContainsKey(match.Value)) continue; string replacement = GetPatternReplacement(meta, index, match.Value); if (String.IsNullOrEmpty(replacement)) continue; replacements[match.Value] = replacement; } // Perform replacement string temp = subPattern; foreach (KeyValuePair<string, string> kvp in replacements) temp = Regex.Replace(temp, @"(?<!%)" + kvp.Key, kvp.Value); temp = temp.Replace("%%", "%"); // Remove escape character if (Regex.IsMatch(temp, "^" + regex + "$")) continue; // Pattern matched but not replaced: skip in path temp = temp.ReplaceInvalidPathChars(); subPath = Path.Combine(subPath, temp); } destinationPath = destinationPath.ReplaceInvalidPathChars(); return Path.Combine(destinationPath, subPath); }
private string CalculateDestinationPath(PatternPathParser parser, string destinationPath, MetaData meta) { string destinationPattern = null; switch (meta.Type) { case MetaType.Image: destinationPattern = DestinationPatternImage; break; case MetaType.Video: destinationPattern = DestinationPatternVideo; break; case MetaType.Music: destinationPattern = DestinationPatternAudio; break; default: throw new NotSupportedException(String.Format("Meta media type not supported: {0}", meta.Type)); } return parser.Parse(destinationPattern, destinationPath, meta); }
private string GetPatternReplacement(MetaData meta, int index, string subpattern) { GroupType groupType; if (!organizeGroups.TryGetValue(subpattern, out groupType)) throw new MediaOrganizerException("Unhandled pattern item: {0}", subpattern); switch (groupType) { case GroupType.Index: { return index.ToString(); } case GroupType.Year: { object temp; if (!meta.Data.TryGetValue(MetaKey.Timestamp, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.Timestamp, groupType); DateTime datetime = (DateTime)temp; return datetime.Year.ToString(); } case GroupType.MonthNumber: { object temp; if (!meta.Data.TryGetValue(MetaKey.Timestamp, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.Timestamp, groupType); DateTime datetime = (DateTime)temp; DateTimeFormatInfo dateinfo = locale.DateTimeFormat; return datetime.Month.ToString("D2"); } case GroupType.MonthName: { object temp; if (!meta.Data.TryGetValue(MetaKey.Timestamp, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.Timestamp, groupType); DateTime datetime = (DateTime)temp; DateTimeFormatInfo dateinfo = locale.DateTimeFormat; return dateinfo.MonthNames[datetime.Month - 1].UppercaseFirst(); } case GroupType.DayNumber: { object temp; if (!meta.Data.TryGetValue(MetaKey.Timestamp, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.Timestamp, groupType); DateTime datetime = (DateTime)temp; DateTimeFormatInfo dateinfo = locale.DateTimeFormat; return datetime.Day.ToString(); } case GroupType.DayName: { object temp; if (!meta.Data.TryGetValue(MetaKey.Timestamp, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.Timestamp, groupType); DateTime datetime = (DateTime)temp; DateTimeFormatInfo dateinfo = locale.DateTimeFormat; return dateinfo.DayNames[datetime.Day - 1].UppercaseFirst(); } case GroupType.FileName: { object temp; if (!meta.Data.TryGetValue(MetaKey.FileName, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.FileName, groupType); return Path.GetFileNameWithoutExtension((string)temp); } case GroupType.OriginalName: { object temp; if (!meta.Data.TryGetValue(MetaKey.OriginalName, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.OriginalName, groupType); return Path.GetFileName((string)temp); } case GroupType.FileExtension: { object temp; if (!meta.Data.TryGetValue(MetaKey.FileName, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.FileName, groupType); return Path.GetExtension((string)temp).Substring(1); } case GroupType.Tags: { //object temp; //if (!meta.Data.TryGetValue(MetaKey.Tags, out temp)) // throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.Tags, groupType); //string[] tags = temp as string[]; //if (tags == null || tags.Length == 0) // return null; //string tag = String.Join(", ", tags); string tag; if (!tags.TryGetValue(index, out tag)) return null; return tag; } case GroupType.Camera: { object temp; if (!meta.Data.TryGetValue(MetaKey.Camera, out temp)) throw new MediaOrganizerException("Failed to retrieve key '{0}' from meta data to parse group type '{1}'", MetaKey.Camera, groupType); return (string)temp; } default: throw new NotImplementedException(String.Format("GroupType: {0}", groupType)); } }
private CopyItem ParseItem(PatternPathParser parser, string destinationPath, MetaData meta) { string sourcePath = meta.Path; CopyItem item = new CopyItem(); item.sourceInfo = new FileInfo(sourcePath); item.sourcePath = sourcePath; item.destinationPath = CalculateDestinationPath(parser, destinationPath, meta); item.meta = meta.Data; return item; }