Some new rubocop checks.

This commit is contained in:
Martin Edenhofer 2015-04-27 23:44:41 +02:00
parent a2d49cefc5
commit bd29c71687
16 changed files with 482 additions and 460 deletions

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::ActivityStreamBase class ApplicationModel
module ActivityStreamBase
=begin =begin
@ -18,28 +19,28 @@ returns
=end =end
def activity_stream_log (type, user_id, force = false) def activity_stream_log (type, user_id, force = false)
# return if we run import mode # return if we run import mode
return if Setting.get('import_mode') return if Setting.get('import_mode')
# return if we run on init mode # return if we run on init mode
return if !Setting.get('system_init_done') return if !Setting.get('system_init_done')
role = self.class.activity_stream_support_config[:role] role = self.class.activity_stream_support_config[:role]
updated_at = self.updated_at updated_at = self.updated_at
if force if force
updated_at = Time.new updated_at = Time.new
end
ActivityStream.add(
o_id: self['id'],
type: type,
object: self.class.name,
group_id: self['group_id'],
role: role,
created_at: updated_at,
created_by_id: user_id,
)
end end
ActivityStream.add(
o_id: self['id'],
type: type,
object: self.class.name,
group_id: self['group_id'],
role: role,
created_at: updated_at,
created_by_id: user_id,
)
end end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::Assets class ApplicationModel
module Assets
=begin =begin
@ -20,24 +21,24 @@ returns
=end =end
def assets (data = {}) def assets (data = {})
if !data[ self.class.to_app_model ] if !data[ self.class.to_app_model ]
data[ self.class.to_app_model ] = {} data[ self.class.to_app_model ] = {}
end end
if !data[ self.class.to_app_model ][ self.id ] if !data[ self.class.to_app_model ][ self.id ]
data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations
end
return data if !self['created_by_id'] && !self['updated_by_id']
['created_by_id', 'updated_by_id'].each {|item|
next if !self[ item ]
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
user = User.lookup( id: self[ item ] )
data = user.assets( data )
end end
}
data
end
return data if !self['created_by_id'] && !self['updated_by_id']
['created_by_id', 'updated_by_id'].each {|item|
next if !self[ item ]
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
user = User.lookup( id: self[ item ] )
data = user.assets( data )
end
}
data
end
end
end end

View file

