From 2ea6e772df4ed99b5b32c7865aea2ce5b626d23f Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Tue, 5 Feb 2019 18:03:11 +0530 Subject: [PATCH] Don't perform API request for .exist? of synced files --- lib/paperclip/storage/cached.rb | 6 ++++-- lib/paperclip/storage/delayeds3.rb | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/paperclip/storage/cached.rb b/lib/paperclip/storage/cached.rb index c7f742a..aa2a6f9 100644 --- a/lib/paperclip/storage/cached.rb +++ b/lib/paperclip/storage/cached.rb @@ -101,9 +101,11 @@ module Paperclip raise '#path is not available for this type of storage, use #to_file instead' end - # By default checks main store if synced and cache otherwise. + # Checks if attached file exists. When store_id is not given + # it uses fast check and does not perform API request for synced files def exists?(style = default_style, store_id = nil) - store_id ||= synced_to?(self.class.main_store_id) ? self.class.main_store_id : :cache + return true if !store_id && synced_to?(self.class.main_store_id) + store_id ||= :cache !self.class.directory_for(store_id).files.head(key(style)).nil? end diff --git a/lib/paperclip/storage/delayeds3.rb b/lib/paperclip/storage/delayeds3.rb index a5e5b2c..2522a42 100644 --- a/lib/paperclip/storage/delayeds3.rb +++ b/lib/paperclip/storage/delayeds3.rb @@ -113,9 +113,11 @@ module Paperclip create_tempfile(response.body) if response.is_a?(Net::HTTPOK) end - # By default checks main store if synced and cache otherwise. + # Checks if attached file exists. When store_id is not given + # it uses fast check and does not perform API request for synced files def exists?(style = default_style, store_id = nil) - store_id ||= instance_read(:synced_to_s3) ? :s3 : :cache + return true if !store_id && instance_read(:synced_to_s3) + store_id ||= :cache case store_id when :cache File.exist?(filesystem_path(style))