2019-08-01 19:17:24 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActiveStorage
|
2019-10-03 17:00:59 +08:00
|
|
|
class RepresentationsController < CustomBaseController
|
2019-08-01 19:17:24 +08:00
|
|
|
include ActiveStorage::SetBlob
|
|
|
|
include ActiveStorage::CheckBlobPermissions
|
|
|
|
|
|
|
|
def show
|
2019-09-24 03:07:03 +08:00
|
|
|
if @blob.attachments.take.record_type == 'Asset'
|
|
|
|
return render plain: '', status: :accepted unless preview_ready?
|
|
|
|
end
|
|
|
|
|
2019-09-12 23:21:48 +08:00
|
|
|
expires_in ActiveStorage.service_urls_expire_in
|
2019-08-01 19:17:24 +08:00
|
|
|
redirect_to @blob.representation(params[:variation_key]).processed.service_url(disposition: params[:disposition])
|
|
|
|
end
|
2019-09-24 03:07:03 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def preview_ready?
|
|
|
|
processing = @blob.attachments.take.record.file_processing
|
|
|
|
return false if processing
|
|
|
|
|
|
|
|
preview_exists =
|
|
|
|
if @blob.variable?
|
|
|
|
@blob.service.exist?(@blob.representation(params['variation_key']).key)
|
|
|
|
else
|
|
|
|
@blob.preview(params['variation_key']).image.attached?
|
|
|
|
end
|
|
|
|
|
|
|
|
return true if preview_exists
|
|
|
|
|
|
|
|
unless processing
|
2019-10-10 20:17:02 +08:00
|
|
|
ActiveStorage::PreviewJob.perform_later(@blob.id)
|
2019-09-24 03:07:03 +08:00
|
|
|
@blob.attachments.take.record.update(file_processing: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
2019-08-01 19:17:24 +08:00
|
|
|
end
|
|
|
|
end
|