Module: Joblin::BackgroundTask::Attachments

Extended by:
ActiveSupport::Concern
Includes:
Concerns::JobWorkingDirs
Included in:
Joblin::BackgroundTask
Defined in:
app/models/joblin/background_task/attachments.rb

Instance Method Summary collapse

Methods included from Concerns::JobWorkingDirs

#working_dir

Instance Method Details

#attach_file(key, path, as: nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'app/models/joblin/background_task/attachments.rb', line 25

def attach_file(key, path, as: nil)
  path = File.join(working_dir, path) unless Pathname.new(path).absolute?

  self.send(key).attach(
    io: File.open(path),
    filename: as || File.basename(path)
  )
end

#attachment_path(key, expires_in: true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/joblin/background_task/attachments.rb', line 8

def attachment_path(key, expires_in: true)
  if !expires_in || Rails.env.development? || Rails.env.test?
    return Rails.application.routes.url_helpers.rails_blob_path(
      send(key),
      only_path: true,
      disposition: 'attachment',
      # organization_id: current_organization&.id,
    )
  end

  if expires_in == true
    send(key).url
  else expires_in
    send(key).url(expires_in:)
  end
end

#load_attachment(key, save_as: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/joblin/background_task/attachments.rb', line 34

def load_attachment(key, save_as: nil)
  @loaded_attachments ||= {}

  @loaded_attachments[key] ||= begin
    save_as = save_as || send(key).filename.to_s || key.to_s
    save_as = File.join(working_dir, save_as) unless Pathname.new(save_as).absolute?

    File.open(save_as, 'w') do |file|
      send(key).download do |chunk|
        file.write(chunk.force_encoding("UTF-8"))
      end
    end
    save_as
  end
end