mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Add file size validation
This commit is contained in:
parent
1be88baab1
commit
060125a1c4
|
@ -21,9 +21,10 @@ module Api
|
|||
def create
|
||||
raise PermissionError.new(Asset, :create) unless can_manage_protocol_in_module?(@protocol)
|
||||
|
||||
asset = @step.assets.create!(asset_params)
|
||||
asset = @step.assets.new(asset_params)
|
||||
asset.save!(context: :on_api_upload)
|
||||
|
||||
asset.reload
|
||||
asset.post_process_file
|
||||
|
||||
render jsonapi: asset,
|
||||
serializer: AssetSerializer,
|
||||
|
@ -42,6 +43,7 @@ module Api
|
|||
|
||||
def load_asset
|
||||
@asset = @step.assets.find(params.require(:id))
|
||||
raise PermissionError.new(Asset, :read) unless can_read_protocol_in_module?(@asset.step.protocol)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,8 +19,8 @@ class Asset < ApplicationRecord
|
|||
# This could cause some problems if you create empty asset and want to
|
||||
# assign it to result
|
||||
validate :step_or_result_or_repository_asset_value
|
||||
validate :wopi_filename_valid,
|
||||
on: :wopi_file_creation
|
||||
validate :wopi_filename_valid, on: :wopi_file_creation
|
||||
validate :check_file_size, on: :on_api_upload
|
||||
|
||||
belongs_to :created_by,
|
||||
foreign_key: 'created_by_id',
|
||||
|
@ -448,4 +448,12 @@ class Asset < ApplicationRecord
|
|||
)
|
||||
end
|
||||
end
|
||||
|
||||
def check_file_size
|
||||
if file.attached?
|
||||
if file.blob.byte_size > Rails.application.config.x.file_max_size_mb.megabytes
|
||||
errors.add(:file, I18n.t('activerecord.errors.models.asset.attributes.file.too_big'))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -85,6 +85,10 @@ en:
|
|||
attributes:
|
||||
x:
|
||||
not_unique: "and Y position has already been taken by another task in the experiment."
|
||||
asset:
|
||||
attributes:
|
||||
file:
|
||||
too_big: "is too big"
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -89,6 +89,22 @@ RSpec.describe 'Api::V1::AssetsController', type: :request do
|
|||
expect(response).to have_http_status(404)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when asset is not found' do
|
||||
it 'renders 404' do
|
||||
get(api_v1_team_project_experiment_task_protocol_step_asset_path(
|
||||
team_id: @team.id,
|
||||
project_id: @project.id,
|
||||
experiment_id: @experiment.id,
|
||||
task_id: @task.id,
|
||||
protocol_id: @protocol.id,
|
||||
step_id: @step.id,
|
||||
id: -1
|
||||
), headers: @valid_headers)
|
||||
|
||||
expect(response).to have_http_status(404)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST step, #create' do
|
||||
|
@ -98,6 +114,7 @@ RSpec.describe 'Api::V1::AssetsController', type: :request do
|
|||
|
||||
before :each do
|
||||
@file = fixture_file_upload('files/test.jpg', 'image/jpg')
|
||||
allow_any_instance_of(Asset).to receive(:post_process_file)
|
||||
end
|
||||
|
||||
let(:action) do
|
||||
|
@ -134,6 +151,12 @@ RSpec.describe 'Api::V1::AssetsController', type: :request do
|
|||
|
||||
expect(response).to have_http_status 201
|
||||
end
|
||||
|
||||
it 'calls post_process_file function for text extraction' do
|
||||
expect_any_instance_of(Asset).to receive(:post_process_file)
|
||||
|
||||
action
|
||||
end
|
||||
end
|
||||
|
||||
context 'when has missing param' do
|
||||
|
|
Loading…
Reference in a new issue