@ -1,11 +1,13 @@
# perform background job # perform background job
class ApplicationModel::BackgroundJobSearchIndex class ApplicationModel
def initialize(object, o_id) class BackgroundJobSearchIndex
@object = object def initialize(object, o_id)
@o_id = o_id @object = object
end @o_id = o_id
end
def perform def perform
Object.const_get(@object).find(@o_id).search_index_update_backend Object.const_get(@object).find(@o_id).search_index_update_backend
end
end end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HistoryLogBase class ApplicationModel
module HistoryLogBase
=begin =begin
@ -15,15 +16,15 @@ returns
=end =end
def history_log (type, user_id, data = {}) def history_log (type, user_id, data = {})
data[:o_id] = self['id'] data[:o_id] = self['id']
data[:history_type] = type data[:history_type] = type
data[:history_object] = self.class.name data[:history_object] = self.class.name
data[:related_o_id] = nil data[:related_o_id] = nil
data[:related_history_object] = nil data[:related_history_object] = nil
data[:created_by_id] = user_id data[:created_by_id] = user_id
History.add(data) History.add(data)
end end
=begin =begin
@ -74,27 +75,27 @@ returns
=end =end
def history_get(fulldata = false) def history_get(fulldata = false)
if !fulldata if !fulldata
return History.list( self.class.name, self['id'] ) return History.list( self.class.name, self['id'] )
end
# get related objects
history = History.list( self.class.name, self['id'], nil, true )
history[:list].each {|item|
record = Kernel.const_get( item['object'] ).find( item['o_id'] )
history[:assets] = record.assets( history[:assets] )
if item['related_object']
record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
history[:assets] = record.assets( history[:assets] )
end end
}
return {
history: history[:list],
assets: history[:assets],
}
end
# get related objects
history = History.list( self.class.name, self['id'], nil, true )
history[:list].each {|item|
record = Kernel.const_get( item['object'] ).find( item['o_id'] )
history[:assets] = record.assets( history[:assets] )
if item['related_object']
record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
history[:assets] = record.assets( history[:assets] )
end
}
{
history: history[:list],
assets: history[:assets],
}
end
end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::SearchIndexBase class ApplicationModel
module SearchIndexBase
=begin =begin
@ -15,47 +16,47 @@ returns
=end =end
def search_index_update_backend def search_index_update_backend
return if !self.class.search_index_support_config return if !self.class.search_index_support_config
# default ignored attributes # default ignored attributes
ignore_attributes = { ignore_attributes = {
created_by_id: true, created_by_id: true,
updated_by_id: true, updated_by_id: true,
active: true, active: true,
}
if self.class.search_index_support_config[:ignore_attributes]
self.class.search_index_support_config[:ignore_attributes].each {|key, value|
ignore_attributes[key] = value
} }
end if self.class.search_index_support_config[:ignore_attributes]
self.class.search_index_support_config[:ignore_attributes].each {|key, value|
# for performance reasons, Model.search_index_reload will only collect if of object ignore_attributes[key] = value
# get whole data here }
data = self.class.find(self.id) end
# remove ignored attributes # for performance reasons, Model.search_index_reload will only collect if of object
attributes = data.attributes # get whole data here
ignore_attributes.each {|key, value| data = self.class.find(self.id)
next if value != true
attributes.delete( key.to_s ) # remove ignored attributes
} attributes = data.attributes
ignore_attributes.each {|key, value|
# fill up with search data next if value != true
attributes = search_index_attribute_lookup(attributes, data) attributes.delete( key.to_s )
return if !attributes }
# update backend # fill up with search data
if self.class.column_names.include? 'active' attributes = search_index_attribute_lookup(attributes, data)
if self.active return if !attributes
SearchIndexBackend.add( self.class.to_s, attributes )
else # update backend
SearchIndexBackend.remove( self.class.to_s, self.id ) if self.class.column_names.include? 'active'
if self.active
SearchIndexBackend.add( self.class.to_s, attributes )
else
SearchIndexBackend.remove( self.class.to_s, self.id )
end
else
SearchIndexBackend.add( self.class.to_s, attributes )
end end
else
SearchIndexBackend.add( self.class.to_s, attributes )
end end
end
=begin =begin
@ -70,16 +71,16 @@ returns
=end =end
def search_index_data def search_index_data
attributes = {} attributes = {}
['name', 'note'].each { |key| ['name', 'note'].each { |key|
if self[key] && !self[key].empty? if self[key] && !self[key].empty?
attributes[key] = self[key] attributes[key] = self[key]
end end
} }
return if attributes.empty? return if attributes.empty?
attributes attributes
end end
private private
@ -95,51 +96,51 @@ returns
=end =end
def search_index_attribute_lookup(attributes, ref_object) def search_index_attribute_lookup(attributes, ref_object)
# default keep attributes # default keep attributes
keep_attributes = {} keep_attributes = {}
if self.class.search_index_support_config[:keep_attributes] if self.class.search_index_support_config[:keep_attributes]
self.class.search_index_support_config[:keep_attributes].each {|key, value| self.class.search_index_support_config[:keep_attributes].each {|key, value|
keep_attributes[key] = value keep_attributes[key] = value
}
end
attributes_new = {}
attributes.each {|key, value|
next if !value
# get attribute name
attribute_name_with_id = key.to_s
attribute_name = key.to_s
next if attribute_name[-3, 3] != '_id'
attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
# check if attribute method exists
next if !ref_object.respond_to?( attribute_name )
# check if method has own class
relation_class = ref_object.send(attribute_name).class
next if !relation_class
# lookup ref object
relation_model = relation_class.lookup( id: value )
next if !relation_model
# get name of ref object
value = nil
if relation_model.respond_to?('search_index_data')
value = relation_model.send('search_index_data')
end
next if !value
# save name of ref object
attributes_new[ attribute_name ] = value
if !keep_attributes[ attribute_name_with_id.to_sym ]
attributes.delete(key)
end
} }
attributes_new.merge(attributes)
end end
attributes_new = {}
attributes.each {|key, value|
next if !value
# get attribute name
attribute_name_with_id = key.to_s
attribute_name = key.to_s
next if attribute_name[-3, 3] != '_id'
attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
# check if attribute method exists
next if !ref_object.respond_to?( attribute_name )
# check if method has own class
relation_class = ref_object.send(attribute_name).class
next if !relation_class
# lookup ref object
relation_model = relation_class.lookup( id: value )
next if !relation_model
# get name of ref object
value = nil
if relation_model.respond_to?('search_index_data')
value = relation_model.send('search_index_data')
end
next if !value
# save name of ref object
attributes_new[ attribute_name ] = value
if !keep_attributes[ attribute_name_with_id.to_sym ]
attributes.delete(key)
end
}
attributes_new.merge(attributes)
end end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module History::Assets class History
module Assets
=begin =begin
@ -20,14 +21,14 @@ returns
=end =end
def assets (data) def assets (data)
if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ] if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ]
user = User.lookup( id: self['created_by_id'] ) user = User.lookup( id: self['created_by_id'] )
data = user.assets( data ) data = user.assets( data )
end
data
end end
data
end end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module Organization::Assets class Organization
module Assets
=begin =begin
@ -20,33 +21,33 @@ returns
=end =end
def assets (data) def assets (data)
if !data[ Organization.to_app_model ] if !data[ Organization.to_app_model ]
data[ Organization.to_app_model ] = {} data[ Organization.to_app_model ] = {}
end
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
if !data[ Organization.to_app_model ][ self.id ]
data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations
if data[ Organization.to_app_model ][ self.id ]['member_ids']
data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id|
if !data[ User.to_app_model ][ user_id ]
user = User.lookup( id: user_id )
data = user.assets( data )
end
}
end end
end if !data[ User.to_app_model ]
['created_by_id', 'updated_by_id'].each {|item| data[ User.to_app_model ] = {}
next if !self[ item ]
if !data[ User.to_app_model ][ self[ item ] ]
user = User.lookup( id: self[ item ] )
data = user.assets( data )
end end
} if !data[ Organization.to_app_model ][ self.id ]
data data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations
if data[ Organization.to_app_model ][ self.id ]['member_ids']
data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id|
if !data[ User.to_app_model ][ user_id ]
user = User.lookup( id: user_id )
data = user.assets( data )
end
}
end
end
['created_by_id', 'updated_by_id'].each {|item|
next if !self[ item ]
if !data[ User.to_app_model ][ self[ item ] ]
user = User.lookup( id: self[ item ] )
data = user.assets( data )
end
}
data
end
end end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module Organization::Permission class Organization
module Permission
=begin =begin
@ -15,24 +16,24 @@ returns
=end =end
def permission (data) def permission (data)
# check customer # check customer
if data[:current_user].is_role('Customer') if data[:current_user].is_role('Customer')
# access ok if its own organization # access ok if its own organization
return false if data[:type] != 'ro' return false if data[:type] != 'ro'
return false if !data[:current_user].organization_id return false if !data[:current_user].organization_id
return true if self.id == data[:current_user].organization_id return true if self.id == data[:current_user].organization_id
# no access # no access
return false return false
end
# check agent
return true if data[:current_user].is_role(Z_ROLENAME_ADMIN)
return true if data[:current_user].is_role('Agent')
false
end end
# check agent
return true if data[:current_user].is_role(Z_ROLENAME_ADMIN)
return true if data[:current_user].is_role('Agent')
return false
end end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module Organization::Search class Organization
module Search
=begin =begin
@ -18,52 +19,53 @@ returns
=end =end
def search(params) def search(params)
# get params # get params
query = params[:query] query = params[:query]
limit = params[:limit] || 10 limit = params[:limit] || 10
current_user = params[:current_user] current_user = params[:current_user]
# enable search only for agents and admins # enable search only for agents and admins
return [] if !current_user.is_role('Agent') && !current_user.is_role(Z_ROLENAME_ADMIN) return [] if !current_user.is_role('Agent') && !current_user.is_role(Z_ROLENAME_ADMIN)
# try search index backend # try search index backend
if SearchIndexBackend.enabled? if SearchIndexBackend.enabled?
items = SearchIndexBackend.search( query, limit, 'Organization' ) items = SearchIndexBackend.search( query, limit, 'Organization' )
organizations = [] organizations = []
items.each { |item| items.each { |item|
organizations.push Organization.lookup( id: item[:id] ) organizations.push Organization.lookup( id: item[:id] )
} }
return organizations return organizations
end end
# fallback do sql query # fallback do sql query
# - stip out * we already search for *query* - # - stip out * we already search for *query* -
query.gsub! '*', '' query.gsub! '*', ''
organizations = Organization.where( organizations = Organization.where(
'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%" 'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
).order('name').limit(limit) ).order('name').limit(limit)
# if only a few organizations are found, search for names of users # if only a few organizations are found, search for names of users
if organizations.length <= 3 if organizations.length <= 3
organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where( organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where(
'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%" 'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"
).order('organizations.name').limit(limit) ).order('organizations.name').limit(limit)
organizations_by_user.each {|organization_by_user| organizations_by_user.each {|organization_by_user|
organization_exists = false organization_exists = false
organizations.each {|organization| organizations.each {|organization|
if organization.id == organization_by_user.id if organization.id == organization_by_user.id
organization_exists = true organization_exists = true
end
}
# get model with full data
if !organization_exists
organizations.push Organization.find(organization_by_user)
end end
} }
end
# get model with full data organizations
if !organization_exists
organizations.push Organization.find(organization_by_user)
end
}
end end
organizations
end end
end end

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module Organization::SearchIndex class Organization
module SearchIndex
=begin =begin
@ -14,47 +15,47 @@ returns
=end =end
def search_index_attribute_lookup(attributes, ref_object) def search_index_attribute_lookup(attributes, ref_object)
attributes_new = {} attributes_new = {}
attributes.each {|key, value| attributes.each {|key, value|
next if !value next if !value
# get attribute name # get attribute name
attribute_name = key.to_s attribute_name = key.to_s
next if attribute_name[-3, 3] != '_id' next if attribute_name[-3, 3] != '_id'
attribute_name = attribute_name[ 0, attribute_name.length - 3 ] attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
# check if attribute method exists # check if attribute method exists
next if !ref_object.respond_to?( attribute_name ) next if !ref_object.respond_to?( attribute_name )
# check if method has own class # check if method has own class
relation_class = ref_object.send(attribute_name).class relation_class = ref_object.send(attribute_name).class
next if !relation_class next if !relation_class
# lookup ref object # lookup ref object
relation_model = relation_class.lookup( id: value ) relation_model = relation_class.lookup( id: value )
next if !relation_model next if !relation_model
# get name of ref object # get name of ref object
value = nil value = nil
if relation_model.respond_to?('search_index_data') if relation_model.respond_to?('search_index_data')
value = relation_model.send('search_index_data') value = relation_model.send('search_index_data')
end end
next if !value next if !value
# save name of ref object # save name of ref object
attributes_new[ attribute_name ] = value attributes_new[ attribute_name ] = value
attributes.delete(key) attributes.delete(key)
} }
# add org member for search index data # add org member for search index data
attributes['member'] = [] attributes['member'] = []
users = User.where( organization_id: self.id ) users = User.where( organization_id: self.id )
users.each { |user| users.each { |user|
attributes['member'].push user.search_index_data attributes['member'].push user.search_index_data
} }
attributes_new.merge(attributes) attributes_new.merge(attributes)
end
end end
end end

