Skip to content

Commit

Permalink
Replace image/attachment files in media library (SpinaCMS#1015)
Browse files Browse the repository at this point in the history
* Replace image/attachment files in media library

* Replace image signed blob ID using a bg job

* Typo

* Replace attachment signed id as well

* Refactor update method

* Update Spina::Account as well

* Simplify accounts method

* Move image refactor
  • Loading branch information
Bramjetten authored Apr 6, 2022
1 parent 0be9325 commit 304ddc8
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.7', '3.0']
ruby-version: ['2.7', '3.1']

services:
postgres:
Expand Down
12 changes: 10 additions & 2 deletions app/controllers/spina/admin/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ def create

def update
@attachment = Attachment.find(params[:id])
old_signed_id = @attachment.file&.blob&.signed_id
@attachment.update(attachment_params) if params[:attachment].present?
if params[:filename].present?
extension = @attachment.file.filename.extension
filename = "#{params[:filename]}.#{extension}"
@attachment.file.blob.update(filename: filename)
end

redirect_to [:admin, @attachment]

# Replace all occurrences of the old signed blob ID
# with the new ID in a background job
if @attachment.reload.file&.blob&.signed_id != old_signed_id
Spina::ReplaceSignedIdJob.perform_later(old_signed_id, @attachment.file&.blob&.signed_id)
end

render @attachment
end

def destroy
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/spina/admin/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,23 @@ def create

def update
@image = Image.find(params[:id])
old_signed_id = @image.file&.blob&.signed_id
@image.update(image_params) if params[:image].present?
if params[:filename].present?
extension = @image.file.filename.extension
filename = "#{params[:filename]}.#{extension}"
@image.file.blob.update(filename: filename)
end

if @image.saved_change_to_media_folder_id?
render :update
else
# Replace all occurrences of the old signed blob ID
# with the new ID in a background job
if @image.reload.file&.blob&.signed_id != old_signed_id
Spina::ReplaceSignedIdJob.perform_later(old_signed_id, @image.file&.blob&.signed_id)
end

@media_folders = MediaFolder.order(:name)
render @image
end
Expand Down Expand Up @@ -82,7 +90,7 @@ def set_media_folder
end

def image_params
params.require(:image).permit(:media_folder_id)
params.require(:image).permit(:media_folder_id, :file)
end

end
Expand Down
24 changes: 24 additions & 0 deletions app/jobs/spina/replace_signed_id_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Spina
class ReplaceSignedIdJob < ApplicationJob
queue_as { Spina.config.queues[:page_updates] }

def perform(old_signed_id, new_signed_id)
return if old_signed_id.blank? || new_signed_id.blank?

pages = get_pages(old_signed_id)
accounts = Spina::Account.all

[pages, accounts].each do |records|
records.update_all("json_attributes = REGEXP_REPLACE(json_attributes::text, '#{old_signed_id}', '#{new_signed_id}', 'g')::jsonb")
end
end

private

def get_pages(signed_id)
return Spina::Page.none unless signed_id.present?
Spina::Page.where("json_attributes::text LIKE ?", "%#{signed_id}%")
end

end
end
9 changes: 9 additions & 0 deletions app/views/spina/admin/attachments/_attachment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
<span class="truncate"><%= attachment.file.filename %></span>

<div class="absolute h-full opacity-0 group-hover:opacity-100 flex items-center right-0 pr-3 top-0">
<%= form_with model: attachment, url: spina.admin_attachment_path(attachment), data: {turbo_frame: dom_id(attachment), controller: "form loading-button", loading_message: t('spina.media_library.uploading'), action: "turbo:submit-end->loading-button#doneLoading"} do |f| %>
<%= f.file_field :file, id: "attachment_#{attachment.id}_file_field", class: 'hidden', data: {action: "loading-button#loading form#requestSubmit"} %>

<button type="button" class="btn btn-default h-7 px-2 mr-2 text-xs" data-controller="delegate-click" data-action="delegate-click#click" data-loading-button-target="button" data-delegate-click-target="#attachment_<%= attachment.id %>_file_field">
<%= heroicon("upload", style: :solid, class: "w-4 h-4 mr-1 text-gray-600") %>
<%=t 'spina.ui.replace' %>
</button>
<% end %>

<%= link_to spina.edit_admin_attachment_path(attachment), class: "btn btn-default h-7 px-2 mr-2 text-xs" do %>
<%= heroicon('pencil', style: :solid, class: 'w-4 h-4 mr-1 text-gray-600') %>
<%=t 'spina.ui.rename' %>
Expand Down
10 changes: 10 additions & 0 deletions app/views/spina/admin/images/_image.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
<span class="truncate"><%= image.file.filename %></span>

<div class="absolute h-full opacity-0 group-hover:opacity-100 flex items-center right-0 pr-3 top-0">

<%= form_with model: image, url: spina.admin_image_path(image), data: {turbo_frame: dom_id(image), controller: "form loading-button", loading_message: t('spina.media_library.uploading'), action: "turbo:submit-end->loading-button#doneLoading"} do |f| %>
<%= f.file_field :file, accept: "image/*", id: "image_#{image.id}_file_field", class: 'hidden', data: {action: "loading-button#loading form#requestSubmit"} %>

<button type="button" class="btn btn-default h-7 px-2 mr-2 text-xs" data-controller="delegate-click" data-action="delegate-click#click" data-loading-button-target="button" data-delegate-click-target="#image_<%= image.id %>_file_field">
<%= heroicon("upload", style: :solid, class: "w-4 h-4 mr-1 text-gray-600") %>
<%=t 'spina.ui.replace' %>
</button>
<% end %>

<%= link_to spina.edit_admin_image_path(image), class: "btn btn-default h-7 px-2 mr-2 text-xs" do %>
<%= heroicon('pencil', style: :solid, class: 'w-4 h-4 mr-1 text-gray-600') %>
<%=t 'spina.ui.rename' %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ de:
new_entry: Neuer Eintrag
optional: Optional
rename: Umbenennen
replace: Ersetzen
save_changes: Änderungen speichern
saving: Speichern...
user_mailer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ en:
new_entry: New entry
optional: Optional
rename: Rename
replace: Replace
save_changes: Save changes
saving: Saving...
user_mailer:
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ nl:
new_entry: Nieuw item
optional: Optioneel
rename: Hernoemen
replace: Vervangen
save_changes: Wijzigingen opslaan
saving: Opslaan...
users:
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/.ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.4
3.1.1

0 comments on commit 304ddc8

Please sign in to comment.