Class: Railspress::Api::V1::PostImportsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/railspress/api/v1/post_imports_controller.rb

Constant Summary collapse

SUPPORTED_EXTENSIONS =
%w[.md .markdown .txt .zip].freeze

Instance Attribute Summary

Attributes inherited from BaseController

#current_api_key

Instance Method Summary collapse

Instance Method Details

#createObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/railspress/api/v1/post_imports_controller.rb', line 11

def create
  source = import_source
  return render_error("Either file or signed_blob_id is required.", status: :unprocessable_content) unless source

  unless supported_file?(source[:filename])
    return render_error("Unsupported import file type. Allowed: .md, .markdown, .txt, .zip.", status: :unprocessable_content)
  end

  import = Railspress::Import.create!(
    import_type: "posts",
    filename: source[:filename],
    content_type: source[:content_type],
    status: "pending",
    user_id: current_api_key&.owner_id
  )

  file_path = persist_import_file(import, source)
  Railspress::ImportPostsJob.perform_later(import.id, [ file_path ])

  render json: { data: serialize_import(import.reload) }, status: :accepted
rescue ActiveSupport::MessageVerifier::InvalidSignature, ActiveRecord::RecordNotFound
  render_error("Invalid signed blob id.", status: :unprocessable_content)
rescue => error
  Rails.logger.warn("Failed to create post import via API: #{error.class} #{error.message}")
  render_error("Failed to queue import.", status: :unprocessable_content)
end

#showObject



38
39
40
# File 'app/controllers/railspress/api/v1/post_imports_controller.rb', line 38

def show
  render json: { data: serialize_import(@import) }
end