shrinerb / shrine-cloudinary

Cloudinary storage for Shrine
https://cloudinary.com/
MIT License
23 stars 6 forks source link

CloudinaryException: Resource not found - when use subfolders #9

Open aaongr opened 4 years ago

aaongr commented 4 years ago

Hello,

I am using latest shrine-cloudinary v1.1.1, with Shrine v3.2.2 in Rails 6.3.2 and Ruby 2.6.5...

config/initializers/shrine.rb

require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/memory"

require "cloudinary"
require "shrine/storage/cloudinary"

if ENV['CLOUDINARY_URL']
  Cloudinary.config do |config|
    config.secure = true
    config.cdn_subdomain = true
  end
else
  Rails.logger.info { puts "****** Warning: There is no 'cloudinary' configuration info" }
end

if Rails.env.test?
  Shrine.storages = {
    cache: Shrine::Storage::Memory.new, # temporary
    store: Shrine::Storage::Memory.new, # permanent
  }
else
  Shrine.storages = {
    cache: Shrine::Storage::Cloudinary.new(prefix: "cache"), # for direct uploads
    store: Shrine::Storage::Cloudinary.new,
  }
end

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
Shrine.plugin :validation
Shrine.plugin :validation_helpers
Shrine.plugin :remote_url, max_size: 20*1024*1024
Shrine.plugin :determine_mime_type
Shrine.plugin :infer_extension
Shrine.plugin :upload_options

I want to have the ability to upload from remote urls and to set the folder in every upload (multi-tenant app)

If I use:

p.attachment_attacher.assign_remote_url(img['url'], upload_options: { folder: "aaa" })

The cache gets created inside folder 'aaa' but I get the error:

CloudinaryException: Resource not found - https://res-1.cloudinary.com/itg-development/image/upload/v1/cache/e504140bb8018bd4f0163ae745659849.jpg

Probably when It moves the file from the cached folder it does not use the specified folder and this must be the reason for the exception...

I try also the following, but the problem persists:

p.attachment_attacher.assign_remote_url(img['url'], upload_options: { folder: "aaa", prefix: "aaa" })

If I use:

p.attachment_attacher.assign_remote_url(img['url'], upload_options: { prefix: "aaa" })

then the file gets uploaded in the root folder in cloudinary and the cache file in 'cache' folder (this is not what I want)...

Any ideas?

With my warmest regards for all of Us... :)

aaongr commented 4 years ago

I do not know if this is an issue of this gem... It could be an issue of the caller (shrine gem)

I forked-cloned this gem

Log - this is the initialization:

*** [shrine-cloudinary] initialize - prefix: "cache", type: upload
upload_options: {}
=========================
*** [shrine-cloudinary] initialize - prefix: nil, type: upload
upload_options: {}
=========================

Log - the first upload has the correct upload_options:

*** [shrine-cloudinary] upload - io: #<File:0x00007fd572a80aa8>, id: 0527540cf7686c37db9c678e4a88bb90.jpg
shrine_metadata: {"filename"=>"dirfis-apoksiramena-aourikoularia-30g_585x585.jpg", "size"=>54491, "mime_type"=>"image/jpeg"}
upload_options: {:folder=>"aaa"}
=========================

Log - the second upload missing the correct upload_options:

*** [shrine-cloudinary] upload - io: #<AttachmentUploader::UploadedFile:0x00007fd57401b660>, id: 0ab93ba604f7b96f28f84ffbf40b4bba.jpg
shrine_metadata: {"filename"=>"dirfis-apoksiramena-aourikoularia-30g_585x585.jpg", "size"=>54491, "mime_type"=>"image/jpeg", "width"=>585, "height"=>585}
upload_options: {}
=========================
*** [shrine-cloudinary] store - options: {:public_id=>"0ab93ba604f7b96f28f84ffbf40b4bba"}
=========================