summaryrefslogtreecommitdiff
path: root/app/models/upload.rb
diff options
context:
space:
mode:
authorSean McGivern <sean@mcgivern.me.uk>2018-02-02 14:00:21 +0000
committerSean McGivern <sean@mcgivern.me.uk>2018-02-02 14:00:21 +0000
commit54a575f1bbba44573ab92dc58a4242f1ee734c5d (patch)
tree0ee2afe8228f184f7ad28e74d86bff2c5965c4ae /app/models/upload.rb
parentc63af942e5baf7849a94fa99da8494bcba28e3f8 (diff)
parent36838a843e99f551c971ec7062327e114c8f0188 (diff)
downloadgitlab-ce-54a575f1bbba44573ab92dc58a4242f1ee734c5d.tar.gz
Merge branch '3867-port-to-ce' into 'master'
Port of gitlab-ee!3867 to CE See merge request gitlab-org/gitlab-ce!16775
Diffstat (limited to 'app/models/upload.rb')
-rw-r--r--app/models/upload.rb49
1 files changed, 29 insertions, 20 deletions
diff --git a/app/models/upload.rb b/app/models/upload.rb
index f194d7bdb80..fb55fd8007b 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -9,22 +9,11 @@ class Upload < ActiveRecord::Base
validates :model, presence: true
validates :uploader, presence: true
- before_save :calculate_checksum, if: :foreground_checksum?
- after_commit :schedule_checksum, unless: :foreground_checksum?
+ before_save :calculate_checksum!, if: :foreground_checksummable?
+ after_commit :schedule_checksum, if: :checksummable?
- def self.remove_path(path)
- where(path: path).destroy_all
- end
-
- def self.record(uploader)
- remove_path(uploader.relative_path)
-
- create(
- size: uploader.file.size,
- path: uploader.relative_path,
- model: uploader.model,
- uploader: uploader.class.to_s
- )
+ def self.hexdigest(path)
+ Digest::SHA256.file(path).hexdigest
end
def absolute_path
@@ -33,10 +22,18 @@ class Upload < ActiveRecord::Base
uploader_class.absolute_path(self)
end
- def calculate_checksum
- return unless exist?
+ def calculate_checksum!
+ self.checksum = nil
+ return unless checksummable?
- self.checksum = Digest::SHA256.file(absolute_path).hexdigest
+ self.checksum = self.class.hexdigest(absolute_path)
+ end
+
+ def build_uploader
+ uploader_class.new(model).tap do |uploader|
+ uploader.upload = self
+ uploader.retrieve_from_store!(identifier)
+ end
end
def exist?
@@ -45,8 +42,16 @@ class Upload < ActiveRecord::Base
private
- def foreground_checksum?
- size <= CHECKSUM_THRESHOLD
+ def checksummable?
+ checksum.nil? && local? && exist?
+ end
+
+ def local?
+ true
+ end
+
+ def foreground_checksummable?
+ checksummable? && size <= CHECKSUM_THRESHOLD
end
def schedule_checksum
@@ -57,6 +62,10 @@ class Upload < ActiveRecord::Base
!path.start_with?('/')
end
+ def identifier
+ File.basename(path)
+ end
+
def uploader_class
Object.const_get(uploader)
end