require 'shrine/storage/s3'
s3_options = {
bucket: 'bucket-name',
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: 'us-west-1',
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(public: true, prefix: 'cache', **s3_options),
store: Shrine::Storage::S3.new(public: true, prefix: 'store', **s3_options)
}
Shrine.plugin :activerecord
Shrine.plugin :restore_cached_data # refresh metadata when attaching the cached file
Shrine.plugin :determine_mime_type
Shrine.plugin :presign_endpoint, presign_options: -> (request) do
# Uppy will send these two query parameters
filename = request.params['filename']
type = request.params['type']
{
content_disposition: ContentDisposition.inline(filename),
content_type: type,
content_length_range: 0..(10*1024*1024)
}
end
class ImageUploader < Shrine
plugin :refresh_metadata
end
class Image < ApplicationRecord
include ImageUploader::Attachment.new(:image)
belongs_to :imageable, polymorphic: true
...
end const uppy = Uppy({
debug: true,
restrictions: {
maxFileSize: 3000000,
maxNumberOfFiles: 5,
allowedFileTypes: ['image/*']
},
})
.use(Dashboard, {
inline: false,
trigger: '#open_uploader',
showProgressDetails: true,
note: 'Images only, up to 5 files, up to 3 MB',
metaFields: [
{ id: 'name', name: 'Name', placeholder: 'file name' },
{ id: 'caption', name: 'Caption', placeholder: 'describe what the image is about' }
],
})
.use(AwsS3, {
companionUrl: '/', // will call `GET /s3/params` on our app
})
.on('upload-success', (file, response) => {
const uploadedFileData = JSON.stringify({
id: file.meta.key.match(/cache\/(.+)/)[1], // remove the Shrine storage prefix
storage: 'cache',
metadata: {
size: file.size,
filename: file.name,
mime_type: file.type,
}
})
console.log(uploadedFileData)
})image = Image.new(imageable: Portfolio.find(1), filename: "file.jpg", mimetype: "image/jpeg", size: 78653)
image.save
json = {"id":"a_long_string.jpg","storage":"cache","metadata":{"size":78653,"filename":"file.jpg","mime_type":"image/jpeg"}}.to_json # this is output from the js console
image.image = json
# This command returns error: "Shrine::Error ({} isn't valid uploaded file data)"image.update(image_data: json)
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/0abc057a-0809-4ee1-9a1f-c733a297aa79%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
require "shrine"require "shrine/storage/file_system"require "active_record"ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:",)Shrine.storages = {cache: Shrine::Storage::FileSystem.new(Dir.tmpdir),store: Shrine::Storage::FileSystem.new(Dir.tmpdir),
}Shrine.plugin :activerecordShrine.plugin :restore_cached_data # refresh metadata when attaching the cached fileShrine.plugin :determine_mime_type
ActiveRecord::Base.connection.create_table :images do |t|t.text :image_dataend
class ImageUploader < Shrineplugin :refresh_metadataend
class Image < ActiveRecord::Baseinclude ImageUploader::Attachment.new(:image)enduploaded_file = Shrine.upload(StringIO.new, :cache)image = Image.newimage.savejson = {"id":uploaded_file.id,"storage":"cache","metadata":{"size":78653,"filename":"file.jpg","mime_type":"image/jpeg"}}.to_jsonimage.image = jsonimage.image # => #<ImageUploader::UploadedFile:0x00007fe24bb1a9b0 @data={"id"=>"14bdb70ea6f12a51ff28b3c0d4262fb2", "storage"=>"cache", "metadata"=>{"size"=>0, "filename"=>"file.jpg", "mime_type"=>nil}}>
--
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-...@googlegroups.com.
t.jsonb "image_data", default: "{}", null: false # Reads from the `<attachment>_data` attribute on the model instance.
# It returns nil if the value is blank.
def read
value = record.send(data_attribute)
convert_after_read(value) unless value.nil? || value.empty? || value == "{}" # <----
end
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-...@googlegroups.com.
def read
value = record.send(data_attribute)
convert_after_read(value) unless value.nil? || value.empty? || value == "{}"
end
def initialize(data)
raise Error, "#{data.inspect} isn't valid uploaded file data" unless data["id"] && data["storage"]
@data = data
@data["metadata"] ||= {}
storage # ensure storage is registered
end
image = Image.find 1
json = {"id":"yyy.jpg","storage":"cache","metadata":{"size":230886,"filename":"photo.jpg","mime_type":"image/jpeg"}}.to_json
image.image = json
image.save> image.image_data = {"id":"510221eacb2cefe9b5f662abb434xxx.jpg","storage":"cache","metadata":{"size":529790,"filename":"Icon.jpg","mime_type":"image/jpeg"}}.to_json
> image.save
=> true
> image.image
=> #<ImageUploader::UploadedFile:0x00007f9bc4b09390 @data={"id"=>"510221eacb2cefe9b5f662abb434xxx.jpg", "storage"=>"cache", "metadata"=>{"size"=>529790, "filename"=>"Icon.jpg", "mime_type"=>"image/jpeg"}}>
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/69e09fd7-adb5-42b3-84a3-eaa4040ff35f%40googlegroups.com.