show saved basil images on content#show gallery tab

This commit is contained in:
Andrew Brown 2023-03-12 17:33:30 -07:00
parent 2d88b62adf
commit 9a36b581e1
6 changed files with 44 additions and 39 deletions

View File

@ -49,8 +49,8 @@ class BasilController < ApplicationController
# Finally, cache some state we can reference in the view
@commissions = BasilCommission.where(entity_type: @content.page_type, entity_id: @content.id)
.order('id DESC')
.limit(20)
.includes(:basil_feedbacks)
.limit(10)
.includes(:basil_feedbacks, :image_blob)
@in_progress_commissions = @commissions.select { |c| c.completed_at.nil? }
@can_request_another = @in_progress_commissions.count < 3
end
@ -288,33 +288,9 @@ class BasilController < ApplicationController
blob.service_name = :amazon_basil
blob.save!
# blob.update_attribute(:key, key)
# blob.update_attribute(:service_name, :amazon_basil)
commission.update(image: blob.signed_id)
commission.cache_after_complete!
# TODO: we should attach the S3 object to the commission.image attachment
# but I dunno how to do that yet. See broken attempts below.
# s3 = Aws::S3::Resource.new(region: "us-east-1")
# obj = s3.bucket("basil-characters").object("job-#{params[:jobid]}.png")
# blob_params = {
# filename: File.basename(obj.key),
# content_type: obj.content_type,
# byte_size: obj.size,
# checksum: obj.etag.gsub('"',"")
# }
# raise "wow"
# blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_params)
# # By default, the blob's key (S3 key, in this case) a secure (random) token
# # However, since the file is already on S3, we need to change the
# # key to match our file on S3
# blob.update_attribute :key, obj.key
# raise params.inspect
render json: { success: true }
end
@ -345,15 +321,15 @@ class BasilController < ApplicationController
id: params[:id],
user: current_user
)
@entity = @commission.entity
raise params.inspect
@commission.update(saved_at: DateTime.current)
end
def delete
raise params.inspect
@commission = BasilCommission.find_by(
id: params[:id],
user: current_user
)
@commission.destroy!
end
private

View File

@ -78,6 +78,8 @@ class ContentController < ApplicationController
return if ENV.key?('CONTENT_BLACKLIST') && ENV['CONTENT_BLACKLIST'].split(',').include?(@content.user.try(:email))
@serialized_content = ContentSerializer.new(@content)
@basil_images = BasilCommission.where(entity: @content)
.where.not(saved_at: nil)
if (current_user || User.new).can_read?(@content)
respond_to do |format|

View File

@ -121,7 +121,7 @@ function commission_basil(style) {
<div class="col s12 m6" style="margin-top: 1rem">
<% if @commissions.any? %>
<div class="card-panel">
<strong>Like an image and want to keep it?</strong> Just click <strong>Save</strong> and it'll save to your <%= @content.name %> page like a normal upload.
<strong>Like an image and want to keep it?</strong> Just click <strong>Save</strong> and it'll save to your <%= @content.name %> page next to any normal uploads.
</div>
<% end %>
@ -135,8 +135,8 @@ function commission_basil(style) {
%>
<div class="card horizontal">
<div class="card-image">
<%= link_to obj.presigned_url(:get) do %>
<%= image_tag obj.presigned_url(:get) %>
<%= link_to commission.image do %>
<%= image_tag commission.image %>
<% end %>
</div>
<div class="card-stacked">
@ -185,8 +185,12 @@ function commission_basil(style) {
</div>
</div>
<div class="card-action">
<%= link_to "Save", basil_save_path(commission), class: 'purple-text' %>
<%= link_to "Delete", basil_delete_path(commission), class: 'red-text right' %>
<% if commission.saved_at? %>
<%= link_to 'Saved', commission.entity, class: 'blue-text' %>
<% else %>
<%= link_to "Save", '#', class: 'purple-text js-save-commission', data: { endpoint: basil_save_path(commission) } %>
<% end %>
<%= link_to "Delete", basil_delete_path(commission), class: 'red-text right right-align', style: 'margin-right: 0' %>
</div>
</div>
</div>
@ -202,3 +206,16 @@ function commission_basil(style) {
<% end %>
</div>
</div>
<% content_for :javascript do %>
$(document).ready(function() {
$('.js-save-commission').click(function() {
$(this).text('Saved!');
var save_endpoint = $(this).data('endpoint');
$.post(save_endpoint, function(data) {
console.log(data);
});
});
});
<% end %>

View File

@ -10,4 +10,14 @@
<% end %>
<% end %>
</div>
<% if @basil_images.any? %>
<div class="center">
<% @basil_images.each do |commission| %>
<%= link_to commission.image, class: 'z-depth-1 hoverable', target: '_new' do %>
<%= image_tag commission.image, style: 'max-width: 100%' %>
<% end %>
<% end %>
</div>
<% end %>
</div>

View File

@ -15,7 +15,7 @@ Rails.application.routes.draw do
# API endpoints
post '/complete/:jobid', to: 'basil#complete_commission'
post '/feedback/:jobid', to: 'basil#feedback', as: :basil_feedback
get '/:id/save', to: 'basil#save', as: :basil_save
post '/:id/save', to: 'basil#save', as: :basil_save
delete '/:id/delete', to: 'basil#delete', as: :basil_delete
# Standard generation flow for users

View File

@ -21,7 +21,7 @@ amazon_basil:
region: <%= ENV.fetch('AWS_REGION', 'us-east-1') %>
bucket: <%= ENV.fetch('S3_BASIL_BUCKET_NAME', 'basil-commissions') %>
# Remember not to checkin your GCS keyfile to a repository
# Remember not to check in your GCS keyfile to a repository
# google:
# service: GCS
# project: your_project