Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
teceler committed Jan 21, 2024
1 parent a5e19d7 commit b25e275
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 58 deletions.
6 changes: 1 addition & 5 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@
//= require jquery-ui/widgets/sortable
//= require jquery-ui/widgets/button
//= require jquery-ui/widgets/dialog
//= require jquery-fileupload/basic
//= require jquery-fileupload/vendor/load-image.all.min
//= require jquery-fileupload/vendor/canvas-to-blob
//= require jquery-fileupload/jquery.fileupload-process
//= require jquery-fileupload/jquery.fileupload-image
//= require bootstrap.min
//= require tinymce
//= require select2
//= require global
//= require activestorage
7 changes: 3 additions & 4 deletions app/controllers/galleries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class GalleriesController < UploadingController
before_action :find_user, only: [:index]
before_action :require_own_gallery, only: [:add, :icon]
before_action :setup_new_icons, only: [:add, :icon]
before_action :set_s3_url, only: [:edit, :add, :icon]
before_action :editor_setup, only: [:new, :edit]

def index
Expand Down Expand Up @@ -176,7 +175,7 @@ def add_new_icons
render :add and return
end

icons = @icons.map { |hash| Icon.new(icon_params(hash.except('filename', 'file')).merge(user: current_user)) }
icons = @icons.map { |hash| Icon.new(icon_params(hash.except('filename')).merge(user: current_user)) }

if icons.any? { |i| !i.valid? }
flash.now[:error] = {
Expand Down Expand Up @@ -254,13 +253,13 @@ def permitted_params
galleries_icons_attributes: [
:id,
:_destroy,
icon_attributes: [:url, :keyword, :credit, :id, :_destroy, :s3_key],
icon_attributes: [:url, :keyword, :credit, :id, :_destroy, :s3_key, :image],
],
icon_ids: [],
)
end

def icon_params(paramset)
paramset.permit(:url, :keyword, :credit, :s3_key)
paramset.permit(:url, :keyword, :credit, :s3_key, :image)
end
end
26 changes: 0 additions & 26 deletions app/controllers/uploading_controller.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
# frozen_string_literal: true
class UploadingController < ApplicationController
protected

def set_s3_url
return if params[:type] == "existing"

presign_conf = {}
if !Rails.env.production? && ENV.key?('MINIO_ENDPOINT') && ENV.key?('MINIO_ENDPOINT_EXTERNAL')
# for minio and Docker compatibility, replace the guest-compatible "minio" host with the host-compatible "localhost" path
standard_endpoint = ENV.fetch('MINIO_ENDPOINT', nil)
replacement_endpoint = ENV.fetch('MINIO_ENDPOINT_EXTERNAL', nil)
bucket_url = S3_BUCKET.url
unless bucket_url.include?(standard_endpoint)
raise RuntimeError.new("couldn't find minio endpoint in direct post URL: #{standard_endpoint} in #{bucket_url}")
end
presign_conf[:url] = bucket_url.sub(standard_endpoint, replacement_endpoint)
end

@s3_direct_post = S3_BUCKET.presigned_post(
key: "users/#{current_user.id}/icons/${filename}",
success_action_status: '201',
acl: 'public-read',
content_type_starts_with: 'image/',
cache_control: 'public, max-age=31536000',
**presign_conf,
)
end
end
18 changes: 7 additions & 11 deletions app/models/icon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@ class Icon < ApplicationRecord
has_one_attached :image
delegate_missing_to :image

validates :keyword, presence: true
validates :url,
presence: true,
length: { maximum: 255 }
validates :credit, length: { maximum: 255 }
validate :url_is_url
validate :uploaded_url_yours
nilify_blanks

