Class: ArchiveDownloadService
- Inherits:
-
Object
- Object
- ArchiveDownloadService
- Defined in:
- app/services/archive_download_service.rb
Instance Method Summary collapse
- #filename ⇒ Object
-
#initialize(model:, selection:) ⇒ ArchiveDownloadService
constructor
A new instance of ArchiveDownloadService.
- #output_file ⇒ Object
- #prepare(delay: 0.seconds, queue: nil) ⇒ Object
- #preparing? ⇒ Boolean
- #ready? ⇒ Boolean
- #temp_file ⇒ Object
- #wait_until_ready ⇒ Object
Constructor Details
#initialize(model:, selection:) ⇒ ArchiveDownloadService
Returns a new instance of ArchiveDownloadService.
2 3 4 5 |
# File 'app/services/archive_download_service.rb', line 2 def initialize(model:, selection:) @model = model @selection = sanitize selection end |
Instance Method Details
#filename ⇒ Object
7 8 9 10 11 12 |
# File 'app/services/archive_download_service.rb', line 7 def filename @filename ||= [ sanitize(@model.slug), @selection ].compact.join("-") + ".zip" end |
#output_file ⇒ Object
14 15 16 17 18 19 |
# File 'app/services/archive_download_service.rb', line 14 def output_file @output_file ||= File.join( ModelFileUploader.find_storage(:downloads).directory, filename ) end |
#prepare(delay: 0.seconds, queue: nil) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'app/services/archive_download_service.rb', line 36 def prepare(delay: 0.seconds, queue: nil) return if ready? || preparing? FileUtils.touch(temp_file) PrepareDownloadJob.set(wait: delay, queue: queue).perform_later( model_id: @model.id, selection: @selection ) end |
#preparing? ⇒ Boolean
32 33 34 |
# File 'app/services/archive_download_service.rb', line 32 def preparing? File.exist?(temp_file) end |
#ready? ⇒ Boolean
28 29 30 |
# File 'app/services/archive_download_service.rb', line 28 def ready? File.exist?(output_file) end |
#temp_file ⇒ Object
21 22 23 24 25 26 |
# File 'app/services/archive_download_service.rb', line 21 def temp_file @temp_file ||= File.join( ModelFileUploader.find_storage(:downloads).directory, Digest::SHA256.hexdigest(filename) ) end |
#wait_until_ready ⇒ Object
45 46 47 48 49 50 |
# File 'app/services/archive_download_service.rb', line 45 def wait_until_ready loop do break if ready? sleep(1) end end |