Module: Lesli::Interfaces::Controllers::Files

Defined in:
app/controllers/lesli/interfaces/controllers/files.rb

Instance Method Summary collapse

Instance Method Details

#createJson

Returns Json that contains wheter the creation of the file was successful or not. If it is not successful, it returs an error message.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let data = {
    ticket_file: {
        file: FILE_CONTENT
        name: "contract_information"
    }
};
this.http.post(`127.0.0.1/help/tickets/${ticket_id}/files`, data);

Returns:

  • (Json)

    Json that contains wheter the creation of the file was successful or not. If it is not successful, it returs an error message



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
# File 'app/controllers/lesli/interfaces/controllers/files.rb', line 118

def create
    file_model = file_model() # If there is a custom file model, it must be returned in this method
    cloud_object_model = file_model.cloud_object_model

    set_cloud_object
    new_file_params = file_params.merge(
        user_creator: current_user,
        cloud_object: @cloud_object
    )

    # Verifying the extension of the file. If it's valid, the block will be executed
    decode_and_verify_file(new_file_params) do |verified_file_params|
        file = file_model.new(verified_file_params)

        if file.save
            # Setting the file name in case it's blank and updating the file in case the filename changed
            if file.name.blank?
                file.update(
                    name: params["#{cloud_object_model.name.demodulize.underscore}_file".to_sym][:attachment].original_filename
                )
            else
                file.update({})
            end

            cloud_object = file.cloud_object

            # Setting up file uploader to upload in background
            Files::AwsUploadJob.perform_later(file)

            if block_given?
                yield(cloud_object, file)
            else
                # Registering an activity in the cloud_object
                cloud_object.activities.create(
                    user_creator: current_user,
                    category: "action_create_file",
                    description: "#{file.name} - #{file.attachment_identifier}"
                )

                # Returning the 200 HTTP response
                respond_with_successful(file)
            end
        else
            respond_with_error(file.errors.full_messages.to_sentence)
        end
    end
end

#destroyJson

Returns A response that contains wheter the file was deleted or not. If it is not successful, it returns an error message.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let file_id = 22;
this.http.delete(`127.0.0.1/help/tickets/${ticket_id}/files/${file_id}`);

Returns:

  • (Json)

    A response that contains wheter the file was deleted or not. If it is not successful, it returns an error message



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'app/controllers/lesli/interfaces/controllers/files.rb', line 270

def destroy
    set_file
    return respond_with_not_found unless @file
    return respond_with_unauthorized unless @file.is_editable_by?(current_user)

    if @file.destroy
        if block_given?
            yield(@cloud_object, @file)
        else
            # Registering an activity in the cloud_object
            @file.cloud_object.activities.create(
                user_creator: current_user,
                category: "action_destroy_file",
                description: @file.name
            )

            respond_with_successful
        end
    else
        respond_with_error(@file.errors.full_messages.to_sentence)
    end
end

#indexJson

Returns Json that contains a list of all files related to a cloud_object.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
this.http.get(`127.0.0.1/help/tickets/${ticket_id}/files.json`);

Returns:

  • (Json)

    Json that contains a list of all files related to a cloud_object



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
# File 'app/controllers/lesli/interfaces/controllers/files.rb', line 48

def index
    file_model = file_model() # If there is a custom file model, it must be returned in this method
    cloud_object_model = file_model.cloud_object_model
     = cloud_object_model.reflect_on_association(:account).klass

    #Get filters from http request
    filters = params[:f]

    #Get start and final date only if the request have filters
    unless filters.blank?
        file_type = filters[:file_type]
    end

    respond_to do |format|
        format.json do
            @files = file_model.where(
                "#{cloud_object_model.table_name}_id".to_sym => params["#{cloud_object_model.name.demodulize.underscore}_id".to_sym]
            )

            # Filter results
            unless file_type.blank?
                @files = @files.where(file_type: file_type)
            end

            @files = @files
            .order(id: :desc).map do |file|
                file_attributes = file.attributes
                file_attributes["user_creator_name"] = file.user_creator&.full_name
                file_attributes["public_url"] = file.attachment_public.url if file.attachment_public
                file_attributes["created_at_raw"] = file_attributes["created_at"]
                file_attributes["created_at"] = LC::Date2.new(file_attributes["created_at"]).date_time.to_s
                file_attributes["updated_at_raw"] = file_attributes["updated_at"]
                file_attributes["updated_at"] = LC::Date2.new(file_attributes["updated_at"]).date_time.to_s
                file_attributes["editable"] = file.is_editable_by?(current_user)
                file_attributes
            end

            if block_given?
                yield(@files)
            else
                respond_with_successful(@files)
            end
        end

        format.zip do

        end
    end
