Class: ProcessUploadedFileJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/process_uploaded_file_job.rb

Instance Method Summary collapse

Instance Method Details

#add_single_file_to_model(model, file) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/jobs/process_uploaded_file_job.rb', line 69

def add_single_file_to_model(model, file)
  # Handle different file types
  case File.extname(file.original_filename).delete(".").downcase
  when *MediaType.indexable_extensions
    if (existing_file = model.model_files.where(filename: file.original_filename).first)
      existing_file.update(attachment: file)
      existing_file
    else
      model.model_files.create(filename: file.original_filename, attachment: file)
    end
  else
    Rails.logger.warn("Ignoring #{file.inspect}")
  end
end

#create_new_model(library, name: nil, owner: nil, creator_id: nil, collection_ids: [], tag_list: nil, license: nil, sensitive: nil, permission_preset: nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/jobs/process_uploaded_file_job.rb', line 51

def create_new_model(library, name: nil, owner: nil, creator_id: nil, collection_ids: [], tag_list: nil, license: nil, sensitive: nil, permission_preset: nil)
  params = {
    name: name,
    path: SecureRandom.uuid,
    creator_id: creator_id,
    collections: collection_ids.map { Collection.find(it) },
    tag_list: tag_list,
    license: license,
    sensitive: sensitive,
    permission_preset: permission_preset,
    owner: owner
  }.compact_blank
  # Create model
  model = library.models.create!(params)
  model.organize!
  model
end

#is_archive?(file) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/jobs/process_uploaded_file_job.rb', line 47

def is_archive?(file)
  MediaType.archive_extensions.include? File.extname(file.original_filename).delete(".").downcase
end

#perform(library_id, uploaded_file, name: nil, owner: nil, creator_id: nil, collection_ids: [], tag_list: nil, license: nil, model: nil, sensitive: nil, permission_preset: nil) ⇒ Object



4
5
6
7
8
9
10
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
37
38
39
40
41
42
43
44
45
# File 'app/jobs/process_uploaded_file_job.rb', line 4

def perform(library_id, uploaded_file, name: nil, owner: nil, creator_id: nil, collection_ids: [], tag_list: nil, license: nil, model: nil, sensitive: nil, permission_preset: nil)
  new_files = []
  new_model = model.nil?
  attachers = []

  ActiveRecord::Base.transaction do
    # Find library
    library = Library.find(library_id)
    return if library.nil?

    attachers = Array.wrap(uploaded_file).map do
      # Attach cached upload file
      attacher = ModelFileUploader::Attacher.new
      attacher.attach_cached(it)
      attacher
    end

    name ||= File.basename(attachers.first.file.original_filename, ".*").humanize.tr("+", " ").careful_titleize
    model ||= create_new_model(library, name: name, owner: owner, creator_id: creator_id, collection_ids: collection_ids, tag_list: tag_list, license: license, sensitive: sensitive, permission_preset: permission_preset)

    attachers.each do
      new_files << if new_model && (attachers.length == 1) && is_archive?(it.file)
        unzip_into_model(model, it.file)
      else
        add_single_file_to_model(model, it.file)
      end
    end
  end

  # Queue scans to fill in data or update things
  if new_model
    model.add_new_files_later(include_all_subfolders: true)
  else
    model.check_for_problems_later
  end
  new_files.flatten.each(&:parse_metadata_later)

  attachers.each do
    # Discard cached file
    it.destroy
  end
end