before_validation :use_icon_host
before_save :use_https
before_update :delete_from_s3
after_update :update_flat_posts
after_destroy :clear_icon_ids, :delete_from_s3
Expand Down Expand Up @@ -57,9 +49,13 @@ def self.times_used(icons, user)
def url
Rails.cache.fetch(Icon.cache_string_for(self.id), expires_in: 1.month) do
if image.attached?
uri = URI(CGI.unescape(image.url))
uri.host = URI(ENV.fetch('ICON_HOST')).host
uri.to_s
if ENV.fetch('ICON_HOST', nil).present?
uri = URI(CGI.unescape(image.url))
uri.host = URI(ENV.fetch('ICON_HOST')).host
uri.to_s
else
Rails.application.routes.url_helpers.rails_storage_proxy_path(image, host: ENV.fetch('DOMAIN_NAME', 'localhost:3000'))
end
else
self[:url]
end
Expand Down
21 changes: 9 additions & 12 deletions app/views/galleries/_add_new.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
= form_tag icon_gallery_path(id: @gallery&.id || 0),
method: :post,
class: 'icon-upload',
data: { 'form-data' => @s3_direct_post.fields, url: @s3_direct_post.url, host: URI.parse(@s3_direct_post.url).host, limit: 150 } do
class: 'icon-upload' do
.content-header
%span.vmid Add New Icons to Gallery: #{@gallery.try(:name) || 'Galleryless'}
- if @gallery
Expand All @@ -13,7 +12,7 @@
%tr
%th.subber.width-150.centered Upload Files
%td.odd
= file_field_tag "icons[][file]", id: "icon_files", class: 'icon_files', multiple: true
= file_field_tag "icons[][image]", id: "icon_files", class: 'icon_files', multiple: true, direct_upload: true
%span.progress-box{style: 'margin-left: 5px;'}
%table.form-table#icon-table
%thead
Expand All @@ -27,23 +26,21 @@
- len = @icons.empty? ? 1 : @icons.size
- len.times do |i|
- klass = cycle('even', 'odd')
- s3_key = @icons[i].try(:[], :s3_key)
- url = @icons[i].try(:[], :url)
- keyword = @icons[i].try(:[], :keyword)
- purl, pkey = url, keyword if s3_key.present?
- uploaded = true
- purl, pkey = url, keyword if uploaded
%tr.icon-row{data: { index: i }}
- empty_gif = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
%td.width-15{class: klass, style: 'padding: 0px;'}= icon_mem_tag purl || empty_gif, pkey || '', class: 'vmid preview-icon'
%td.padding-5{class: klass}
%span.conf{class: ('hidden' unless s3_key.present?)}
%span.conf{class: ('hidden' unless uploaded)}
= image_tag "icons/accept.png", class: 'vmid', alt: ''
Uploaded
%span.filename= @icons[i].try(:[], :filename)
= text_field_tag "icons[][url]", url, placeholder: "URL", class: ('hidden' if s3_key.present?), id: "icons_#{i}_url"
= hidden_field_tag "icons[][s3_key]", s3_key, id: "icons_#{i}_s3_key"
= file_field_tag "icons[][image]", id: "icons_#{i}_files", class: 'icon_files', direct_upload: true
= text_field_tag "icons[][url]", url, placeholder: "URL", class: ('hidden' if uploaded), id: "icons_#{i}_url"
= hidden_field_tag "icons[][filename]", @icons[i].try(:[], :filename), id: "icons_#{i}_filename"
%td.padding-5{class: klass}= text_field_tag "icons[][keyword]", keyword, placeholder: "Keyword", id: "icons_#{i}_keyword"
%td.padding-5{class: klass}= text_field_tag "icons[][credit]", @icons[i].try(:[], :credit), placeholder: "Credit", id: "icons_#{i}_credit"
%td.padding-5{class: klass}= text_field_tag "icons[][metadata][keyword]", keyword, placeholder: "Keyword", id: "icons_#{i}_keyword"
%td.padding-5{class: klass}= text_field_tag "icons[][metadata][credit]", @icons[i].try(:[], :credit), placeholder: "Credit", id: "icons_#{i}_credit"
%td.right-align.icon-buttons{class: klass}
%a{href: '#'}= image_tag "icons/add.png", class: "icon-row-add", alt: 'Add Row'
%a{href: '#'}= image_tag "icons/minus.png", class: "icon-row-rem", alt: 'Delete Row'
Expand Down

0 comments on commit b25e275

Please sign in to comment.