Attachments not being automatically promoted when using shrine with tus #714
-
Hey, I am able to upload my files to a cache storage via tus but shrine is not automatically promoting these to permanent storage Here is my setup: require "shrine/storage/s3"
require "shrine/storage/tus"
require "shrine/storage/file_system"
if Rails.env.production?
Shrine.storages[:store] = Shrine::Storage::S3.new(
bucket: Rails.application.credentials.blackblaze&.bucket,
region: Rails.application.credentials.blackblaze&.region,
access_key_id: Rails.application.credentials.blackblaze&.key_id,
secret_access_key: Rails.application.credentials.blackblaze&.key_name,
endpoint: Rails.application.credentials.blackblaze&.endpoint
)
Shrine.storages[:cache] = Shrine::Storage::S3.new(
bucket: Rails.application.credentials.shrine_cache&.bucket,
region: Rails.application.credentials.shrine_cache&.region,
access_key_id: Rails.application.credentials.shrine_cache&.access_key_id,
secret_access_key: Rails.application.credentials.shrine_cache&.secret_access_key
)
else
Shrine.storages[:store] = Shrine::Storage::FileSystem.new(Rails.root.join("tmp"), prefix: "shrine_uploads")
Shrine.storages[:cache] = Shrine::Storage::FileSystem.new(Rails.root.join("tmp"), prefix: "shrine_cache")
end
Shrine.storages[:tus] = Shrine::Storage::Tus.new(tus_storage: Tus::Server.opts[:storage])
Shrine.plugin :activerecord
Shrine.plugin :backgrounding
Shrine.plugin :refresh_metadata
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :derivatives, create_on_promote: true
Shrine.plugin :store_dimensions, analyzer: :ruby_vips
Shrine::Attacher.promote_block do
ShrinePromoteJob.perform_async(self.class.name, record.class.name, record.id, name.to_s, file_data)
end
Shrine::Attacher.destroy_block do
ShrineDestroyJob.perform_async(self.class.name, data)
end require "tus/server"
require "tus/storage/s3"
require "tus/storage/filesystem"
if Rails.env.production?
Tus::Server.opts[:storage] = Tus::Storage::S3.new(
bucket: Rails.application.credentials.blackblaze&.bucket,
region: Rails.application.credentials.blackblaze&.region,
access_key_id: Rails.application.credentials.blackblaze&.key_id,
secret_access_key: Rails.application.credentials.blackblaze&.key_name,
endpoint: Rails.application.credentials.blackblaze&.endpoint
)
Tus::Server.opts[:redirect_download] = true
else
Tus::Server.opts[:storage] = Tus::Storage::Filesystem.new(Rails.root.join("tmp/tus_uploads"))
end class TusUploader < Shrine
storages[:cache] = storages[:tus]
Attacher.derivatives do |original|
magick = ImageProcessing::Vips.source(original)
{
thumbnail: magick.resize_to_limit!(130, 130)
}
end
end class OrderLogAttachment < ApplicationRecord
belongs_to :order_log
validates :attachment, presence: true
include TusUploader::Attachment(:attachment)
end class Api::Scrubbers::Orders::OrderLogAttachmentsController < ApiController
def create
order_log = OrderLog.find_by(public_id: params[:order_log_id])
raise NotFoundError if order_log.nil?
order_log.order_log_attachments.create!(
attachment_data: order_log_attachment_params[:attachment_data].merge(
storage: "cache"
).to_json
)
render json: {
result: "success"
}, status: :created
end
private
def order_log_attachment_params
params.permit(attachment_data: [:id, metadata: [:filename, :mime_type, :size]])
end
def order
@order ||= current_scrubber.orders.find_by(public_id: params[:order_id])
end
def order_log
@order_log ||= order.order_logs.find_by(public_id: params[:order_log_id])
end
end Two questions I guess:
Please let me know if I am missing something or if things have changed and the docs weren't updated. I followed these two resources: https://github.com/shrinerb/shrine-tus-demo, https://github.com/shrinerb/shrine/wiki/Adding-Resumable-Uploads |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@janko. |
Beta Was this translation helpful? Give feedback.
You're assigning cached data directly to the column, which doesn't get picked up by Shrine. You need to use Shrine's attribute setter
attachment=
.