Skip to content

Commit a12caa5

Browse files
committed
Remove carrierwave dependency from Spotlight, relying on ActiveStorage
1 parent 9c13409 commit a12caa5

16 files changed

+66
-139
lines changed

app/models/spotlight/attachment.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ module Spotlight
44
##
55
# Sir-trevor image upload attachments
66
class Attachment < ActiveRecord::Base
7+
# Open to alternatives on how to do this, but this works
8+
include Rails.application.routes.url_helpers
9+
710
belongs_to :exhibit
8-
mount_uploader :file, Spotlight::AttachmentUploader
11+
has_one_attached :file
912

10-
def as_json(options = nil)
11-
file.as_json(options).merge(name: name, uid: uid, attachment: to_global_id)
13+
def as_json(_options = nil)
14+
# as_json has problems with single has_one_attached content
15+
# https://github.com/rails/rails/issues/33036
16+
{
17+
name: name, uid: uid, attachment: to_global_id, url: rails_blob_path(file, only_path: true)
18+
}
1219
end
1320
end
1421
end

app/models/spotlight/bulk_update.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Spotlight
44
class BulkUpdate < ActiveRecord::Base
5-
mount_uploader :file, Spotlight::BulkUpdatesUploader
5+
has_one_attached :file
66
belongs_to :exhibit
77
end
88
end

app/models/spotlight/featured_image.rb

+3-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ module Spotlight
44
##
55
# Featured images for browse categories, feature pages, and exhibits
66
class FeaturedImage < ActiveRecord::Base
7-
mount_uploader :image, Spotlight::FeaturedImageUploader
7+
has_one_attached :image
88

99
before_validation do
1010
next unless upload_id.present? && source == 'remote'
1111

1212
# copy the image from the temp upload
1313
temp_image = Spotlight::TemporaryImage.find(upload_id)
14-
self.image = CarrierWave::SanitizedFile.new tempfile: StringIO.new(temp_image.image.read),
15-
filename: temp_image.image.filename || temp_image.image.identifier,
16-
content_type: temp_image.image.content_type
14+
image.attach(temp_image.image.blob)
1715

1816
# Unset the incoming iiif_tilesource, which points at the temp image
1917
self.iiif_tilesource = nil
@@ -24,13 +22,6 @@ class FeaturedImage < ActiveRecord::Base
2422
Spotlight::TemporaryImage.find(upload_id).delete if upload_id.present?
2523
end
2624

27-
after_save do
28-
if image.present?
29-
image.cache! unless image.cached?
30-
image.store!
31-
end
32-
end
33-
3425
attr_accessor :upload_id
3526

3627
def iiif_url
@@ -60,7 +51,7 @@ def document
6051
end
6152

6253
def file_present?
63-
image.file.present?
54+
image.blob.present?
6455
end
6556

6657
def iiif_tilesource

app/services/spotlight/carrierwave_file_resolver.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def initialize
99
end
1010

1111
def pattern(id)
12-
uploaded_file = Spotlight::FeaturedImage.find(id).image.file
12+
uploaded_file = Spotlight::FeaturedImage.find(id).image.blob
1313
raise Riiif::ImageNotFoundError, "unable to find file for #{id}" if uploaded_file.nil?
1414

15-
uploaded_file.file
15+
ActiveStorage::Blob.service.path_for(uploaded_file.key)
1616
end
1717
end
1818
end

app/services/spotlight/exhibit_import_export_service.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ def from_hash!(hash)
186186

187187
# dedupe by something??
188188
ar = exhibit.attachments.build(attr)
189-
ar.file = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
190-
filename: file[:filename],
191-
content_type: file[:content_type]
189+
ar.file.attach io: StringIO.new(Base64.decode64(file[:content])),
190+
filename: file[:filename],
191+
content_type: file[:content_type]
192192
end
193193

194194
hash[:languages].each do |attr|
@@ -212,9 +212,9 @@ def deserialize_featured_image(obj, method, data)
212212
image = obj.public_send("build_#{method}")
213213
image.update(data)
214214
if file
215-
image.image = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
216-
filename: file[:filename],
217-
content_type: file[:content_type]
215+
image.image.attach io: StringIO.new(Base64.decode64(file[:content])),
216+
filename: file[:filename],
217+
content_type: file[:content_type]
218218
# Unset the iiif_tilesource field as the new image should be different, because
219219
# the source has been reloaded
220220
image.iiif_tilesource = nil
@@ -301,11 +301,11 @@ def attach_featured_images(json)
301301

302302
def serialize_featured_image(id)
303303
image = Spotlight::FeaturedImage.find(id)
304-
file = image.image.file
304+
file = image.image
305305
if file
306306
img = {
307307
image: {
308-
filename: file.filename, content_type: file.content_type, content: Base64.encode64(file.read)
308+
filename: file.filename, content_type: file.content_type, content: Base64.encode64(file.download)
309309
}
310310
}
311311
end

