/// <summary> /// History is made of simple strings that represent enough information to load a file: /// - The picture full name /// - The archive file (if any) from which the file derives /// </summary> /// <param name="tre"></param> public bool LoadPicture(TextRepresentationEntry tre) { if (tre.ArchiveFile == String.Empty) { //it's just a regular file, go load it return(LoadPicture(tre.FullName)); } else { if (File.Exists(tre.ArchiveFile)) { List <ArchiveEntry> imagesFiles = ArchiveEntry.GetImageFiles(tre.ArchiveFile); if (imagesFiles.Count > 0) { int index = imagesFiles.FindIndex(x => x.InternalArchiveFullName.Equals(tre.FullName)); //check if image still exist inside this archive. Could have been deleted for whatever reason if (index != -1) { directoryIndex = index; entries = imagesFiles.ToList <IEntry>(); return(loadPicture(entries[index])); } } } } return(false); }
/// <summary> /// When loading from a single fullname string, this full name can be many different things which leads to different behaviors /// - An image file: list images in the folder, load the image /// - A folder: list images in the folder, load the first image in the folder /// - An image inside an archive: list images inside the folder archive, load the image /// - An archive: list images in the archive, load the first image in the archive /// </summary> /// <param name="fullname"></param> public bool LoadPicture(string fullname) { if (File.Exists(fullname)) { //check if its an archive. Here we depend on the file extension. It's not a fullproof method but it's a reasonnable assumption //and expectation that the file extension is correct string fullnameL = fullname.ToLower(); if (Config.ArchiveFilter.Any(x => fullnameL.EndsWith(x))) { try { List <ArchiveEntry> imagesFiles = ArchiveEntry.GetImageFiles(fullname); if (imagesFiles.Count > 0) { directoryIndex = 0; entries = imagesFiles.ToList <IEntry>(); return(loadPicture(entries[0])); } } catch (Exception e) { throw (e); } } else { //attempt to load as a regular image string path = Path.GetDirectoryName(fullname); string[] files = Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly).Where(file => Config.ExtensionFilter.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase))).ToArray(); int index = Array.FindIndex(files, x => x.Contains(fullname)); if (index != -1) { List <IEntry> l = new List <IEntry>(); foreach (string s in files) { var e = new FileEntry(s); l.Add(e); } entries = l; directoryIndex = index; return(loadPicture(entries[directoryIndex])); } else { //the file is probably not a supported image } } } else { try { FileAttributes attr = File.GetAttributes(fullname); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { var files = Directory.EnumerateFiles(fullname, "*.*", SearchOption.TopDirectoryOnly).Where(file => Config.ExtensionFilter.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase))).ToArray(); //workingData.directoryIndex = Array.FindIndex(files, x => x.Contains(fullname)); if (files.Length > 0) { List <IEntry> l = new List <IEntry>(); foreach (string s in files) { var e = new FileEntry(s); l.Add(e); } directoryIndex = 0; entries = l; return(loadPicture(entries[0])); } else { //no image files where founds. return(false); } } } catch (FileNotFoundException notfounde) { //it's not a file, but its not a directory either? it could be a file inside an archive //TODO: support direct files inside archives throw (notfounde); } catch (DirectoryNotFoundException notfounde) { notfounde.Data.Add("fullname", fullname); throw (notfounde); } } return(false); }