View file

@ -1,90 +1,92 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Store::File < ApplicationModel class Store
include ApplicationLib class File < ApplicationModel
after_destroy :destroy_provider include ApplicationLib
after_destroy :destroy_provider
# add new file # add new file
def self.add(data) def self.add(data)
sha = Digest::SHA256.hexdigest( data ) sha = Digest::SHA256.hexdigest( data )
file = Store::File.where( sha: sha ).first file = Store::File.where( sha: sha ).first
if file == nil if file == nil
# load backend based on config # load backend based on config
adapter_name = Setting.get('storage_provider') || 'DB' adapter_name = Setting.get('storage_provider') || 'DB'
if !adapter_name if !adapter_name
raise 'Missing storage_provider setting option' raise 'Missing storage_provider setting option'
end
adapter = self.load_adapter( "Store::Provider::#{ adapter_name }" )
adapter.add( data, sha )
file = Store::File.create(
provider: adapter_name,
sha: sha,
)
end
file
end
# read content
def content
adapter = self.class.load_adapter("Store::Provider::#{ self.provider }")
if self.sha
c = adapter.get( self.sha )
else
# fallback until migration is done
c = Store::Provider::DB.where( md5: self.md5 ).first.data
end
c
end
# check data and sha, in case fix it
def self.verify(fix_it = nil)
success = true
Store::File.all.each {|item|
content = item.content
sha = Digest::SHA256.hexdigest( content )
puts "CHECK: Store::File.find(#{item.id}) "
if sha != item.sha
success = false
puts "DIFF: sha diff of Store::File.find(#{item.id}) "
if fix_it
item.update_attribute( :sha, sha )
end end
adapter = self.load_adapter( "Store::Provider::#{ adapter_name }" )
adapter.add( data, sha )
file = Store::File.create(
provider: adapter_name,
sha: sha,
)
end end
} file
success end
end
# move file from one to other provider # read content
# e. g. Store::File.move('File', 'DB') def content
# e. g. Store::File.move('DB', 'File') adapter = self.class.load_adapter("Store::Provider::#{ self.provider }")
def self.move(source, target) if self.sha
adapter_source = load_adapter("Store::Provider::#{ source }") c = adapter.get( self.sha )
adapter_target = load_adapter("Store::Provider::#{ target }") else
# fallback until migration is done
c = Store::Provider::DB.where( md5: self.md5 ).first.data
end
c
end
Store::File.all.each {|item| # check data and sha, in case fix it
next if item.provider == target def self.verify(fix_it = nil)
content = item.content success = true
Store::File.all.each {|item|
content = item.content
sha = Digest::SHA256.hexdigest( content )
puts "CHECK: Store::File.find(#{item.id}) "
if sha != item.sha
success = false
puts "DIFF: sha diff of Store::File.find(#{item.id}) "
if fix_it
item.update_attribute( :sha, sha )
end
end
}
success
end
# add to new provider # move file from one to other provider
adapter_target.add( content, item.sha ) # e. g. Store::File.move('File', 'DB')
# e. g. Store::File.move('DB', 'File')
def self.move(source, target)
adapter_source = load_adapter("Store::Provider::#{ source }")
adapter_target = load_adapter("Store::Provider::#{ target }")
# update meta data Store::File.all.each {|item|
item.update_attribute( :provider, target ) next if item.provider == target
content = item.content
# remove from old provider # add to new provider
adapter_source.delete( item.sha ) adapter_target.add( content, item.sha )
puts "NOTICE: Moved file #{item.sha} from #{source} to #{target}" # update meta data
} item.update_attribute( :provider, target )
true
end
private # remove from old provider
adapter_source.delete( item.sha )
def destroy_provider puts "NOTICE: Moved file #{item.sha} from #{source} to #{target}"
adapter = self.class.load_adapter("Store::Provider::#{ self.provider }") }
adapter.delete( self.sha ) true
end
private
def destroy_provider
adapter = self.class.load_adapter("Store::Provider::#{ self.provider }")
adapter.delete( self.sha )
end
end end
end end