app/uploaders/spotlight/attachment_uploader.rb

-15
This file was deleted.

app/uploaders/spotlight/bulk_updates_uploader.rb

-7
This file was deleted.

app/uploaders/spotlight/featured_image_uploader.rb

-17
This file was deleted.

blacklight-spotlight.gemspec

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ these collections.)
2828
s.add_dependency 'bootstrap_form', '~> 4.1'
2929
s.add_dependency 'breadcrumbs_on_rails', '>= 3.0', '< 5'
3030
s.add_dependency 'cancancan'
31-
s.add_dependency 'carrierwave', '~> 2.2'
3231
s.add_dependency 'clipboard-rails', '~> 1.5'
3332
s.add_dependency 'devise', '~> 4.1'
3433
s.add_dependency 'devise_invitable'

lib/generators/spotlight/install_generator.rb

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def add_js
2929
run 'bundle exec rails webpacker:install'
3030
end
3131

32+
def setup_activestorage
33+
run 'bundle exec rails active_storage:install'
34+
run 'bundle exec rails db:migrate'
35+
end
36+
3237
def inject_spotlight_routes
3338
route "mount Spotlight::Engine, at: 'spotlight'"
3439
gsub_file 'config/routes.rb', /^\s*root.*/ do |match|

lib/spotlight/engine.rb

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Engine < ::Rails::Engine
3636
end
3737
end
3838

39-
require 'carrierwave'
4039
require 'underscore-rails'
4140
require 'github/markup'
4241
require 'sir_trevor_rails'

lib/tasks/spotlight_tasks.rake

+36
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,42 @@ namespace :spotlight do
7777
Migration::PageLanguage.run
7878
end
7979

80+
desc 'Migrate CarrierWave content to ActiveStorage'
81+
task migrate_carrier_wave: :environment do
82+
puts 'Mirating Spotlight::Attachment'
83+
Spotlight::Attachment.find_each do |attachment|
84+
next if attachment.file.blob
85+
86+
attachment.file.attach(
87+
io: File.open(Rails.root.join("public/uploads/spotlight/attachment/file/#{attachment.attributes['file']}")),
88+
filename: attachment.attributes['file'],
89+
content_type: Mime::Type.lookup_by_extension(File.extname(attachment.attributes['file'])[1..])
90+
)
91+
end
92+
93+
puts 'Mirating Spotlight::FeaturedImage'
94+
Spotlight::FeaturedImage.find_each do |featured_image|
95+
next if featured_image.image.blob
96+
97+
featured_image.image.attach(
98+
io: File.open(Rails.root.join("public/uploads/spotlight/featured_image/image/#{featured_image.attributes['image']}")),
99+
filename: featured_image.attributes['image'],
100+
content_type: Mime::Type.lookup_by_extension(File.extname(featured_image.attributes['image'])[1..])
101+
)
102+
end
103+
104+
puts 'Mirating Spotlight::BulkUpdate'
105+
Spotlight::BulkUpdate.find_each do |bulk_update|
106+
next if bulk_update.file.blob
107+
108+
bulk_update.file.attach(
109+
io: File.open(Rails.root.join("public/uploads/#{bulk_update.attributes['file']}")),
110+
filename: bulk_update.attributes['file'],
111+
content_type: Mime::Type.lookup_by_extension(File.extname(bulk_update.attributes['file'])[1..])
112+
)
113+
end
114+
end
115+
80116
def prompt_to_create_user
81117
Spotlight::Engine.user_class.find_or_create_by!(email: prompt_for_email) do |u|
82118
puts 'User not found. Enter a password to create the user.'

spec/test_app_templates/carrierwave.rb

-8
This file was deleted.

spec/test_app_templates/lib/generators/test_app_generator.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def run_blacklight_generator
2525

2626
def run_spotlight_migrations
2727
rake 'spotlight:install:migrations'
28+
rake 'active_storage:install'
2829
rake 'db:migrate'
2930
end
3031

@@ -40,10 +41,6 @@ def add_rake_tasks_to_app
4041
rakefile 'spotlight_test.rake', File.read(find_in_source_paths('spotlight_test.rake'))
4142
end
4243

43-
def disable_carrierwave_processing
44-
copy_file 'carrierwave.rb', 'config/initializers/carrierwave.rb'
45-
end
46-
4744
def add_theme_assets
4845
copy_file 'fixture.png', 'app/assets/images/spotlight/themes/default_preview.png'
4946
copy_file 'fixture.png', 'app/assets/images/spotlight/themes/modern_preview.png'

spec/uploaders/spotlight/attachment_uploader_spec.rb

-27
This file was deleted.

spec/uploaders/spotlight/featured_image_uploader_spec.rb

-33
This file was deleted.

0 commit comments

Comments
 (0)