@@ -8,6 +8,8 @@ module Store
8
8
include CarrierWave ::Uploader ::Cache
9
9
10
10
included do
11
+ attr_accessor :retrieval_retry_count
12
+
11
13
prepend Module . new {
12
14
def initialize ( *)
13
15
super
@@ -93,17 +95,41 @@ def store!(new_file=nil)
93
95
end
94
96
end
95
97
98
+ # Attempt to retrieve the file from the store
99
+ # if the file is nil, but to avoid calling this
100
+ # over and over if retrieving it returns nil,
101
+ # we keep track of the number of times we've
102
+ # attempted to retrieve it and just return nil
103
+ # if we exceed the maximum number of retries
104
+ #
105
+ # === Returns
106
+ #
107
+ # [CarrierWave::SanitizedFile, nil] the stored file or nil
108
+ def file
109
+ return @file unless @identifier
110
+ retrieve_from_store! ( @identifier ) unless @file
111
+ @file
112
+ end
113
+
96
114
##
97
115
# Retrieves the file from the storage.
98
116
#
99
117
# === Parameters
100
118
#
101
119
# [identifier (String)] uniquely identifies the file to retrieve
102
120
#
103
- def retrieve_from_store! ( identifier )
104
- with_callbacks ( :retrieve_from_store , identifier ) do
105
- @file = storage . retrieve! ( identifier )
106
- @identifier = identifier
121
+ def retrieve_from_store! ( file_identifier )
122
+ self . retrieval_retry_count ||= 0
123
+ return if self . retrieval_retry_count > download_retry_count
124
+ self . retrieval_retry_count += 1
125
+ with_callbacks ( :retrieve_from_store , file_identifier ) do
126
+ # We can't retrieve the file if we have no identifier
127
+ # to retrieve it with. Identifier should be assigned
128
+ # when setting up the uploader, or when caching,
129
+ # or storing a new file
130
+ next unless file_identifier
131
+ @file = storage . retrieve! ( file_identifier )
132
+ @identifier = file_identifier
107
133
end
108
134
end
109
135
0 commit comments