Class: Collavre::Creatives::Importer

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/creatives/importer.rb

Defined Under Namespace

Classes: Error, UnsupportedFile

Constant Summary collapse

MARKDOWN_MIME_TYPES =
%w[text/markdown text/x-markdown application/octet-stream].freeze
PPT_MIME_TYPES =
%w[
  application/vnd.ms-powerpoint
  application/vnd.openxmlformats-officedocument.presentationml.presentation
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(file:, user:, parent: nil) ⇒ Importer

Returns a new instance of Importer.



13
14
15
16
17
# File 'app/services/collavre/creatives/importer.rb', line 13

def initialize(file:, user:, parent: nil)
  @file = file
  @user = user
  @parent = parent
end

Instance Method Details

#callObject

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/collavre/creatives/importer.rb', line 19

def call
  raise Error, "File required" if file.blank?

  case mime_type
  when *MARKDOWN_MIME_TYPES
    content = read_file_content
    MarkdownImporter.import(content, parent: parent, user: user, create_root: true)
  when *PPT_MIME_TYPES
    PptImporter.import(file.tempfile, parent: parent, user: user, create_root: true, filename: file.original_filename)
  else
    raise UnsupportedFile, "Invalid file type"
  end
end