#403: refactoring of file_store_clean

This commit is contained in:
Vokhmin Alexey V 2014-06-20 22:40:18 +04:00
parent 4a40764173
commit 3d08c050ad
4 changed files with 74 additions and 61 deletions

View File

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

View File

@ -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
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

View File

@ -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

View File

@ -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