private static void CopyPhoto(PhotoInfo info) { try { //Console.WriteLine(string.Format("************************** Trying to copy file: {0}", info.NewPath)); var directory = Path.GetDirectoryName(info.NewPath); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } if (!File.Exists(info.NewPath)) { //Console.WriteLine(string.Format("[PhotoHelper] Copying file: {0}", info.NewPath)); File.Copy(info.PhotoMetadata.FilePath, info.NewPath); } else { info.NewPath = string.Format("{0}_{1}.jpg", info.NewPath, Guid.NewGuid()); File.Copy(info.PhotoMetadata.FilePath, info.NewPath); //Console.WriteLine(string.Format("[PhotoHelper] Copying file with Guid path: {0}", info.NewPath)); } } catch (Exception e) { Console.WriteLine(string.Format("[PhotoHelper] Error copying photo: {0}", e.Message)); notCopiedPhotos.Add(info.PhotoMetadata.FilePath); } }
private static void CacheLocationTimeSpan(PhotoInfo info) { /*if (!locationTimeSpan.ContainsKey(info.City)) * { * locationTimeSpan.Add(info.City, new Tuple<DateTime, DateTime, string>(info.PhotoMetadata.TakenDate, info.PhotoMetadata.TakenDate, info.Country)); * } * * var dateSpan = locationTimeSpan[info.City]; * //Console.WriteLine(string.Format("[PhotoHelper]###################### before city {0} adjusted time span {1}", info.City, JsonConvert.SerializeObject(dateSpan))); * * if (dateSpan.Item1 > info.PhotoMetadata.TakenDate && dateSpan.Item1.Subtract(info.PhotoMetadata.TakenDate).TotalDays < config.SpanLimitDays) * { * locationTimeSpan[info.City] = new Tuple<DateTime, DateTime, string>(info.PhotoMetadata.TakenDate, dateSpan.Item2, info.Country); * } * * if (dateSpan.Item2 < info.PhotoMetadata.TakenDate && info.PhotoMetadata.TakenDate.Subtract(dateSpan.Item2).TotalDays < config.SpanLimitDays) * { * locationTimeSpan[info.City] = new Tuple<DateTime, DateTime, string>(dateSpan.Item1, info.PhotoMetadata.TakenDate, info.Country); * }*/ //Console.WriteLine(string.Format("[PhotoHelper]###################### after city {0} adjusted time span {1}", info.City, JsonConvert.SerializeObject(info))); if (info.Node != null) { CacheNodeTimeSpan(info.Node, info); } }
private static object SelectPhotoInfoProperty(string pathPart, PhotoInfo info) { object result = string.Empty; switch (pathPart) { case PATH_COUNTRY: result = info.Country; break; case PATH_CITY: result = info.City; break; case PATH_DEVICE: result = info.PhotoMetadata.Model; break; case PATH_DATE: result = info.PhotoMetadata.TakenDate; break; default: result = string.Empty; break; } return(result); }
private static string CreateGPSNewPath(PhotoInfo info) { var format = string.Format("{0}/{1}.jpg", config.PhotoDestinationFolder, config.NewPhotoPathFormatGPS); var pathParams = GeneratePathParams(gpsFilePathParts, info); var result = string.Format(format, pathParams); // Console.WriteLine(result + " ********** " + pathParams.Length + " gpsFilePathParts count" + gpsFilePathParts.Count); return(result); //return string.Format(@"{0}/{1} {4:yyyy.MM}/{2}/{3}/{1}_{2}_{4:yyyy-MM-dd HH-mm-ss}.jpg", config.PhotoDestinationFolder, info.Country, info.City, info.PhotoMetadata.Model, info.PhotoMetadata.TakenDate); }
private static string CreateNoGPSNewPath(PhotoInfo info) { var format = string.Format("{0}/{1}.jpg", config.PhotoDestinationFolder, config.NewPhotoPathFormatNoneGPS); var pathParams = GeneratePathParams(noneGPSFilePathParts, info); var result = string.Format(format, pathParams); //Console.WriteLine(result + " ********** " + pathParams.Length + " noneGPSFilePathParts count" + gpsFilePathParts.Count); return(result); //return string.Format(@"{0}/!NoGPS/{1}/{2}/{2}_{3}.jpg", config.PhotoDestinationFolder, info.PhotoMetadata.TakenDate.ToString("yyyy-MM"), info.PhotoMetadata.Model, info.PhotoMetadata.TakenDate.ToString("yyyy-MM-dd HH-mm-ss")); }
private static void CreateNewPhotoPathWithLocation(PhotoInfo info) { if (info.PhotoMetadata.HasLocation && !string.IsNullOrWhiteSpace(info.City)) { //Console.WriteLine(string.Format("[PhotoHelper]###################### CreateNewPhotoPathWithLocation for city {0}. Info: {1}", info.City, JsonConvert.SerializeObject(info))); info.NewPath = CreateGPSNewPath(info); if (!IsCityExcluded(info.City) && !IsLocationExcluded(info.PhotoMetadata.GPS)) { CacheLocationTimeSpan(info); } } }
private static async Task <PhotoInfo> ReadMeta(string path) { var result = new PhotoInfo(); using (FileStream stream = new FileStream(path, FileMode.Open)) { var reader = new PhotoMetadataReader(stream); var meta = reader.ParseMetadata(); if (meta != null && !string.IsNullOrWhiteSpace(meta.Model)) { meta.FilePath = stream.Name; //Console.WriteLine(string.Format("[PhotoHelper]###################### ReadMeta for file {0}, model: {1}. meta: {2}", meta.FilePath, meta.Model, JsonConvert.SerializeObject(meta))); meta.Model = meta.Model.Trim('\x00'); //Console.WriteLine(string.Format("[PhotoHelper]###################### ReadMeta for file {0}. meta: {1}", meta.FilePath, JsonConvert.SerializeObject(meta))); result.PhotoMetadata = meta; if (meta.HasLocation) { var leafNode = await Cache.GetLeafNode(meta.GPS.LatRef, meta.GPS.Lat, meta.GPS.LonRef, meta.GPS.Lon).ConfigureAwait(false); if (leafNode != null) { var cityAndCountryName = GetCityAndCountryName(leafNode); result.City = cityAndCountryName.Item1; result.Country = cityAndCountryName.Item2; result.Node = leafNode; } if (string.IsNullOrWhiteSpace(result.City)) { noGPSPhotos.Add(result); meta.HasLocation = false; } } else { noGPSPhotos.Add(result); } } else { unprocessedPhotos.Add(path); } } return(result); }
private static void UpdateSpanTime(NodeTimeSpan span, PhotoInfo info) { //Console.WriteLine(string.Format("try to update span for {0}. left: {1}, right: {2}. PhotoTakenDate: {3}", span.Node.FormattedAddress, span.LeftDate.ToString("yyyy-MM-dd HH-mm-ss"), span.RightDate.ToString("yyyy-MM-dd HH-mm-ss"), info.PhotoMetadata.TakenDate.ToString("yyyy-MM-dd HH-mm-ss"))); if (span.LeftDate > info.PhotoMetadata.TakenDate && span.LeftDate.Subtract(info.PhotoMetadata.TakenDate).TotalDays < config.SpanLimitDays) { span.LeftDate = info.PhotoMetadata.TakenDate; //Console.WriteLine(string.Format("after update left span for {0}. left: {1}, right: {2}", span.Node.FormattedAddress, span.LeftDate.ToString("yyyy-MM-dd HH-mm-ss"), span.RightDate.ToString("yyyy-MM-dd HH-mm-ss"))); } if (span.RightDate < info.PhotoMetadata.TakenDate && info.PhotoMetadata.TakenDate.Subtract(span.RightDate).TotalDays < config.SpanLimitDays) { span.RightDate = info.PhotoMetadata.TakenDate; //Console.WriteLine(string.Format("after update right span for {0}. left: {1}, right: {2}", span.Node.FormattedAddress, span.LeftDate.ToString("yyyy-MM-dd HH-mm-ss"), span.RightDate.ToString("yyyy-MM-dd HH-mm-ss"))); } }
private static NodeTimeSpan CacheNodeTimeSpan(Node node, PhotoInfo info) { if (node.Parent == null) { var topNode = node; //Console.WriteLine(string.Format("########## parent is null ############### try to cache time span {0} for {1}", node.FormattedAddress, info.PhotoMetadata.FilePath)); if (!nodeTimeSpanCache.ContainsKey(topNode.PlaceId)) { NodeTimeSpan span = new NodeTimeSpan(); span.Node = topNode; span.LeftDate = info.PhotoMetadata.TakenDate; span.RightDate = info.PhotoMetadata.TakenDate; nodeTimeSpanCache.Add(topNode.PlaceId, span); } var topNodeSpan = nodeTimeSpanCache[topNode.PlaceId]; UpdateSpanTime(topNodeSpan, info); return(topNodeSpan); } else { var parentNode = node.Parent; //Console.WriteLine(string.Format("########## parent is not null ############### try to cache time span {0} for {1}", node.FormattedAddress, info.PhotoMetadata.FilePath)); var parentNodeSpan = CacheNodeTimeSpan(parentNode, info); if (!parentNodeSpan.Children.ContainsKey(node.PlaceId)) { NodeTimeSpan span = new NodeTimeSpan(); span.Node = node; span.LeftDate = info.PhotoMetadata.TakenDate; span.RightDate = info.PhotoMetadata.TakenDate; parentNodeSpan.Children.Add(node.PlaceId, span); } var childSpan = parentNodeSpan.Children[node.PlaceId]; UpdateSpanTime(childSpan, info); return(parentNodeSpan); } }
private static void AppendLocationForNoneGPSPhotos(PhotoInfo info) { /*foreach (var item in locationTimeSpan) * { * var leftTime = item.Value.Item1.Subtract(tolerance); * var rightTime = item.Value.Item2.Add(tolerance); * * //var leftTime = item.Value.Item1; * //var rightTime = item.Value.Item2; * * Console.WriteLine(string.Format("[PhotoHelper]###################### try to append location {0} to {1}. Photo Date {2}, left time: {3}, right time: {4}", info.City, info.PhotoMetadata.FilePath, info.PhotoMetadata.TakenDate.ToString("yyyy-MM-dd HH-mm-ss"), leftTime.ToString("yyyy-MM-dd HH-mm-ss"), rightTime.ToString("yyyy-MM-dd HH-mm-ss"))); * * if (info.PhotoMetadata.TakenDate > leftTime && info.PhotoMetadata.TakenDate < rightTime) * { * info.City = item.Key; * info.Country = item.Value.Item3; * * Console.WriteLine(string.Format("[PhotoHelper]###################### append location {0} to {1}. Photo Date {2}, left time: {3}, right time: {4}", info.City, info.PhotoMetadata.FilePath, info.PhotoMetadata.TakenDate.ToString("yyyy-MM-dd HH-mm-ss"), leftTime.ToString("yyyy-MM-dd HH-mm-ss"), rightTime.ToString("yyyy-MM-dd HH-mm-ss"))); * * break; * } * }*/ // Console.WriteLine(string.Format("try to append location for {0}", info.PhotoMetadata.FilePath)); foreach (var item in nodeTimeSpanCache) { var node = MatchNodeByTimeSpan(info, item.Value); if (node != null) { //Console.WriteLine(string.Format("matched top level {0} for {1}", node.FormattedAddress, info.PhotoMetadata.FilePath)); var cityAndCountryName = GetCityAndCountryName(node); info.City = cityAndCountryName.Item1; info.Country = cityAndCountryName.Item2; info.Node = node; break; } } }
public static Node MatchNodeByTimeSpan(PhotoInfo info, NodeTimeSpan span) { //Console.WriteLine(string.Format("try to match {0} for {1}", span.Node.FormattedAddress, info.PhotoMetadata.FilePath)); Node result = null; var tolerance = new TimeSpan(0, config.TimeTolerance, 0); var leftTime = span.LeftDate.Subtract(tolerance); var rightTime = span.RightDate.Add(tolerance); //var leftTime = span.LeftDate; //var rightTime = span.RightDate; //Console.WriteLine(string.Format("[PhotoHelper]###################### node span details: {0}", JsonConvert.SerializeObject(span))); //Console.WriteLine(string.Format("[PhotoHelper]###################### try to append location {0} to {1}. Photo Date {2}, left time: {3}, right time: {4}", span.Node.FormattedAddress, info.PhotoMetadata.FilePath, info.PhotoMetadata.TakenDate.ToString("yyyy-MM-dd HH-mm-ss"), leftTime.ToString("yyyy-MM-dd HH-mm-ss"), rightTime.ToString("yyyy-MM-dd HH-mm-ss"))); if (info.PhotoMetadata.TakenDate > leftTime && info.PhotoMetadata.TakenDate < rightTime) { result = span.Node; //Console.WriteLine(string.Format("matched {0} for {1}", span.Node.FormattedAddress, info.PhotoMetadata.FilePath)); if (span.Children.Count > 0) { foreach (var childSpan in span.Children) { var childResult = MatchNodeByTimeSpan(info, childSpan.Value); if (childResult != null) { //Console.WriteLine(string.Format("matched child level {0} for {1}", span.Node.FormattedAddress, info.PhotoMetadata.FilePath)); result = childResult; break; } } } } return(result); }
private static object[] GeneratePathParams(Dictionary <int, string> pathParts, PhotoInfo info) { object[] results = new object[pathParts.Count]; for (var i = 0; i < pathParts.Count; i++) { results.SetValue(SelectPhotoInfoProperty(pathParts[i], info), i); } return(results); }