Class: Download

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData
Defined in:
app/models/download.rb

Overview

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API

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

Class Method Summary collapse

Instance Method Summary collapse

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

transaction_with_retry

Instance Attribute Details

#descriptionString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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

#expiresString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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

#filenameString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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_publicString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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

#nameString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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_idString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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

#requestString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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_downloadedString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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_recordsInteger?

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.

Returns:

  • (Integer, nil)

    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

#typeString, ...

A Download represents an expirable file (mostly ZIP files) users can download.

Returns:

  • (String)

    The name for this download (not file name).

  • (String)

    A description for this download.

  • (String)

    The filename of this download.

  • (String)

    The type of Download, e.g. ‘Download::DwCArchive`.

  • (String)

    The request URI path this download was generated from. This attribute may be used for caching.

  • (Datetime)

    The date and time this download is elegible for removal.

  • (Integer)

    The number of times the file was downloaded.

  • (Integer)

    the project ID

  • (Boolean, nil)

    whether the Download should be shared on the API



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_pathObject

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_fileObject

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_pathObject (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.

Returns:

  • (Boolean)


80
81
82
# File 'app/models/download.rb', line 80

def expired?
  expires < Time.now
end

#fileObject



74
75
76
# File 'app/models/download.rb', line 74

def file
  File.read(file_path)
end

#file_pathPathname

  Retrieves the full-path of stored file

Returns:

  • (Pathname)


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.

Returns:

  • (Boolean)


86
87
88
# File 'app/models/download.rb', line 86

def ready?
  !expired? && file_path.exist?
end

#save_fileObject (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