diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2018-01-08 17:33:51 -0200 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2018-01-08 19:25:31 -0200 |
commit | b1378f05a11345f761cd31ae51aafc268003150a (patch) | |
tree | 5d0444965ad3b5638cfc9d61ef550b9dbfbbc4b1 /lib/tasks | |
parent | 8a7bbe1ff3a384a21a837c8db82cc7c9613e3114 (diff) | |
download | gitlab-ce-b1378f05a11345f761cd31ae51aafc268003150a.tar.gz |
Add rake task to check integrity of uploaded files
Diffstat (limited to 'lib/tasks')
-rw-r--r-- | lib/tasks/gitlab/uploads.rake | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/tasks/gitlab/uploads.rake b/lib/tasks/gitlab/uploads.rake new file mode 100644 index 00000000000..fd5ef78103b --- /dev/null +++ b/lib/tasks/gitlab/uploads.rake @@ -0,0 +1,44 @@ +namespace :gitlab do + namespace :uploads do + desc 'GitLab | Uploads | Check integrity of uploaded files' + task check: :environment do + puts 'Starting checking integrity of uploaded files' + + uploads_batches do |batch| + batch.each do |upload| + puts "- Checking file (#{upload.id}): #{upload.absolute_path}".color(:green) + + if upload.exist? + check_checksum(upload) + else + puts " * File does not exist on the file system".color(:red) + end + end + end + + puts 'Done!' + end + + def batch_size + ENV.fetch('BATCH', 200).to_i + end + + def calculate_checksum(absolute_path) + Digest::SHA256.file(absolute_path).hexdigest + end + + def check_checksum(upload) + checksum = calculate_checksum(upload.absolute_path) + + if checksum != upload.checksum + puts " * File checksum (#{checksum}) does not match the one in the database (#{upload.checksum})".color(:red) + end + end + + def uploads_batches(&block) + Upload.all.in_batches(of: batch_size, start: ENV['ID_FROM'], finish: ENV['ID_TO']) do |relation| # rubocop: disable Cop/InBatches + yield relation + end + end + end +end |