From 3d08c050ad8a9b857a1a6f11ea86a97936192caa Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Fri, 20 Jun 2014 22:40:18 +0400 Subject: [PATCH] #403: refactoring of file_store_clean --- app/models/build_script.rb | 2 +- app/models/concerns/file_store_clean.rb | 60 +-------------------- app/models/project.rb | 4 +- app/services/file_store_service.rb | 69 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 app/services/file_store_service.rb diff --git a/app/models/build_script.rb b/app/models/build_script.rb index 26cea8721..44e0c43c3 100644 --- a/app/models/build_script.rb +++ b/app/models/build_script.rb @@ -40,7 +40,7 @@ class BuildScript < ActiveRecord::Base old_sha1, new_commit = sha1, last_commit archive = project.archive_by_treeish_and_format(treeish, FORMAT) - new_sha1 = FileStoreClean.save_file_to_file_store(archive) + new_sha1 = FileStoreService::File.new(data: archive).save if new_sha1.present? update_attributes(sha1: new_sha1, commit: new_commit) destroy_files_from_file_store(old_sha1) if old_sha1.present? diff --git a/app/models/concerns/file_store_clean.rb b/app/models/concerns/file_store_clean.rb index 7568ce835..6a2f2ed08 100644 --- a/app/models/concerns/file_store_clean.rb +++ b/app/models/concerns/file_store_clean.rb @@ -14,17 +14,8 @@ module FileStoreClean def destroy_files_from_file_store(args = sha1_of_file_store_files) files = *args - token = FileStoreClean.file_store_authentication_token - uri = URI APP_CONFIG['file_store_url'] - Net::HTTP.start(uri.host, uri.port) do |http| - files.each do |sha1| - begin - req = Net::HTTP::Delete.new("/api/v1/file_stores/#{sha1}.json") - req.basic_auth token, '' - http.request(req) - rescue # Dont care about it - end - end + files.each do |sha1| + FileStoreService::File.new(sha1: sha1).destroy end end @@ -34,51 +25,4 @@ module FileStoreClean later :later_destroy_files_from_file_store, queue: :middle end - def self.file_store_authentication_token - User.find_by(uname: 'file_store').authentication_token - end - - # @param [Hash] data: - # - [String] path - path to file - # - [String] fullname - file name - def self.save_file_to_file_store(data) - sha1_hash = Digest::SHA1.hexdigest(File.read(data[:path])) - return sha1_hash if file_exist_on_file_store?(sha1_hash) - - begin - resource = RestClient::Resource.new( - "#{APP_CONFIG['file_store_url']}/api/v1/upload", - user: file_store_authentication_token - ) - - file = File.new(data[:path]) - # Hook for RestClient - # See: [RestClient::Payload#create_file_field](https://github.com/rest-client/rest-client/blob/master/lib/restclient/payload.rb#L202-L215) - file.define_singleton_method(:original_filename) { data[:fullname] } - resp = resource.post(file_store: { file: file }) - resp = JSON(resp) - rescue RestClient::UnprocessableEntity => e # 422, file already exist - return sha1_hash - rescue # Dont care about it - return nil - end - if resp.respond_to?(:[]) && resp['sha1_hash'].present? - resp['sha1_hash'] - else - nil - end - end - - def self.file_exist_on_file_store?(sha1) - begin - resp = JSON(RestClient.get "#{APP_CONFIG['file_store_url']}/api/v1/file_stores.json", params: {hash: sha1}) - rescue # Dont care about it - resp = [] - end - if resp[0].respond_to?('[]') && resp[0]['file_name'] && resp[0]['sha1_hash'] - true - else - false - end - end end diff --git a/app/models/project.rb b/app/models/project.rb index df7c33da3..7b9f1b987 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -240,10 +240,10 @@ class Project < ActiveRecord::Base format_id = ProjectTag::FORMATS["#{tag_file_format(format)}"] project_tag = project_tags.where(tag_name: tag.name, format_id: format_id).first - return project_tag.sha1 if project_tag && project_tag.commit_id == tag.commit.id && FileStoreClean.file_exist_on_file_store?(project_tag.sha1) + return project_tag.sha1 if project_tag && project_tag.commit_id == tag.commit.id && FileStoreService::File.new(sha1: project_tag.sha1).exist? archive = archive_by_treeish_and_format tag.name, format - sha1 = FileStoreClean.save_file_to_file_store(archive) + sha1 = FileStoreService::File.new(data: archive).save return nil if sha1.blank? if project_tag diff --git a/app/services/file_store_service.rb b/app/services/file_store_service.rb new file mode 100644 index 000000000..ba812a486 --- /dev/null +++ b/app/services/file_store_service.rb @@ -0,0 +1,69 @@ +module FileStoreService + class File + + URL = APP_CONFIG['file_store_url'] + + attr_accessor :sha1, :file + + # @param [String] sha1 + # @param [Hash] data: + # - [String] path - path to file + # - [String] fullname - file name + def initialize(sha1: nil, data: {}) + @sha1, @file = sha1, file + end + + def exist? + resp = JSON(RestClient.get "#{URL}/api/v1/file_stores.json", params: {hash: sha1}) + if resp[0].respond_to?('[]') && resp[0]['file_name'] && resp[0]['sha1_hash'] + true + else + false + end + rescue # Dont care about it + return false + end + + def save + sha1 = Digest::SHA1.hexdigest(File.read(data[:path])) + return sha1 if exist? + + resource = RestClient::Resource.new("#{URL}/api/v1/upload", user: token) + file = File.new(data[:path]) + # Hook for RestClient + # See: [RestClient::Payload#create_file_field](https://github.com/rest-client/rest-client/blob/master/lib/restclient/payload.rb#L202-L215) + file.define_singleton_method(:original_filename) { data[:fullname] } + resp = resource.post(file_store: { file: file }) + resp = JSON(resp) + + if resp.respond_to?(:[]) && resp['sha1_hash'].present? + resp['sha1_hash'] + else + nil + end + rescue RestClient::UnprocessableEntity => e # 422, file already exist + return sha1 + rescue # Dont care about it + return nil + end + + def destroy + uri = URI(URL) + Net::HTTP.start(uri.host, uri.port) do |http| + req = Net::HTTP::Delete.new("/api/v1/file_stores/#{sha1}.json") + req.basic_auth token, '' + http.request(req) + end + rescue # Dont care about it + end + + protected + + def token + Rails.cache.fetch([FileStoreService::File, :token], expires_in: 10.minutes) do + User.find_by(uname: 'file_store').authentication_token + end + end + + end +end \ No newline at end of file