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
-
.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
-
#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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path 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 |
# File 'app/models/download.rb', line 44 class Download < ApplicationRecord include Housekeeping include Shared::IsData 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 private STORAGE_PATH = Rails.root.join(Rails.env.test? ? 'tmp' : '', "downloads#{ENV['TEST_ENV_NUMBER']}").freeze def dir_path str = id.to_s.rjust(9, '0') STORAGE_PATH.join(str[-str.length..-7], str[-6..-4], str[-3..-1]) end def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path end end |
Class Method Details
.storage_path ⇒ Object
Gets the downloads storage path
59 60 61 |
# File 'app/models/download.rb', line 59 def self.storage_path STORAGE_PATH end |
Instance Method Details
#delete_file ⇒ Object
Deletes associated file from storage
91 92 93 94 95 96 |
# File 'app/models/download.rb', line 91 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)
102 103 104 105 |
# File 'app/models/download.rb', line 102 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.
80 81 82 |
# File 'app/models/download.rb', line 80 def expired? expires < Time.now end |
#file ⇒ Object
74 75 76 |
# File 'app/models/download.rb', line 74 def file File.read(file_path) end |
#file_path ⇒ Pathname
Retrieves the full-path of stored file
70 71 72 |
# File 'app/models/download.rb', line 70 def file_path dir_path.join(filename) end |
#ready? ⇒ Boolean
Tells whether the download is ready to be downloaded.
86 87 88 |
# File 'app/models/download.rb', line 86 def ready? !expired? && file_path.exist? end |
#save_file ⇒ Object (private)
107 108 109 110 |
# File 'app/models/download.rb', line 107 def save_file FileUtils.mkdir_p(dir_path) FileUtils.cp(@source_file_path, file_path) if @source_file_path end |
#source_file_path=(path) ⇒ Object
Used as argument for :new.
64 65 66 |
# File 'app/models/download.rb', line 64 def source_file_path=(path) @source_file_path = path end |