2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'app/controllers/uploaded_files_controller.rb', line 2
def file_upload
if request.["HTTP_ACCEPT"].split(",").map(&:strip).include?("application/json")
content_type = "application/json"
else
content_type = "text/plain"
end
uploaded_file = UploadedFile.new(file: params[:files][0])
if uploaded_file.save
render json: {
files: [
{
"name" => uploaded_file.read_attribute(:file_file_name),
"size" => uploaded_file.read_attribute(:file_file_size),
"url" => uploaded_file.file.url,
"id" => uploaded_file.id
}
]
}, content_type: content_type
else
render json: {
error: {
"message" => "An error prevents the uploaded file to be saved"
}
}, content_type: content_type
end
end
|