Class: Download
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Download
- Includes:
- Housekeeping, Shared::IsData
- Defined in:
- app/models/download.rb
Overview
A Download represents an expirable file (mostly ZIP files) users can download.
Direct Known Subclasses
BasicNomenclature, Bibtex, Coldp, DwcArchive, PaperCatalog, ProjectDump, Text
Defined Under Namespace
Classes: BasicNomenclature, Bibtex, Coldp, DwcArchive, PaperCatalog, ProjectDump, Text
Constant Summary collapse
- STORAGE_PATH =
Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze
Instance Attribute Summary collapse
-
#description ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#expires ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#filename ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#is_public ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#name ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#project_id ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#request ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#times_downloaded ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
-
#total_records ⇒ Integer?
And estimate of the total records (rows of data) in this Download.
-
#type ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
Class Method Summary collapse
- .api_buildable? ⇒ Boolean
-
.storage_path ⇒ Object
Gets the downloads storage path.
Instance Method Summary collapse
-
#delete_file ⇒ Object
Deletes associated file from storage.
- #dir_path ⇒ Object private
-
#expired? ⇒ Boolean
Tells whether the download expiry date has been surpassed.
- #file ⇒ Object
-
#file_path ⇒ Pathname
Retrieves the full-path of stored file.
-
#ready? ⇒ Boolean
Tells whether the download is ready to be downloaded.
-
#save_file ⇒ Object
private
This is the only method to move a temporary file to its location on the file server.
- #set_sha2 ⇒ Object private
-
#source_file_path=(path) ⇒ Object
Used as argument for :new.
Methods included from Shared::IsData
#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar
Methods included from Housekeeping
#has_polymorphic_relationship?
Methods inherited from ApplicationRecord
Instance Attribute Details
#description ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#expires ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#filename ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#is_public ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#name ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#project_id ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#request ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#times_downloaded ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#total_records ⇒ Integer?
Returns and estimate of the total records (rows of data) in this Download. Because Downloads can be variously create some generating might not accurate count the total.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
#type ⇒ String, ...
A Download represents an expirable file (mostly ZIP files) users can download.
44 45 46 47 48 49 50 51 52 53 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData # TODO: consider removing default_scope { where('expires >= ?', Time.now) } after_save :save_file after_destroy :delete_file validates_presence_of :name validates_presence_of :filename validates_presence_of :expires validates_presence_of :type # Gets the downloads storage path def self.storage_path STORAGE_PATH end # Used as argument for :new. def source_file_path=(path) @source_file_path = path end # @return [Pathname] # Retrieves the full-path of stored file def file_path dir_path.join(filename) end def file File.read(file_path) end # @return [Boolean] # Tells whether the download expiry date has been surpassed. def expired? expires < Time.now end # @return [Boolean] # Tells whether the download is ready to be downloaded. def ready? !expired? && file_path.exist? end # Deletes associated file from storage def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end def self.api_buildable? false end private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end # This is the only method to move a temporary file # to its location on the file server. # # ActiveJob generating files trigger this method # by .updating the filename attribute. def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end end |
Class Method Details
.api_buildable? ⇒ Boolean
99 100 101 |
# File 'app/models/download.rb', line 99 def self.api_buildable? false end |
.storage_path ⇒ Object
Gets the downloads storage path
60 61 62 |
# File 'app/models/download.rb', line 60 def self.storage_path STORAGE_PATH end |
Instance Method Details
#delete_file ⇒ Object
Deletes associated file from storage
92 93 94 95 96 97 |
# File 'app/models/download.rb', line 92 def delete_file path = dir_path raise 'Download: dir_path not pointing inside storage path! Aborting deletion' unless path.to_s.start_with?(STORAGE_PATH.to_s) FileUtils.rm_rf(path) end |
#dir_path ⇒ Object (private)
113 114 115 116 |
# File 'app/models/download.rb', line 113 def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end |
#expired? ⇒ Boolean
Tells whether the download expiry date has been surpassed.
81 82 83 |
# File 'app/models/download.rb', line 81 def expired? expires < Time.now end |
#file ⇒ Object
75 76 77 |
# File 'app/models/download.rb', line 75 def file File.read(file_path) end |
#file_path ⇒ Pathname
Retrieves the full-path of stored file
71 72 73 |
# File 'app/models/download.rb', line 71 def file_path dir_path.join(filename) end |
#ready? ⇒ Boolean
Tells whether the download is ready to be downloaded.
87 88 89 |
# File 'app/models/download.rb', line 87 def ready? !expired? && file_path.exist? end |
#save_file ⇒ Object (private)
This is the only method to move a temporary file to its location on the file server.
ActiveJob generating files trigger this method by .updating the filename attribute.
123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/download.rb', line 123 def save_file FileUtils.mkdir_p(dir_path) if @source_file_path FileUtils.cp(@source_file_path, file_path) set_sha2 true else false end end |
#set_sha2 ⇒ Object (private)
107 108 109 110 111 |
# File 'app/models/download.rb', line 107 def set_sha2 if @source_file_path self.update_column(:sha2, Digest::SHA256.file(@source_file_path).to_s) end end |
#source_file_path=(path) ⇒ Object
Used as argument for :new.
65 66 67 |
# File 'app/models/download.rb', line 65 def source_file_path=(path) @source_file_path = path end |