Module: Condo

Defined in:
lib/condo.rb,
lib/condo/engine.rb,
lib/condo/errors.rb,
lib/condo/version.rb,
lib/condo/configuration.rb,
lib/condo/strata/amazon_s3.rb,
lib/condo/strata/microsoft_azure.rb,
lib/condo/strata/open_stack_swift.rb,
lib/condo/strata/google_cloud_storage.rb

Defined Under Namespace

Modules: Errors, Strata Classes: Configuration, Engine

Constant Summary collapse

VERSION =
"2.0.2"

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/condo.rb', line 18

def self.included(base)
    base.class_eval do


        def new
            # Returns the provider that will be used for this file upload
            resident = current_resident

            # Ensure parameters are correct
            params.require(:file_size)
            params.require(:file_name)
            permitted = params.permit(:file_size, :file_name, :file_path)
            @upload = {
                file_size: permitted[:file_size].to_i,
                file_name: @@callbacks[:sanitize_filename].call(permitted[:file_name])
            }
            @upload[:file_path] = @@callbacks[:sanitize_filepath].call(permitted[:file_path]) if permitted[:file_path]

            valid, errors = instance_exec(@upload, &@@callbacks[:pre_validation])       # Ensure the upload request is valid before uploading
            
            if valid
                residence = current_residence

                render json: {residence: residence.name}
            elsif errors.is_a? Hash
                render json: errors, status: :not_acceptable
            else
                render nothing: true, status: :not_acceptable
            end
        end

        def create
            # Check for existing upload or create a new one
            # => mutually exclusive so can send back either the parts signature from show or a bucket creation signature and the upload_id
            resident = current_resident

            # Ensure parameters are correct
            params.require(:file_size)
            params.require(:file_name)
            params.require(:file_id)
            permitted = params.permit(:file_size, :file_name, :file_path, :file_id)
            @upload = {
                file_size: permitted[:file_size].to_i,
                file_name: @@callbacks[:sanitize_filename].call(permitted[:file_name]),
                file_id: permitted[:file_id],
                user_id: resident
            }
            @upload[:file_path] = @@callbacks[:sanitize_filepath].call(permitted[:file_path]) if permitted[:file_path]

            # Check for existing uploads
            upload = condo_backend.check_exists(@upload)

            if upload.present?
                residence = current_residence(upload)

                # Return the parts or direct upload sig
                request = nil
                if upload.resumable_id.present? && upload.resumable
                    request = residence.get_parts({
                        bucket_name: upload.bucket_name,
                        object_key: upload.object_key,
                        object_options: upload.object_options,
                        file_size: upload.file_size,
                        resumable_id: upload.resumable_id
                    })

                    request[:part_list] = upload.part_list || []
                    request[:part_data] = upload.part_data if upload.part_data
                else
                    request = residence.new_upload({
                        bucket_name: upload.bucket_name,
                        object_key: upload.object_key,
                        object_options: upload.object_options,
                        file_size: upload.file_size,
                        file_id: upload.file_id
                    })
                end
                
                render json: request.merge!({
                    upload_id: upload.id,
                    residence: residence.name
                })
            else
                # Create a new upload
                valid, errors = instance_exec(@upload, &@@callbacks[:pre_validation])               # Ensure the upload request is valid before uploading

                if valid
                    residence = current_residence

                    # Build the request
                    @upload.merge!({
                        bucket_name:    (instance_eval &@@callbacks[:bucket_name]),             # Allow the application to define a custom bucket name
                        object_key:     instance_exec(@upload, &@@callbacks[:object_key]),      # The object key should also be generated by the application
                        object_options: instance_exec(@upload, &@@callbacks[:object_options])   # Do we want to mess with any of the options?
                    })
                    request = residence.new_upload(@upload)
                    resumable = request[:type] == :chunked_upload

                    # Save a reference to this upload in the database
                    # => This should throw an error on failure
                    upload = condo_backend.add_entry(@upload.merge!({provider_name: residence.name, provider_location: residence.location, resumable: resumable}))
                    render json: request.merge!(upload_id: upload.id, residence: residence.name)
                    
                elsif errors.is_a? Hash
                    render json: errors, status: :not_acceptable
                else
                    render nothing: true, status: :not_acceptable
                end
            end
        end

        # Authorization check all of these
        def edit
            # Get the signature for parts + final commit
            upload = current_upload

            params.require(:part)
            safe_params = params.permit(:part, :file_id)

            if upload.resumable_id.present? && upload.resumable
                residence = current_residence(upload)

                request = residence.set_part({
                    bucket_name: upload.bucket_name,
                    object_key: upload.object_key,
                    object_options: upload.object_options,
                    resumable_id: upload.resumable_id,
                    # part may be called 'finish' for commit signature
                    part: safe_params[:part],
                    file_size: upload.file_size,
                    file_id: safe_params[:file_id]
                })

                render json: request.merge!(upload_id: upload.id)
            else
                render nothing: true, status: :not_acceptable
            end
        end


        def update
            # Provide the upload id after creating a resumable upload (may not be completed)
            # => We then provide the first part signature
            #
            # OR
            #
            # Complete an upload
            upload = current_upload

            safe = params.permit(:resumable_id, {part_list: []}, {part_data: [
                :md5,
                :size_bytes,
                :path,
                :part
            ]})
            part_list = safe[:part_list]
            resumable_id = safe[:resumable_id]

            if resumable_id || part_list
                if upload.resumable
                    if part_list
                        upload.part_list = part_list || []

                        # We incrementally update this as it might otherwise contain
                        # sum(1 -> 10,000) parts over the time of the upload
                        # (as is the case with the largest file amazon supports)
                        if safe[:part_data]
                            upload.part_data ||= {}
                            safe[:part_data].each do |part|
                                upload.part_data[part[:part]] = part
                            end
                        end
                    end

                    upload.resumable_id = resumable_id if resumable_id
                    upload.save!

                    if params[:part_update]
                        render nothing: true
                    else
                        @current_upload = upload

                        # Render is called from edit
                        edit
                    end
                else
                    render nothing: true, status: :not_acceptable
                end

            else # We are completeing the upload
                response = instance_exec upload, &@@callbacks[:upload_complete]
                if response
                    render nothing: true
                else
                    render nothing: true, status: :not_acceptable
                end
            end
        end

        def destroy
            # Delete the file from the cloud system - the client is not responsible for this
            response = instance_exec current_upload, &@@callbacks[:destroy_upload]
            if response
                render nothing: true
            else
                render nothing: true, status: :not_acceptable
            end
        end


        protected


        def current_residence(upload = nil)
            @current_residence ||= instance_exec(condo_config, current_resident, upload, &@@callbacks[:select_residence])
        end

        def current_upload
            return @current_upload if @current_upload

            safe_params = params.permit(:upload_id, :id)
            # current_residence.name && current_residence.location && resident.id.exists?
            @current_upload = condo_backend.check_exists({user_id: current_resident, upload_id: (safe_params[:upload_id] || safe_params[:id])}).tap do |object|
                raise Condo::Errors::NotYourPlace unless object.present?
            end
        end

        def current_resident
            # instance_exec for params
            @current_resident ||= (instance_eval &@@callbacks[:resident_id]).tap do |object|
                raise Condo::Errors::LostTheKeys unless object.present?
            end
        end

        def condo_backend
            ::Condo::Store
        end

        def condo_config
            ::Condo::Configuration
        end


        # Defines the default callbacks
        (@@callbacks ||= {}).merge! Condo::Configuration.callbacks

        def self.condo_callback(name, callback = nil, &block)
            callback ||= block
            if callback.respond_to?(:call)
                @@callbacks[name.to_sym] = callback
            else
                raise ArgumentError, 'No callback provided'
            end
        end
    end
end