0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-07 13:19:19 +08:00
discourse/app/services/custom_emoji/preview_import.rb
Martin Brennan b736e72b1c
DEV: Retroactive review and refactor of custom emoji bulk import/export (#41860)
Followup bd3906ce64

Converts the new endpoints made in the original PR into
service classes, using AI to drive implementation based
on the service authoring skill:

* CustomEmoji::Export
* CustomEmoji::PreviewImport
* CustomEmoji::ConfirmImport

In addition, some supporting action, lib, and serializer
classes have been added. This fixed a couple of
bugs as well:

* Import names now go through `Emoji.sanitize_emoji_name` like
single-emoji create
* No-group imports no longer persist a literal "default" string
* Two 500s (suspicious zip entries, malformed CSV) became proper 422s
* Duplicate CSV names are flagged at preview instead of exploding the
confirm transaction
*` Emoji.clear_cache `moved outside the transaction
2026-07-22 10:06:54 +10:00

45 lines
1 KiB
Ruby
Vendored

# frozen_string_literal: true
require "csv"
class CustomEmoji::PreviewImport
include Service::Base
EXPECTED_ARCHIVE_ERRORS = [
CSV::MalformedCSVError,
Compression::SafeZipReader::MissingEntryError,
Compression::SafeZipReader::TooManyEntriesError,
Compression::SafeZipReader::EntryTooLargeError,
Compression::SafeZipReader::SuspiciousEntryError,
].freeze
params do
attribute :file
validates :file, presence: true
end
# Has to be done this way rather than in a model step
# otherwise the error doesn't bubble up correctly
try(*EXPECTED_ARCHIVE_ERRORS) { step :stage_rows }
policy :manifest_not_empty
model :token, :store_preview
private
def stage_rows(params:, guardian:)
context[:rows] = CustomEmoji::Action::StageImportRows.call(
zip_path: params.file.tempfile.path,
acting_user: guardian.user,
)
end
def manifest_not_empty(rows:)
rows.present?
end
def store_preview(rows:, guardian:)
CustomEmoji::ImportPreviewCache.new(guardian.user).store(rows)
end
end