このコミットが含まれているのは:
2026-07-12 12:43:31 +09:00
コミット 769966648b
4個のファイルの変更354行の追加209行の削除
+11 -93
ファイルの表示
@@ -44,7 +44,7 @@ class PostImportsController < ApplicationController
end
def validate
rows = validate_rows_params
rows = normalised_import_rows(allow_warning_fields: true)
changed_row = Integer(params[:changed_row], exception: false)
result =
PostImportPreviewer.new(
@@ -61,7 +61,7 @@ class PostImportsController < ApplicationController
def create
result = PostImportRunner.new(
actor: current_user,
rows: create_rows_params,
rows: normalised_import_rows,
existing: params[:existing]).run
render json: result, status: result[:created].positive? ? :created : :ok
rescue ArgumentError => e
@@ -117,6 +117,7 @@ class PostImportsController < ApplicationController
raw = mappings.to_unsafe_h
unknown_fields = raw.keys - PostImportPreviewer::FIELDS
raise ArgumentError, '列割当て先が不正です.' if unknown_fields.present?
raise ArgumentError, '列割当てが大きすぎます.' if raw.to_json.bytesize > PostImportSourceParser::MAX_BYTES
permitted =
mappings.permit(*PostImportPreviewer::FIELDS.map { |field|
@@ -139,105 +140,22 @@ class PostImportsController < ApplicationController
parsed && parsed >= 0 && parsed < column_count
})
)
raise ArgumentError, '列番号が重複しています.' if Array(columns).uniq.length != Array(columns).length
parsed_columns = Array(columns).map { Integer(_1) }
raise ArgumentError, '列番号が重複しています.' if parsed_columns.uniq.length != parsed_columns.length
raise ArgumentError, '固定値が不正です.' if kind == 'fixed' && !value['value'].is_a?(String)
if value['value'].to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES
raise ArgumentError, '固定値が大きすぎます.'
end
result[field] = {
'kind' => kind,
'value' => value['value'].to_s,
'columns' => Array(columns).map { Integer(_1) } }
'columns' => parsed_columns }
end
end
def validate_rows_params
def normalised_import_rows allow_warning_fields: false
rows = params[:rows]
raise ArgumentError, '取込件数が多すぎます.' unless rows.is_a?(Array)
raise ArgumentError, '取込件数が多すぎます.' if rows.length > PostImportSourceParser::MAX_ROWS
normalised_rows = rows.map do |row|
unless row.is_a?(Hash) || row.is_a?(ActionController::Parameters)
raise ArgumentError, '行の形式が不正です.'
end
value = ActionController::Parameters.new(row).permit(
:source_row,
:sourceRow,
:url,
:metadata_url,
:metadataUrl,
attributes: {},
provenance: {},
tag_sources: {},
tagSources: {},
fetch_warnings: [],
fetchWarnings: [],
validation_warnings: [],
validationWarnings: [])
normalised = value.to_h.deep_transform_keys { _1.to_s.underscore }
attributes = normalised.fetch('attributes', { })
provenance = normalised.fetch('provenance', { })
allowed = PostImportPreviewer::FIELDS
unless attributes.is_a?(Hash) && provenance.is_a?(Hash) &&
(attributes.keys - allowed).empty? &&
(provenance.keys - (allowed + ['url'])).empty?
raise ArgumentError, '行の形式が不正です.'
end
unless provenance.values.all? { _1.in?(['automatic', 'mapped', 'fixed', 'manual']) }
raise ArgumentError, '値の由来が不正です.'
end
raise ArgumentError, 'セルが大きすぎます.' if [normalised['url'], *attributes.values].any? { |value|
value.to_s.bytesize > PostImportSourceParser::MAX_CELL_BYTES
}
if normalised['tag_sources'].present? &&
(!normalised['tag_sources'].is_a?(Hash) ||
(normalised['tag_sources'].keys - ['automatic', 'mapped', 'fixed', 'manual']).present?)
raise ArgumentError, 'タグ由来の形式が不正です.'
end
if (normalised['tag_sources'].present? &&
(!normalised['tag_sources'].values.all?(String) ||
normalised['tag_sources'].values.any? { |value|
value.bytesize > PostImportSourceParser::MAX_CELL_BYTES
} ||
(normalised['tag_sources'].values.sum(&:bytesize) >
PostImportSourceParser::MAX_CELL_BYTES)))
raise ArgumentError, 'タグ由来が大きすぎます.'
end
source_row = Integer(normalised['source_row'], exception: false)
raise ArgumentError, '元行番号が不正です.' if source_row.nil? || source_row <= 0
normalised['source_row'] = source_row
normalised.symbolize_keys
end
source_rows = normalised_rows.map { _1[:source_row] }
if source_rows.uniq.length != source_rows.length
raise ArgumentError, '元行番号が不正です.'
end
normalised_rows
end
def create_rows_params
rows = params[:rows]
raise ArgumentError, '取込行の形式が不正です.' unless rows.is_a?(Array)
rows.map do |row|
unless row.is_a?(Hash) || row.is_a?(ActionController::Parameters)
raise ArgumentError, '取込行の形式が不正です.'
end
parameters =
row.is_a?(ActionController::Parameters) ? row : ActionController::Parameters.new(row)
parameters.permit(
:source_row,
:sourceRow,
:url,
:metadata_url,
:metadataUrl,
attributes: {},
provenance: {},
tag_sources: {},
tagSources: {}).to_h
end
PostImportRowNormaliser.normalise!(rows, allow_warning_fields:)
end
end