Update PUT result file endpoint, refactor

This commit is contained in:
Urban Rotnik 2020-07-25 11:41:26 +02:00
parent 4958139699
commit 9b06510dcc
4 changed files with 85 additions and 75 deletions

View file

@ -28,7 +28,7 @@ module Api
blob = ActiveStorage::Blob.create_after_upload!(
io: StringIO.new(Base64.decode64(asset_params[:file_data])),
filename: asset_params[:file_name],
content_type: asset_params[:content_type]
content_type: asset_params[:file_type]
)
asset = @step.assets.new(file: blob)
end
@ -48,7 +48,7 @@ module Api
return params.require(:data).require(:attributes).permit(:file) if @form_multipart_upload
attr_list = %i(file_data content_type file_name)
attr_list = %i(file_data file_type file_name)
params.require(:data).require(:attributes).require(attr_list)
params.require(:data).require(:attributes).permit(attr_list)
end
@ -59,7 +59,7 @@ module Api
end
def check_upload_type
@form_multipart_upload = true if params.dig(:data, :attributes)[:file]
@form_multipart_upload = true if params.dig(:data, :attributes, :file)
end
end
end

View file

@ -17,7 +17,6 @@ module Api
def create
create_text_result if result_text_params.present?
create_file_result if !@result && result_file_params.present?
render jsonapi: @result,
@ -31,6 +30,7 @@ module Api
update_file_result if result_file_params.present? && @result.is_asset
update_text_result if result_text_params.present? && @result.is_text
if (@result.changed? && @result.save!) || @asset_result_updated
render jsonapi: @result,
serializer: ResultSerializer,
@ -103,22 +103,12 @@ module Api
def create_file_result
Result.transaction do
@result = @task.results.create!(result_params.merge(user_id: current_user.id))
if !result_file_params[:file]
filedata = Base64.decode64(result_file_params[:file_data])
content_type = result_file_params[:content_type]
filename = result_file_params[:file_name]
blob = ActiveStorage::Blob.create_after_upload!(
io: StringIO.new(filedata),
filename: filename,
content_type: content_type
)
asset = Asset.create!(file: blob)
else # multipart form
if @form_multipart_upload
asset = Asset.create!(result_file_params)
else
blob = create_blob_from_params
asset = Asset.create!(file: blob)
end
ResultAsset.create!(asset: asset, result: @result)
end
end
@ -127,12 +117,26 @@ module Api
old_checksum, new_checksum = nil
Result.transaction do
old_checksum = @result.asset.file.blob.checksum
@result.asset.file.attach(result_file_params[:file])
if @form_multipart_upload
@result.asset.file.attach(result_file_params[:file])
else
blob = create_blob_from_params
@result.asset.update!(file: blob)
end
new_checksum = @result.asset.file.blob.checksum
end
@asset_result_updated = old_checksum != new_checksum
end
def create_blob_from_params
blob = ActiveStorage::Blob.create_after_upload!(
io: StringIO.new(Base64.decode64(result_file_params[:file_data])),
filename: result_file_params[:file_name],
content_type: result_file_params[:file_type]
)
blob
end
def result_params
raise TypeError unless params.require(:data).require(:type) == 'results'
@ -154,11 +158,14 @@ module Api
prms = params[:included]&.select { |el| el[:type] == 'result_files' }&.first
return nil unless prms
return prms.dig(:attributes).permit(:file) if prms.require(:attributes)[:file]
if prms.require(:attributes)[:file]
@form_multipart_upload = true
return prms.dig(:attributes).permit(:file)
end
attr_list = %i(file_data file_type file_name)
prms.require(:attributes).require(:file_data)
prms.require(:attributes).require(:file_name)
prms.dig(:attributes).permit(:file_data, :file_name)
prms.require(:attributes).require(attr_list)
prms.dig(:attributes).permit(attr_list)
end
def tiny_mce_asset_params

View file

