private IEnumerable <ImageRec> SearchDir(string dir) { try { var files = new List <ImageRec>(); // Search for image files in this directory. string[] imageFiles = Directory.GetFiles(dir); foreach (string file in imageFiles) { if (Settings.IgnoreHiddenFiles == false || (File.GetAttributes(file) & FileAttributes.Hidden) == 0) { if (ImageFormatDesc.FileNameToImageFormat(file) != null) { files.Add(ImageRec.FromFile(file)); } } } // Search sub-folders in this directory. string[] subDirs = Directory.GetDirectories(dir); foreach (string subDir in subDirs) { if (Settings.IgnoreHiddenFiles == false || (File.GetAttributes(subDir) & FileAttributes.Hidden) == 0) { files.AddRange(SearchDir(subDir)); } } return(files); } catch (Exception ex) { Log.Write(ex, "Exception when searching directory '{0}'.", dir); return(new ImageRec[0]); } }
public bool Retrieve(Database db) { if (_image != null) { return(true); } switch (_type) { case ImageLocationType.File: try { if (!File.Exists(_location)) { Log.Warning("Image file no longer exists: {0}", _location); return(false); } lock (this) { _imageFormat = ImageFormatDesc.FileNameToImageFormat(_location); _image = ImageUtil.LoadFromFile(_location); _size = FileUtil.GetFileSize(_location); MakeThumbnail(db); } return(true); } catch (Exception ex) { Log.Write(ex, "Failed to load image from file '{0}'.", _location); lock (this) { _image = null; _imageFormat = null; _size = null; } return(false); } case ImageLocationType.Url: try { string fileName; if (ImageCache.TryGetCachedImage(db, _location, out fileName)) { try { Log.Write(LogLevel.Debug, "Loading cached image from '{0}'.", fileName); lock (this) { _image = ImageUtil.LoadFromFile(fileName); _size = FileUtil.GetFileSize(fileName); MakeThumbnail(db); } return(true); } catch (Exception ex) { Log.Write(ex, "Error when loading cached image from '{0}'.", fileName); } } Log.Write(LogLevel.Debug, "Downloading image '{0}'.", _location); var request = (HttpWebRequest)HttpWebRequest.Create(_location); var response = (HttpWebResponse)request.GetResponse(); Log.Write(LogLevel.Debug, "Content Type is '{0}'.", response.ContentType); _imageFormat = ImageFormatDesc.ContentTypeToImageFormat(response.ContentType); var stream = response.GetResponseStream(); lock (this) { _image = Image.FromStream(stream); _cachePathName = ImageCache.SaveImage(this, db); if (!string.IsNullOrEmpty(_cachePathName)) { _size = FileUtil.GetFileSize(_cachePathName); } else { _size = null; } db.ExecuteNonQuery("update img set cache_path = @cache_path where path = @path", "@cache_path", _cachePathName, "@path", _location); MakeThumbnail(db); } return(true); } catch (Exception ex) { Log.Write(ex, "Failed to load image from url '{0}'.", _location); lock (this) { _image = null; _imageFormat = null; } return(false); } } return(false); }