public static List <string> FindSimilarNames(string path, string[] types, string[] otherFiles, out bool nameHasSuffix, out string pathWithoutSuffix) { // parse the file pathWithoutSuffix = null; nameHasSuffix = FindPathWithSuffixRemoved(path, types, out pathWithoutSuffix); // delete all the rest in group var nameWithoutSuffix = nameHasSuffix ? pathWithoutSuffix : path; List <string> results = new List <string>(); foreach (var otherFile in otherFiles) { if (otherFile.ToUpperInvariant() != path.ToUpperInvariant()) { if (FilenameUtils.SameExceptExtension(nameWithoutSuffix, otherFile) || (FindPathWithSuffixRemoved(otherFile, types, out string nameMiddleRemoved) && FilenameUtils.SameExceptExtension(nameWithoutSuffix, nameMiddleRemoved))) { results.Add(otherFile); } } } return(results); }
public void MakeBmp(string path, int clickX, int clickY, int widthOfResizedImage, int heightOfResizedImage, JpegRotationFinder shouldRotateImages) { Bmp.Dispose(); Bmp = new Bitmap(MaxWidth, MaxHeight, PixelFormat.Format32bppPArgb); if (path == null || !FilenameUtils.LooksLikeImage(path) || !File.Exists(path)) { return; } // we can disregard bitmapWillLockFile because we'll quickly dispose bitmapFull. using (Bitmap bitmapFull = ImageCache.GetBitmap(path, shouldRotateImages, out bool bitmapWillLockFile)) { if (bitmapFull.Width == 1 || bitmapFull.Height == 1) { return; } GetShiftAmount(bitmapFull, clickX, clickY, widthOfResizedImage, heightOfResizedImage, out int shiftX, out int shiftY); // draw the entire image, but pushed off to the side using (Graphics g = Graphics.FromImage(Bmp)) { g.FillRectangle(Brushes.White, 0, 0, MaxWidth, MaxHeight); g.DrawImageUnscaled(bitmapFull, -shiftX, -shiftY); } } }
// add MRU history, suggestions, and clipboard contents to the list of examples. public static IEnumerable <string> GetInputSuggestions(string currentSuggestion, InputBoxHistory historyKey, PersistMostRecentlyUsedList history, bool useClipboard, bool mustBeDirectory, string[] more) { List <string> suggestions = new List <string>(); if (!string.IsNullOrEmpty(currentSuggestion)) { suggestions.Add(currentSuggestion); } if (useClipboard && !string.IsNullOrEmpty(Utils.GetClipboard()) && Utils.LooksLikePath(Utils.GetClipboard()) == mustBeDirectory) { // get from clipboard if the right type of string (path vs not path) suggestions.Add(Utils.GetClipboard()); } if (historyKey != InputBoxHistory.None) { suggestions.AddRange(history.Get()); } if (more != null) { suggestions.AddRange(more); } return(suggestions.Where(entry => !mustBeDirectory || FilenameUtils.IsPathRooted(entry))); }
public void AutoAcceptSmallFiles(FormGallery form, int capJpg = 0, int capWebp = 0) { var list = form.GetFilelist().GetList(); // first, check for duplicate names foreach (var path in list) { var similar = FindSimilarFilenames.FindSimilarNames( path, GetFileTypes(), list, out bool nameHasSuffix, out string pathWithoutSuffix); if (similar.Count != 0) { Utils.MessageErr("the file " + path + " has similar name(s) " + string.Join(Utils.NL, similar)); return; } } // then, accept the small files if (capJpg == 0) { var optCapWebp = InputBoxForm.GetInteger("Accept webp files less than this many Kb:", 50); if (!optCapWebp.HasValue) { return; } var optCapJpg = InputBoxForm.GetInteger("Accept jpg files less than this many Kb:", 100); if (!optCapJpg.HasValue) { return; } capWebp = 1024 * optCapWebp.Value; capJpg = 1024 * optCapJpg.Value; } int countAccepted = 0; var sizeIsGoodCategory = GetDefaultCategories().Split(new char[] { '/' })[2]; foreach (var path in list) { var fileLength = new FileInfo(path).Length; if (fileLength > 0 && ((ModeUtils.IsWebp(path) && fileLength < capWebp) || (ModeUtils.IsJpg(path) && fileLength < capJpg))) { countAccepted++; var newPath = FilenameUtils.AddCategoryToFilename(path, sizeIsGoodCategory); form.WrapMoveFile(path, newPath); } } Utils.MessageBox("Accepted for " + countAccepted + " images.", true); }
static void TestMethod_UtilsGetCategory() { var testAdd = FilenameUtils.AddCategoryToFilename(PathSep("c:/dir/test/b b.aaa.jpg"), "mk"); TestUtil.IsEq(PathSep("c:/dir/test/b b.aaa__MARKAS__mk.jpg"), testAdd); testAdd = FilenameUtils.AddCategoryToFilename(PathSep("c:/dir/test/b b.aaa.jpg"), ""); TestUtil.IsEq(PathSep("c:/dir/test/b b.aaa__MARKAS__.jpg"), testAdd); Func <string, string> testGetCategory = (input) => { FilenameUtils.GetCategoryFromFilename(input, out string pathWithoutCategory, out string category); return(pathWithoutCategory + "|" + category); };
static void TestMethod_IsExtensionInList() { var exts = new string[] { ".jpg", ".png" }; TestUtil.IsTrue(!FilenameUtils.IsExtensionInList("", exts)); TestUtil.IsTrue(!FilenameUtils.IsExtensionInList("png", exts)); TestUtil.IsTrue(!FilenameUtils.IsExtensionInList("a.bmp", exts)); TestUtil.IsTrue(FilenameUtils.IsExtensionInList("a.png", exts)); TestUtil.IsTrue(FilenameUtils.IsExtensionInList("a.PNG", exts)); TestUtil.IsTrue(FilenameUtils.IsExtensionInList("a.jpg", exts)); TestUtil.IsTrue(FilenameUtils.IsExtensionInList("a.bmp.jpg", exts)); TestUtil.IsTrue(FilenameUtils.IsExt("a.png", ".png")); TestUtil.IsTrue(FilenameUtils.IsExt("a.PNG", ".png")); TestUtil.IsTrue(!FilenameUtils.IsExt("apng", ".png")); TestUtil.IsTrue(!FilenameUtils.IsExt("a.png", ".jpg")); }
public string[] GetList(bool forceRefresh = false, bool includeMarked = false) { Func <string, bool> includeFile = (path) => { if (!includeMarked && _excludeMarked && path.Contains(FilenameUtils.MarkerString)) { return(false); } else if (!FilenameUtils.IsExtensionInList(path, _extensionsAllowed)) { return(false); } else { return(true); } }; return(_list.GetList(forceRefresh).Where(includeFile).ToArray()); }
static ModeBase GuessModeBasedOnFileExtensions(IEnumerable <string> paths) { if (!Configs.Current.GetBool(ConfigKey.EnablePersonalFeatures)) { return(new ModeCategorizeAndRename()); } else if (paths.Any(path => FilenameUtils.LooksLikeImage(path))) { return(new ModeCategorizeAndRename()); } else if (paths.All(path => path.EndsWith(".wav", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".flac", StringComparison.OrdinalIgnoreCase))) { return(new ModeMarkWavQuality()); } else { return(new ModeMarkMp3Quality()); } }
static void TestMethod_NumberedPrefix() { TestUtil.IsEq(PathSep("c:/test/([0000])abc.jpg"), FilenameUtils.AddNumberedPrefix(PathSep("c:/test/abc.jpg"), 0)); TestUtil.IsEq(PathSep("c:/test/([0010])abc.jpg"), FilenameUtils.AddNumberedPrefix(PathSep("c:/test/abc.jpg"), 1)); TestUtil.IsEq(PathSep("c:/test/([1230])abc.jpg"), FilenameUtils.AddNumberedPrefix(PathSep("c:/test/abc.jpg"), 123)); TestUtil.IsEq(PathSep("c:/test/([1230])abc.jpg"), FilenameUtils.AddNumberedPrefix(PathSep("c:/test/([1230])abc.jpg"), 123)); TestUtil.IsEq(PathSep("c:/test/([9999])abc.jpg"), FilenameUtils.AddNumberedPrefix(PathSep("c:/test/([9999])abc.jpg"), 123)); TestUtil.IsEq(PathSep("a.jpg"), FilenameUtils.GetFileNameWithoutNumberedPrefix(PathSep("a.jpg"))); TestUtil.IsEq(PathSep("abc.jpg"), FilenameUtils.GetFileNameWithoutNumberedPrefix(PathSep("c:/test/([9999])abc.jpg"))); TestUtil.IsEq(PathSep("abc.jpg"), FilenameUtils.GetFileNameWithoutNumberedPrefix(PathSep("c:/test/([0000])abc.jpg"))); TestUtil.IsEq(PathSep("abc.jpg"), FilenameUtils.GetFileNameWithoutNumberedPrefix(PathSep("c:/test/([1230])abc.jpg"))); }
public Bitmap GetResizedBitmap(string path, out int originalWidth, out int originalHeight) { if (!FilenameUtils.LooksLikeImage(path) || !File.Exists(path)) { originalWidth = 0; originalHeight = 0; return(new Bitmap(1, 1, PixelFormat.Format32bppPArgb)); } Bitmap bitmapFull = GetBitmap(path, _shouldRotateThisImage, out bool bitmapWillLockFile); // resize and preserve ratio originalWidth = bitmapFull.Width; originalHeight = bitmapFull.Height; if (bitmapFull.Width > MaxWidth || bitmapFull.Height > MaxHeight) { using (bitmapFull) { var ratio = Math.Min((double)MaxWidth / bitmapFull.Width, (double)MaxHeight / bitmapFull.Height); int newWidth = (int)(bitmapFull.Width * ratio); int newHeight = (int)(bitmapFull.Height * ratio); return(ClassImageOps.ResizeImage(bitmapFull, newWidth, newHeight, this.ResizeToFit, path)); } } else if (bitmapWillLockFile) { // make a copy of the bitmap, otherwise the file remains locked using (bitmapFull) { return(new Bitmap(bitmapFull)); } } else { return(bitmapFull); } }
static void TestMethod_UtilsSameExceptExtension() { TestUtil.IsTrue(FilenameUtils.SameExceptExtension( "test6.jpg", "test6.jpg")); TestUtil.IsTrue(FilenameUtils.SameExceptExtension( "test6.jpg", "test6.png")); TestUtil.IsTrue(FilenameUtils.SameExceptExtension( "test6.jpg", "test6.BMP")); TestUtil.IsTrue(!FilenameUtils.SameExceptExtension( "test6.jpg", "test6.jpg.jpg")); TestUtil.IsTrue(!FilenameUtils.SameExceptExtension( "test6a.jpg", "test6.jpg")); TestUtil.IsTrue(FilenameUtils.SameExceptExtension( "aa.jpg.test6.jpg", "aa.jpg.test6.bmp")); TestUtil.IsTrue(!FilenameUtils.SameExceptExtension( "aa.jpg.test6.jpg", "aa.bmp.test6.jpg")); TestUtil.IsTrue(FilenameUtils.SameExceptExtension( PathSep("a/test6.jpg"), PathSep("a/test6.jpg"))); TestUtil.IsTrue(!FilenameUtils.SameExceptExtension( PathSep("a/test6.jpg"), PathSep("b/test6.jpg"))); }
public static bool IsJpg(string s) { return(FilenameUtils.IsExtensionInList(s, new string[] { ".jpg", ".jpeg" })); }
public static bool IsWebp(string s) { return(FilenameUtils.IsExtensionInList(s, new string[] { ".webp" })); }