View file

@ -1,5 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Store::Object < ApplicationModel class Store
validates :name, presence: true class Object < ApplicationModel
validates :name, presence: true
end
end end

View file

@ -1,25 +1,28 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Store::Provider::DB < ApplicationModel class Store
self.table_name = 'store_provider_dbs' class Provider
class DB < ApplicationModel
self.table_name = 'store_provider_dbs'
def self.add(data, sha) def self.add(data, sha)
Store::Provider::DB.create( Store::Provider::DB.create(
data: data, data: data,
sha: sha, sha: sha,
) )
true true
end
def self.get(sha)
file = Store::Provider::DB.where( sha: sha ).first
return if !file
file.data
end
def self.delete(sha)
Store::Provider::DB.where( sha: sha ).destroy_all
true
end
end
end end
def self.get(sha)
file = Store::Provider::DB.where( sha: sha ).first
return if !file
file.data
end
def self.delete(sha)
Store::Provider::DB.where( sha: sha ).destroy_all
true
end
end end

View file

@ -1,84 +1,87 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Store::Provider::File class Store
class Provider
class File
def self.add(data, sha) def self.add(data, sha)
write_to_fs(data, sha) write_to_fs(data, sha)
true true
end end
def self.get(sha) def self.get(sha)
read_from_fs(sha) read_from_fs(sha)
end end
def self.delete(sha) def self.delete(sha)
unlink_from_fs(sha) unlink_from_fs(sha)
end end
private private
# generate file location # generate file location
def self.get_locaton(sha) def self.get_locaton(sha)
# generate directory # generate directory
base = Rails.root.to_s + '/storage/fs/' base = Rails.root.to_s + '/storage/fs/'
parts = sha.scan(/.{1,4}/) parts = sha.scan(/.{1,4}/)
path = parts[ 1 .. 10 ].join('/') + '/' path = parts[ 1 .. 10 ].join('/') + '/'
file = parts[ 11 .. parts.count ].join('') file = parts[ 11 .. parts.count ].join('')
location = "#{base}/#{path}" location = "#{base}/#{path}"
# create directory if not exists # create directory if not exists
if !File.exist?( location ) if !File.exist?( location )
FileUtils.mkdir_p( location ) FileUtils.mkdir_p( location )
end end
location += file location += file
end end
# unlink file from fs # unlink file from fs
def self.unlink_from_fs(sha) def self.unlink_from_fs(sha)
if File.exist?( get_locaton(sha) ) if File.exist?( get_locaton(sha) )
puts "NOTICE: storge remove '#{ get_locaton(sha) }'" puts "NOTICE: storge remove '#{ get_locaton(sha) }'"
File.delete( get_locaton(sha) ) File.delete( get_locaton(sha) )
end
end
# read file from fs
def self.read_from_fs(sha)
puts "read from fs #{ get_locaton(sha) }"
if !File.exist?( get_locaton(sha) )
raise "ERROR: No such file #{ get_locaton(sha) }"
end
data = File.open( get_locaton(sha), 'rb' )
content = data.read
# check sha
local_sha = Digest::SHA256.hexdigest( content )
if local_sha != sha
raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}"
end
content
end
# write file to fs
def self.write_to_fs(data, sha)
# install file
permission = '600'
if !File.exist?( get_locaton(sha) )
puts "NOTICE: storge write '#{ get_locaton(sha) }' (#{permission})"
file = File.new( get_locaton(sha), 'wb' )
file.write( data )
file.close
end
File.chmod( permission.to_i(8), get_locaton(sha) )
# check sha
local_sha = Digest::SHA256.hexdigest( read_from_fs(sha) )
if sha != local_sha
raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}"
end
true
end
end end
end end
# read file from fs
def self.read_from_fs(sha)
puts "read from fs #{ get_locaton(sha) }"
if !File.exist?( get_locaton(sha) )
raise "ERROR: No such file #{ get_locaton(sha) }"
end
data = File.open( get_locaton(sha), 'rb' )
content = data.read
# check sha
local_sha = Digest::SHA256.hexdigest( content )
if local_sha != sha
raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}"
end
content
end
# write file to fs
def self.write_to_fs(data, sha)
# install file
permission = '600'
if !File.exist?( get_locaton(sha) )
puts "NOTICE: storge write '#{ get_locaton(sha) }' (#{permission})"
file = File.new( get_locaton(sha), 'wb' )
file.write( data )
file.close
end
File.chmod( permission.to_i(8), get_locaton(sha) )
# check sha
local_sha = Digest::SHA256.hexdigest( read_from_fs(sha) )
if sha != local_sha
raise "ERROR: Corrupt file in fs #{ get_locaton(sha) }, sha should be #{sha} but is #{local_sha}"
end
true
end
end end

View file

@ -319,7 +319,7 @@ returns
return check_time if !escalation_time return check_time if !escalation_time
return escalation_time if !check_time return escalation_time if !check_time
return check_time if escalation_time > check_time return check_time if escalation_time > check_time
return escalation_time escalation_time
end end
end end
end end

View file

@ -73,7 +73,7 @@ returns
assets = record.assets(assets) assets = record.assets(assets)
end end
} }
return { {
history: list, history: list,
assets: assets, assets: assets,
} }