end

#optionsvoid

This method returns an undefined value.

Examples:

# Executing this controller's action from javascript's frontend
this.http.get('127.0.0.1/house/options/project/files')


299
300
301
302
# File 'app/controllers/lesli/interfaces/controllers/files.rb', line 299

def options
    file_model = file_model() # If there is a custom file model, it must be returned in this method
    respond_with_successful(file_model.options(@query))
end

#showvoid

This method returns an undefined value.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let file_id = 5;
this.http.get(`127.0.0.1/help/tickets/${ticket_id}/files/${file_id}`);


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
# File 'app/controllers/lesli/interfaces/controllers/files.rb', line 224

def show
    set_file
    return respond_with_not_found unless @file

    file_model = file_model()

    disposition = "inline"
    disposition = "attachment" if params["download"]

    begin
        # Sending file using CarrierWave
        if @file.attachment_s3.file

            # We either get the file from AWS and serve it ourselves or provide a direct AWS link with expiration time
            if @file.size_mb && @file.size_mb > file_model.size_threshold
                redirect_to(@file.refresh_external_url, allow_other_host: true)
            else
                send_data(@file.attachment_s3.read, filename: @file.attachment_s3_identifier, disposition: disposition, stream: "true")
            end
        elsif @file.attachment_public.file
            redirect_to(@file.attachment_public_url, allow_other_host: true)
        else
            send_data(@file.attachment.read, filename: @file.attachment_identifier, disposition: disposition, stream: "true")
        end
    rescue => exception
        # Logging the failure to retrieve the file
        @file.cloud_object.activities.create(
            user_creator: current_user,
            category: "action_not_found_file",
            description: "#{@file.name} - #{@file.attachment_identifier}"
        )

        # Returning a generic image in response if the file is not found on storage
        redirect_to("/assets/global/generic.png")
    end
end

#updateJson

Returns Json that contains wheter the creation of the file was successful or not. If it is not successful, it returs an error message.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let data = {
    ticket_file: {
        file: FILE_CONTENT
        name: "contract_information"
    }
};
this.http.put(`127.0.0.1/help/tickets/${ticket_id}/files`, data);

Returns:

  • (Json)

    Json that contains wheter the creation of the file was successful or not. If it is not successful, it returs an error message



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
# File 'app/controllers/lesli/interfaces/controllers/files.rb', line 185

def update
    set_file
    return respond_with_not_found unless @file

    # Verifying the extension of the file. If it's valid, the block will be executed
    decode_and_verify_file(file_params) do |verified_file_params|
        if @file.update(verified_file_params)

            # Setting up file uploader to upload in background
            Files::AwsUploadJob.perform_later(@file)

            if block_given?
                yield(@cloud_object, @file)
            else
                # Registering an activity in the cloud_object
                @file.cloud_object.activities.create(
                    user_creator: current_user,
                    category: "action_update_file",
                    description: "#{@file.name} - #{@file.attachment_identifier}"
                )

                # Returning the 200 HTTP response
                respond_with_successful(@file)
            end
        else
            respond_with_error(@file.errors.full_messages.to_sentence)
        end
    end
end

#zip_downloadvoid

This method returns an undefined value.

Examples:

# Executing this controller's action from javascript's frontend
this.http.get('127.0.0.1/house/projects/1/resources/files-zip-download&ids=1,2,3,4');


311
312
313
# File 'app/controllers/lesli/interfaces/controllers/files.rb', line 311

def zip_download
            
end