private bool isImageFile(string path) { bool result = false; System.Drawing.Image image = null; System.Drawing.Graphics graphics = null; try { image = System.Drawing.Image.FromFile(path); result = true; graphics = System.Drawing.Graphics.FromImage(image); } catch (System.Exception ex) { logException(ex); FileWriterWithAppendMode.GlobalWrite(path); } finally { if (graphics != null) { graphics.Dispose(); } if (image != null) { image.Dispose(); } } return(result); }
private void DeleteDirectoryWithoutException(string directoryPath,ThreadParameters parameters) { try { FileWriterWithAppendMode.GlobalWrite(CultureStrings.TryDeleting+" "+singleQuoteText(directoryPath)); string[]subDirectories=System.IO.Directory.GetDirectories(directoryPath); foreach(string subDirectory in subDirectories) { if(!parameters.SourceDirectories.Any(sourceDirectory=>sourceDirectory.StartsWith(subDirectory))) { DeleteDirectoryWithoutException(subDirectory,parameters); } } subDirectories=System.IO.Directory.GetDirectories(directoryPath); string[]files=System.IO.Directory.GetFiles(directoryPath); if(onlyOneThumbsDbFile(files)) { deleteFileWithoutException(files[0],parameters); } files=System.IO.Directory.GetFiles(directoryPath); if(subDirectories.Length==0&&files.Length==0) { System.IO.Directory.Delete(directoryPath); FileWriterWithAppendMode.GlobalWrite(CultureStrings.TextDeleted+" "+singleQuoteText(directoryPath)); } } catch(System.Exception ex) { FileWriterWithAppendMode.GlobalWrite(CultureStrings.TextFailedInDeleting+" "+singleQuoteText(directoryPath)); logException(ex); } }
private string getPropertyValue(string imagePath, System.Drawing.Image image, int id) { try { if (image.PropertyIdList.Contains(id)) { return(getPropertyValue(image.GetPropertyItem(id))); } else { TagLib.Image.File tagImage = TagLib.File.Create(imagePath) as TagLib.Image.File; string result = null; if (id == manufacturerId && tagImage.ImageTag.Make != null && tagImage.ImageTag.Make.Length > 0) { result = tagImage.ImageTag.Make; } else if (id == modelId && tagImage.ImageTag.Model != null && tagImage.ImageTag.Model.Length > 0) { result = tagImage.ImageTag.Model; } else if (id == dateTakenId && tagImage.ImageTag.DateTime.HasValue) { result = tagImage.ImageTag.DateTime.Value.ToString(CultureStrings.FormatDateTaken); } else if (id == dateTakenId && !tagImage.ImageTag.DateTime.HasValue) { result = getPropertyValue(image.GetPropertyItem(dateTimeId)); } else if (id == latitudeLongitudeId && tagImage.ImageTag.Latitude.HasValue && tagImage.ImageTag.Longitude.HasValue) { result = tagImage.ImageTag.Latitude + "," + tagImage.ImageTag.Longitude; } tagImage.Dispose(); if (result != null) { return(result); } else { FileWriterWithAppendMode.GlobalWrite(string.Format(CultureStrings.InfomationImageProperties, doubleQuoteText(imagePath), id, convertIntsToText(image.PropertyIdList))); } } } catch (System.Exception ex) { logException(ex); } return(null); }
private void deleteFileWithoutException(string filePath,ThreadParameters parameters) { try { System.IO.FileInfo fileInfo=new System.IO.FileInfo(filePath); if(fileInfo.IsReadOnly&¶meters.DeleteReadOnly) { fileInfo.IsReadOnly=false; } if(fileInfo.Exists) { fileInfo.Delete(); } FileWriterWithAppendMode.GlobalWrite(CultureStrings.TextDeleted+" "+singleQuoteText(filePath)); } catch(System.Exception ex) { FileWriterWithAppendMode.GlobalWrite(CultureStrings.TextFailedInDeleting+" "+singleQuoteText(filePath)); logException(ex); } }
private void getFiles(string directoryPath, string searchPattern, System.IO.SearchOption searchOption, int deepnessLimitation, ThreadParameters parameters) { FileWriterWithAppendMode.GlobalWrite(string.Format("getFiles, directory path: {0}, deepness limitation: {1}.", doubleQuoteText(directoryPath), deepnessLimitation)); string[] fileExtensions = parameters.FilterWhileSearching?parameters.ImageFileExtensions.Concat(parameters.IncludeFileExtensions).ToArray():null; string resultFilePath = parameters.LogFilePath_ThreadResult; if (backgroundWorker_Search.CancellationPending) { throw new System.Exception("Cancel getFiles!"); } if (backgroundWorker_Search.IsBusy) { backgroundWorker_Search.ReportProgress(0, directoryPath); } if (deepnessLimitation <= 0) { return; } string[] topDirectoryFiles = getFiles(directoryPath, searchPattern); if (topDirectoryFiles != null) { appendLines(resultFilePath, filterFilePaths(topDirectoryFiles, fileExtensions)); } if (searchOption == System.IO.SearchOption.AllDirectories) { string[] directories = getDirectories(directoryPath); if (directories != null) { foreach (string directory in directories) { if (isMatchingDirectoryPath(directory, parameters.BypassRegexes)) { continue; } getFiles(directory, searchPattern, searchOption, deepnessLimitation - 1, parameters); } } } }
private ThreadParameters copyFiles(ThreadParameters parameters) { System.Collections.Generic.List <string> sourcePaths = new System.Collections.Generic.List <string>(); if (parameters.SourcePaths != null) { sourcePaths.AddRange(parameters.SourcePaths); } FileWriterWithAppendMode sourcePathLogger = null; FileWriterWithAppendMode deletedSourcePathLogger = null; FileWriterWithAppendMode destinationPathLogger = null; lock (copyFilesJobs) { sourcePathLogger = FileWriterWithAppendMode.Open(buildCollectPhotoLogFilePath(parameters.DestinationRootDirectory, CultureStrings.LogFileCopiedSourcePaths)); } lock (copyFilesJobs) { destinationPathLogger = FileWriterWithAppendMode.Open(buildCollectPhotoLogFilePath(parameters.DestinationRootDirectory, CultureStrings.LogFileCopiedDestinationPaths)); } lock (copyFilesJobs) { deletedSourcePathLogger = FileWriterWithAppendMode.Open(buildCollectPhotoLogFilePath(parameters.DestinationRootDirectory, CultureStrings.LogFileToBeDeletedSourcePaths)); } ThreadProgress threadProgress = new ThreadProgress(); resetThreadProgress(ref threadProgress, sourcePaths.Count, sourcePathLogger.FilePath, deletedSourcePathLogger.FilePath, destinationPathLogger.FilePath); if (parameters.LogFilePath_ThreadResult == null) { threadProgress.ProgressBarStyle = System.Windows.Forms.ProgressBarStyle.Marquee; backgroundWorker_Copy.ReportProgress(0, threadProgress); parameters = filterSourcePaths(searchDirectories(parameters)); } if (System.IO.File.Exists(parameters.LogFilePath_ThreadResult)) { sourcePaths.AddRange(System.IO.File.ReadAllLines(parameters.LogFilePath_ThreadResult, System.Text.Encoding.Unicode).ToList()); } threadProgress.CountTotal = sourcePaths.Count; threadProgress.ProgressBarStyle = System.Windows.Forms.ProgressBarStyle.Blocks; for (int i = 0; i < sourcePaths.Count; i++) { string sourcePath = sourcePaths[i]; string destinationPath = null; threadProgress.SourcePath = sourcePath; bool isCopy = false; bool isOverwriteCopy = false; bool alreadyExists = false; try { if (isMatchingFilePath(sourcePath, parameters.IncludeFileExtensions) || isMatchingFilePath(sourcePath, parameters.IncludeFileExtensionAndFileNameMap)) { destinationPath = buildImageDestinationFilePath(parameters, sourcePath, null); preCopyFile(parameters, sourcePath, ref destinationPath, ref alreadyExists, ref isCopy); } else if (isMatchingFilePath(sourcePath, parameters.ExcludeFileExtensions) || isMatchingFilePath(sourcePath, parameters.IncludeFileExtensionAndFileNameMap.Keys.ToArray())) { } else if (isImageFile(sourcePath)) { bool isLowGradeImage = false; using (System.Drawing.Image image = getImage(sourcePath)) { isLowGradeImage = !isImageLargerThanMinDimension(parameters.FileNameComponent, image); destinationPath = buildImageDestinationFilePath(parameters, sourcePath, image); } if (parameters.CopyLowGradeImage && (isLowGradeImage || destinationPath == null)) { destinationPath = buildUniqueDestinationFilePath(parameters, sourcePath); preCopyFile(parameters, sourcePath, ref destinationPath, ref alreadyExists, ref isCopy); } else if (destinationPath != null) { preCopyFile(parameters, sourcePath, ref destinationPath, ref alreadyExists, ref isCopy); if (!alreadyExists && isCopy && System.IO.File.Exists(destinationPath) && getFileSize(destinationPath) < getFileSize(sourcePath)) { isOverwriteCopy = true; } } } if (parameters.DeleteSourceIfDestinationAlreadyExists && alreadyExists && areTwoCopies(sourcePath, destinationPath)) { deletedSourcePathLogger.WriteLine(sourcePath); deletedSourcePathLogger.WriteLine(destinationPath); DeleteFileAndRelatedDirectoriesWithoutException(sourcePath, parameters); reportSkippedCopy(ref threadProgress, destinationPath, i); } else if (isCopy) { if (isOverwriteCopy) { overwriteFile(sourcePath, destinationPath); } else if (parameters.DeleteAfterCopy) { createDirectoryForFile(destinationPath); System.IO.File.Move(sourcePath, destinationPath); DeleteFileAndRelatedDirectoriesWithoutException(sourcePath, parameters); } else { copyFile(sourcePath, destinationPath); } sourcePathLogger.WriteLine(sourcePath); destinationPathLogger.WriteLine(destinationPath); reportSuccessCopy(ref threadProgress, destinationPath, i); } else { reportSkippedCopy(ref threadProgress, destinationPath, i); } } catch (System.Exception ex) { logException(ex); reportSkippedCopy(ref threadProgress, destinationPath, i); } sourcePathLogger.Flush(); destinationPathLogger.Flush(); deletedSourcePathLogger.Flush(); } sourcePathLogger.Close(); destinationPathLogger.Close(); deletedSourcePathLogger.Close(); FileWriterWithAppendMode.GlobalWrite(threadProgress.ToString()); return(parameters); }