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`
This commit is contained in:
David Taylor
2020-05-05 09:07:50 +01:00
committed by GitHub
parent 5cf6984a1a
commit 03818e642a
25 changed files with 563 additions and 57 deletions

View File

@@ -0,0 +1,19 @@
# frozen_string_literal: true
class AddTopicThumbnailInformation < ActiveRecord::Migration[6.0]
def change
add_reference :posts, :image_upload
add_reference :topics, :image_upload
add_column :theme_modifier_sets, :topic_thumbnail_sizes, :string, array: true
create_table :topic_thumbnails do |t|
t.references :upload, null: false
t.references :optimized_image, null: true
t.integer :max_width, null: false
t.integer :max_height, null: false
end
add_index :topic_thumbnails, [:upload_id, :max_width, :max_height], name: :unique_topic_thumbnails, unique: true
end
end

View File

@@ -0,0 +1,95 @@
# frozen_string_literal: true
class MigrateImageUrlToImageUploadId < ActiveRecord::Migration[6.0]
disable_ddl_transaction! # Avoid holding update locks on posts for the whole migration
BATCH_SIZE = 1000
def up
# Defining regex here to avoid needing to double-escape the \ characters
regex = '\/(original|optimized)\/\dX[\/\.\w]*\/([a-zA-Z0-9]+)[\.\w]*'
execute <<~SQL
CREATE TEMPORARY TABLE tmp_post_image_uploads(
post_id int primary key,
upload_id int
)
SQL
# Look for an SHA1 in the existing image_url, and match to the uploads table
execute <<~SQL
INSERT INTO tmp_post_image_uploads(post_id, upload_id)
SELECT
posts.id as post_id,
uploads.id as upload_id
FROM posts
LEFT JOIN LATERAL regexp_matches(posts.image_url, '#{regex}') matched_sha1 ON TRUE
LEFT JOIN uploads on uploads.sha1 = matched_sha1[2]
WHERE posts.image_url IS NOT NULL
AND uploads.id IS NOT NULL
ORDER BY posts.id ASC
SQL
# Update the posts table to match the temp table data
last_update_id = -1
begin
result = DB.query <<~SQL
WITH to_update AS (
SELECT post_id, upload_id
FROM tmp_post_image_uploads
JOIN posts ON posts.id = post_id
WHERE posts.id > #{last_update_id}
ORDER BY post_id ASC
LIMIT #{BATCH_SIZE}
)
UPDATE posts SET image_upload_id = to_update.upload_id
FROM to_update
WHERE to_update.post_id = posts.id
RETURNING posts.id
SQL
last_update_id = result.last&.id
end while last_update_id
# Update the topic image based on the first post image
last_update_id = -1
begin
result = DB.query <<~SQL
WITH to_update AS (
SELECT topic_id, posts.image_upload_id as upload_id
FROM topics
JOIN posts ON post_number = 1 AND posts.topic_id = topics.id
WHERE posts.image_upload_id IS NOT NULL
AND topics.id > #{last_update_id}
ORDER BY topics.id ASC
LIMIT #{BATCH_SIZE}
)
UPDATE topics SET image_upload_id = to_update.upload_id
FROM to_update
WHERE topics.id = to_update.topic_id
RETURNING topics.id
SQL
last_update_id = result.last&.id
end while last_update_id
# For posts we couldn't figure out, mark them for background rebake
last_update_id = -1
begin
updated_count = DB.query <<~SQL
WITH to_update AS (
SELECT id as post_id
FROM posts
WHERE posts.image_url IS NOT NULL
AND posts.image_upload_id IS NULL
AND posts.id > #{last_update_id}
ORDER BY posts.id ASC
LIMIT #{BATCH_SIZE}
)
UPDATE posts SET baked_version = NULL
FROM to_update
WHERE posts.id = to_update.post_id
RETURNING posts.id
SQL
last_update_id = result.last&.id
end while last_update_id
end
end