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); }
private void SyncDatabaseToImageRecList(IEnumerable <ImageRec> filesOnDisk, Theme theme, Database db, CancellationToken cancel) { var table = db.SelectDataTable("select rowid, * from img where location_id = @id", "@id", _rowid); // Find files in the database that don't exist on disk var filesToRemove = new List <long>(); foreach (DataRow row in table.Rows) { if (cancel.IsCancellationRequested) { return; } var path = (string)row["path"]; var found = false; foreach (var file in filesOnDisk) { if (string.Equals(file.Location, path, StringComparison.OrdinalIgnoreCase)) { found = true; break; } } if (!found) { filesToRemove.Add((long)row["rowid"]); } } var changesMade = false; using (var tran = db.BeginTransaction()) { if (filesToRemove.Count > 0) { using (var cmd = db.CreateCommand("delete from img where rowid = @rowid")) { foreach (var rowid in filesToRemove) { if (cancel.IsCancellationRequested) { return; } cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@rowid", rowid); cmd.ExecuteNonQuery(); changesMade = true; } } } // Find files on disk that don't exist in the database var filesToAdd = new List <ImageRec>(); foreach (var file in filesOnDisk) { if (cancel.IsCancellationRequested) { return; } DataRow foundRow = null; foreach (DataRow row in table.Rows) { if (cancel.IsCancellationRequested) { return; } if (string.Equals(file.Location, (string)row["path"], StringComparison.OrdinalIgnoreCase)) { foundRow = row; break; } } if (foundRow == null) { filesToAdd.Add(file); } else { // Check if new cache path needs to be updated in the img table if (file.Type == ImageLocationType.Url && string.IsNullOrEmpty(file.LocationOnDisk)) { string cachePathName; if (ImageCache.TryGetCachedImage(db, file.Location, out cachePathName)) { db.ExecuteNonQuery("update img set cache_path = @cache_path where path = @path", "@cache_path", cachePathName, "@path", file.Location); db.ExecuteNonQuery("update history set cache_path = @cache_path where path = @path", "@cache_path", cachePathName, "@path", file.Location); changesMade = true; } } // Check if the size needs updating if (foundRow.GetLong("size", -1) == -1 && !string.IsNullOrEmpty(file.LocationOnDisk)) { file.RefreshFileSize(); var size = file.Size; if (size.HasValue) { db.ExecuteNonQuery("update img set size = @size where path = @path", "@size", size.Value, "@path", file.Location); // No need to update history since it doesn't care about the size. changesMade = true; } } } } if (filesToAdd.Count > 0) { using (var cmd = db.CreateCommand("insert into img (theme_id, location_id, type, path, pub_date, rating, thumb, size)" + " values (@theme_id, @location_id, @type, @path, @pub_date, @rating, @thumb, @size)")) { foreach (var img in filesToAdd) { if (cancel.IsCancellationRequested) { return; } #if DEBUG Log.Debug("Inserting: {0}", img.Location); #endif cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@theme_id", theme.RowId); cmd.Parameters.AddWithValue("@location_id", _rowid); cmd.Parameters.AddWithValue("@type", img.Type.ToString()); cmd.Parameters.AddWithValue("@path", img.Location); cmd.Parameters.AddWithValue("@pub_date", img.PubDate.HasValue ? (object)img.PubDate.Value : null); cmd.Parameters.AddWithValue("@rating", img.Rating); cmd.Parameters.AddWithValue("@thumb", img.Thumbnail?.Data); if (img.Size.HasValue) { cmd.Parameters.AddWithValue("@size", img.Size.Value); } else { cmd.Parameters.AddWithValue("@size", null); } cmd.ExecuteNonQuery(); changesMade = true; } } } tran.Commit(); } if (changesMade) { ContentUpdated?.Invoke(this, EventArgs.Empty); } }