FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 04:07:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This model indicates an 'attempt' to create a topic thumbnail
|
|
|
|
# for an upload. This means we don't keep trying to create optimized
|
|
|
|
# images for small/invalid original images.
|
|
|
|
#
|
|
|
|
# Foreign keys with ON DELETE CASCADE are used to ensure unneeded data
|
|
|
|
# is deleted automatically
|
|
|
|
class TopicThumbnail < ActiveRecord::Base
|
|
|
|
belongs_to :upload
|
|
|
|
belongs_to :optimized_image
|
|
|
|
|
|
|
|
def self.find_or_create_for!(original, max_width:, max_height:)
|
|
|
|
existing =
|
|
|
|
TopicThumbnail.find_by(upload: original, max_width: max_width, max_height: max_height)
|
|
|
|
return existing if existing
|
|
|
|
return nil if !SiteSetting.create_thumbnails?
|
|
|
|
|
|
|
|
target_width, target_height =
|
|
|
|
ImageSizer.resize(
|
|
|
|
original.width,
|
|
|
|
original.height,
|
|
|
|
{ max_width: max_width, max_height: max_height },
|
|
|
|
)
|
|
|
|
|
|
|
|
if target_width < original.width && target_height < original.height
|
|
|
|
optimized = OptimizedImage.create_for(original, target_width, target_height)
|
|
|
|
end
|
|
|
|
|
2020-05-26 02:10:05 -04:00
|
|
|
# may have been associated already, bulk insert will skip dupes
|
|
|
|
TopicThumbnail.insert_all(
|
|
|
|
[
|
|
|
|
upload_id: original.id,
|
|
|
|
max_width: max_width,
|
|
|
|
max_height: max_height,
|
2020-05-26 02:19:05 -04:00
|
|
|
optimized_image_id: optimized&.id,
|
2020-05-26 02:10:05 -04:00
|
|
|
],
|
|
|
|
)
|
2023-01-09 07:20:10 -05:00
|
|
|
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 04:07:50 -04:00
|
|
|
TopicThumbnail.find_by(upload: original, max_width: max_width, max_height: max_height)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ensure_consistency!
|
|
|
|
# Clean up records for broken upload links or broken optimized image links
|
|
|
|
TopicThumbnail
|
|
|
|
.joins("LEFT JOIN uploads on upload_id = uploads.id")
|
|
|
|
.joins("LEFT JOIN optimized_images on optimized_image_id = optimized_images.id")
|
|
|
|
.where(<<~SQL)
|
|
|
|
(optimized_image_id IS NOT NULL AND optimized_images IS NULL)
|
|
|
|
OR uploads IS NULL
|
|
|
|
SQL
|
|
|
|
.delete_all
|
|
|
|
|
|
|
|
# Delete records for sizes which are no longer needed
|
|
|
|
sizes =
|
|
|
|
Topic.thumbnail_sizes +
|
|
|
|
ThemeModifierHelper.new(theme_ids: Theme.pluck(:id)).topic_thumbnail_sizes
|
|
|
|
sizes_sql =
|
|
|
|
sizes.map { |s| "(max_width = #{s[0].to_i} AND max_height = #{s[1].to_i})" }.join(" OR ")
|
|
|
|
TopicThumbnail.where.not(sizes_sql).delete_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: topic_thumbnails
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# upload_id :bigint not null
|
|
|
|
# optimized_image_id :bigint
|
|
|
|
# max_width :integer not null
|
|
|
|
# max_height :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_topic_thumbnails_on_optimized_image_id (optimized_image_id)
|
|
|
|
# index_topic_thumbnails_on_upload_id (upload_id)
|
|
|
|
# unique_topic_thumbnails (upload_id,max_width,max_height) UNIQUE
|
|
|
|
#
|