@ -165,13 +165,7 @@ RSpec.describe 'Api::V1::AssetsController', type: :request do
'AACCAIAAAD91JpzAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAE0lE'\
'QVQIHWP8//8/AwMDExADAQAkBgMBOOSShwAAAABJRU5ErkJggg=='
end
let(:attributes) do
{
file_data: filedata_base64,
file_name: 'file.png',
content_type: 'image/png'
}
end
let(:attributes) { { file_data: filedata_base64, file_name: 'file.png', file_type: 'image/png' } }
let(:request_body) { super().to_json }
it 'creates new asset' do
@ -193,15 +187,7 @@ RSpec.describe 'Api::V1::AssetsController', type: :request do
end
context 'when has missing param' do
let(:request_body) do
{
data: {
type: 'attachments',
attributes: {
}
}
}
end
let(:request_body) { { data: { type: 'attachments', attributes: {} } } }
it 'renders 400' do
action

View file

@ -289,7 +289,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
), params: request_body, headers: @valid_headers)
end
context 'when file is base64' do
context 'when sending base64' do
let(:filedata_base64) do
'iVBORw0KGgoAAAANSUhEUgAAAAIAA'\
'AACCAIAAAD91JpzAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAE0lE'\
@ -299,7 +299,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
{
file_data: filedata_base64,
file_name: 'file.png',
content_type: 'image/png'
file_type: 'image/png'
}
end
let(:request_body) { super().to_json }
@ -315,7 +315,7 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
end
end
context 'when file is multipart form' do
context 'when sending multipart form' do
let(:attributes) { { file: fixture_file_upload('files/test.jpg', 'image/jpg') } }
it 'creates new asset' do
@ -396,7 +396,6 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
context 'when resultType is file' do
let(:result_file) { @valid_task.results.last }
let(:file) { fixture_file_upload('files/test.jpg', 'image/jpg') }
let(:second_file) { fixture_file_upload('files/apple.jpg', 'image/jpg') }
let(:request_body) do
{
data: {
@ -437,38 +436,56 @@ RSpec.describe 'Api::V1::ResultsController', type: :request do
end
end
# ### Refactor without instance variables
#
# context 'when has new image for update' do
# let(:request_body_with_same_name_new_file) do
# {
# data: {
# type: 'results',
# attributes: {
# name: result_file.reload.name
# }
# },
# included: [
# { type: 'result_files',
# attributes: {
# file: second_file
# } }
# ]
# }
# end
#
# it 'returns status 200' do
# put(api_v1_team_project_experiment_task_result_path(
# team_id: @teams.first.id,
# project_id: @valid_project,
# experiment_id: @valid_experiment,
# task_id: @valid_task,
# id: result_file.id
# ), params: request_body_with_same_name_new_file, headers: @valid_headers)
#
# expect(response).to have_http_status 200
# end
# end
context 'when has new image for update' do
let(:action) do
put(api_v1_team_project_experiment_task_result_path(
team_id: @teams.first.id,
project_id: @valid_project,
experiment_id: @valid_experiment,
task_id: @valid_task,
id: result_file.id
), params: request_body, headers: @valid_headers)
end
let(:request_body) do
{
data: { type: 'results', attributes: { name: result_file.reload.name } },
included: [{ type: 'result_files', attributes: attributes }]
}
end
context 'when sending base64' do
let(:filedata_base64) do
'iVBORw0KGgoAAAANSUhEUgAAAAIAA'\
'AACCAIAAAD91JpzAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAE0lE'\
'QVQIHWP8//8/AwMDExADAQAkBgMBOOSShwAAAABJRU5ErkJggg=='
end
let(:attributes) do
{
file_data: filedata_base64,
file_name: 'file.png',
file_type: 'image/png'
}
end
let(:request_body) { super().to_json }
it 'returns status 200' do
action
expect(response).to have_http_status 200
end
end
context 'when sending multipart form' do
let(:attributes) { { file: fixture_file_upload('files/apple.jpg', 'image/jpg') } }
it 'returns status 200' do
action
expect(response).to have_http_status 200
end
end
end
context 'when there is nothing to update' do
let(:request_body_with_same_name) do