Class: Lifter::FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lifter/file_manager.rb

Constant Summary collapse

DEFAULT_HASH_METHOD =
:md5
SCRUB_HEADERS =
%w(host content-type content-length accept accept-encoding accept-language
connection)

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ FileManager

Returns a new instance of FileManager.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lifter/file_manager.rb', line 10

def initialize(config)
  @working_dir = resolve_working_dir(config.get(:working_dir))

  @authorize_webhook_endpoint = config.get(:authorize_webhook)
  @completed_webhook_endpoint = config.get(:completed_webhook)

  @work = ThreadPool.new(5)
  @webhooks = ThreadPool.new(5)
  @files = FilePool.new

  @upload_hash_method = config.get(:upload_hash_method) || DEFAULT_HASH_METHOD
  @upload_prologue_size = config.get(:upload_prologue_size)
end

Instance Method Details

#cancel_file(connection, file_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lifter/file_manager.rb', line 70

def cancel_file(connection, file_id)
  @webhooks.clear(file_id)

  @work.push(file_id) do
    file = @files.get(file_id)

    file.close
    file.rm

    @files.remove(file_id)
  end
end

#close_file(connection, file_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lifter/file_manager.rb', line 51

def close_file(connection, file_id)
  @work.push(file_id) do
    file = @files.get(file_id)

    file.flush
    file.close

    file.mv("#{@working_dir}/completed/#{file_id}")

    # In the event the upload was too small for the prologue size to have been met previously,
    # ensure the authorize webhook is fired off before the completed webhook.
    if file.prologue.length < @upload_prologue_size
      authorize_file(connection, file_id, file)
    end

    complete_file(connection, file_id, file)
  end
end

#open_file(connection, file_id, file_param, file_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lifter/file_manager.rb', line 24

def open_file(connection, file_id, file_param, file_name)
  @work.push(file_id) do
    file = File.open("#{@working_dir}/progress/#{file_id}", 'wb')

    file_opts = {
      hash_method: @upload_hash_method,
      prologue_size: @upload_prologue_size,
      original_name: file_name,
      original_request: connection.request,
      param: file_param
    }

    @files.add(file_id, file, file_opts)
  end
end

#write_file_data(connection, file_id, data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/lifter/file_manager.rb', line 40

def write_file_data(connection, file_id, data)
  @work.push(file_id) do
    file = @files.get(file_id)
    file.write(data)

    if file.prologue.length >= @upload_prologue_size
      authorize_file(connection, file_id, file)
    end
  end
end