Class: Lifter::FileManager
- Inherits:
-
Object
- Object
- Lifter::FileManager
- 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
- #cancel_file(connection, file_id) ⇒ Object
- #close_file(connection, file_id) ⇒ Object
-
#initialize(config) ⇒ FileManager
constructor
A new instance of FileManager.
- #open_file(connection, file_id, file_param, file_name) ⇒ Object
- #write_file_data(connection, file_id, data) ⇒ Object
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
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/lifter/file_manager.rb', line 81 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/lifter/file_manager.rb', line 54 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}") @files.remove(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 completed. if file.prologue.length < @upload_prologue_size @webhooks.push(file_id) do webhook = (connection, file) webhook.deliver end end @webhooks.push(file_id) do webhook = create_completed_webhook(connection, file) webhook.deliver end 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 50 51 52 |
# 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 @webhooks.push(file_id) do webhook = (connection, file) webhook.deliver end end end end |