1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00

Migrate images to Active Storage

Also adds a rake task to do the migration.
This commit is contained in:
Daniel Barber 2018-10-19 18:09:49 -04:00
parent bc1afcf1f0
commit 5b2d1d4d17
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
15 changed files with 111 additions and 18 deletions

3
.gitignore vendored
View File

@ -18,6 +18,9 @@
# Ignore environment variables
.env
# Ignore Active Storage
/storage
# Ignore dragonfly uploads
/public/system
/public/packs

View File

@ -9,6 +9,7 @@ gem "rails", "5.2.1"
gem "delayed_job_active_record"
gem "dragonfly"
gem "http"
gem "mini_magick"
gem "omniauth-github"
gem "pg"
gem "puma"

View File

@ -121,6 +121,7 @@ GEM
mimemagic (~> 0.3.2)
method_source (0.9.1)
mimemagic (0.3.2)
mini_magick (4.9.2)
mini_mime (1.0.1)
mini_portile2 (2.3.0)
minitest (5.11.3)
@ -270,6 +271,7 @@ DEPENDENCIES
geckodriver-helper
http
launchy
mini_magick
omniauth-github
pg
pry
@ -289,4 +291,4 @@ RUBY VERSION
ruby 2.5.1p57
BUNDLED WITH
1.16.4
1.16.5

View File

@ -1,9 +1,15 @@
module ImageHelper
def image_variant(image, variant)
image.variant(variant)
end
def image_thumb(image, size)
if image.format == "jpeg"
image.thumb(size).encode("jpg", "-quality 90")
else
image.thumb(size)
end
image.variant(
combine_options: {
gravity: "center",
resize: "#{size}x#{size}^",
extent: "#{size}x#{size}",
},
)
end
end

View File

@ -1,5 +1,6 @@
class Image < ActiveRecord::Base
dragonfly_accessor :image
dragonfly_accessor :df_image
has_one_attached :image
validates :image, presence: true

View File

@ -1,17 +1,17 @@
<picture>
<source srcset="<%= image_thumb(@image.image, "2000x2000").url %>"
<source srcset="<%= url_for image_variant(@image.image, resize: "2000x2000") %>"
media="(min-width: 960px) and (-webkit-min-device-pixel-ratio: 2),
(min-width: 960px) and (min-resolution: 192dpi)">
<source srcset="<%= image_thumb(@image.image, "1000x1000").url %>"
<source srcset="<%= url_for image_variant(@image.image, resize: "1000x1000") %>"
media="(min-width: 960px)">
<source srcset="<%= image_thumb(@image.image, "1400x1400").url %>"
<source srcset="<%= url_for image_variant(@image.image, resize: "1400x1400") %>"
media="(min-width: 640px) and (-webkit-min-device-pixel-ratio: 2),
(min-width: 640px) and (min-resolution: 192dpi)">
<source srcset="<%= image_thumb(@image.image, "700x700").url %>"
<source srcset="<%= url_for image_variant(@image.image, resize: "700x700") %>"
media="(min-width: 640px)">
<source srcset="<%= image_thumb(@image.image, "960x960").url %>"
<source srcset="<%= url_for image_variant(@image.image, resize: "960x960") %>"
media="(-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi)">
<source srcset="<%= image_thumb(@image.image, "480x480").url %>">
<img src="<%= image_thumb(@image.image, "480x480").url %>" itemprop="image">
<source srcset="<%= url_for image_variant(@image.image, resize: "480x480") %>">
<img src="<%= url_for image_variant(@image.image, resize: "480x480") %>" itemprop="image">
</picture>

View File

@ -5,7 +5,7 @@
<ul class="images">
<% @images.each do |image| %>
<li>
<%= link_to image_tag(image.image.thumb('200x200#').url),
<%= link_to image_tag(image_thumb(image.image, 200)),
user_image_path(image), class: :image %><br>
<%= link_to 'Delete', user_image_path(image),
method: :delete, data: { confirm: 'Are you sure?' } %>

View File

@ -16,6 +16,9 @@ MyImages::Application.configure do
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Store files locally.
config.active_storage.service = :local
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

View File

@ -72,6 +72,9 @@ MyImages::Application.configure do
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify
# Store files locally.
config.active_storage.service = :local
# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false

View File

@ -33,4 +33,7 @@ MyImages::Application.configure do
# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
# Store files locally.
config.active_storage.service = :test
end

7
config/storage.yml Normal file
View File

@ -0,0 +1,7 @@
local:
service: Disk
root: <%= Rails.root.join("storage") %>
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

View File

@ -0,0 +1,26 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false
t.index [ :key ], unique: true
end
create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false
t.datetime :created_at, null: false
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
end
end
end

View File

@ -0,0 +1,6 @@
class RenameDragonflyColumns < ActiveRecord::Migration[5.2]
def change
rename_column :images, :image_uid, :df_image_uid
rename_column :images, :image_name, :df_image_name
end
end

View File

@ -10,11 +10,32 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_08_26_163510) do
ActiveRecord::Schema.define(version: 2018_10_19_210305) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table "delayed_jobs", force: :cascade do |t|
t.integer "priority", default: 0, null: false
t.integer "attempts", default: 0, null: false
@ -32,8 +53,8 @@ ActiveRecord::Schema.define(version: 2018_08_26_163510) do
create_table "images", force: :cascade do |t|
t.integer "user_id"
t.string "image_uid"
t.string "image_name"
t.string "df_image_uid"
t.string "df_image_name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tags", default: [], array: true

View File

@ -0,0 +1,11 @@
namespace :images do
desc "Migrate all the images to Active Storage"
task migrate: :environment do
Image.all.each do |image|
image.image.attach(
io: image.df_image.file,
filename: image.df_image.name,
)
end
end
end