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/
module ApplicationModel::ActivityStreamBase
class ApplicationModel
module ActivityStreamBase
=begin
@ -18,28 +19,28 @@ returns
=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 Setting.get('import_mode')
# return if we run import mode
return if Setting.get('import_mode')
# return if we run on init mode
return if !Setting.get('system_init_done')
# return if we run on init mode
return if !Setting.get('system_init_done')
role = self.class.activity_stream_support_config[:role]
updated_at = self.updated_at
if force
updated_at = Time.new
role = self.class.activity_stream_support_config[:role]
updated_at = self.updated_at
if force
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
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

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::Assets
class ApplicationModel
module Assets
=begin
@ -20,24 +21,24 @@ returns
=end
def assets (data = {})
def assets (data = {})
if !data[ self.class.to_app_model ]
data[ self.class.to_app_model ] = {}
end
if !data[ self.class.to_app_model ][ self.id ]
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 )
if !data[ self.class.to_app_model ]
data[ self.class.to_app_model ] = {}
end
if !data[ self.class.to_app_model ][ self.id ]
data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations
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

View file

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

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HistoryLogBase
class ApplicationModel
module HistoryLogBase
=begin
@ -15,15 +16,15 @@ returns
=end
def history_log (type, user_id, data = {})
data[:o_id] = self['id']
data[:history_type] = type
data[:history_object] = self.class.name
data[:related_o_id] = nil
data[:related_history_object] = nil
data[:created_by_id] = user_id
History.add(data)
end
def history_log (type, user_id, data = {})
data[:o_id] = self['id']
data[:history_type] = type
data[:history_object] = self.class.name
data[:related_o_id] = nil
data[:related_history_object] = nil
data[:created_by_id] = user_id
History.add(data)
end
=begin
@ -74,27 +75,27 @@ returns
=end
def history_get(fulldata = false)
if !fulldata
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] )
def history_get(fulldata = false)
if !fulldata
return History.list( self.class.name, self['id'] )
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

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::SearchIndexBase
class ApplicationModel
module SearchIndexBase
=begin
@ -15,47 +16,47 @@ returns
=end
def search_index_update_backend
return if !self.class.search_index_support_config
def search_index_update_backend
return if !self.class.search_index_support_config
# default ignored attributes
ignore_attributes = {
created_by_id: true,
updated_by_id: 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
# default ignored attributes
ignore_attributes = {
created_by_id: true,
updated_by_id: true,
active: true,
}
end
# for performance reasons, Model.search_index_reload will only collect if of object
# get whole data here
data = self.class.find(self.id)
# remove ignored attributes
attributes = data.attributes
ignore_attributes.each {|key, value|
next if value != true
attributes.delete( key.to_s )
}
# fill up with search data
attributes = search_index_attribute_lookup(attributes, data)
return if !attributes
# update backend
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 )
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
# for performance reasons, Model.search_index_reload will only collect if of object
# get whole data here
data = self.class.find(self.id)
# remove ignored attributes
attributes = data.attributes
ignore_attributes.each {|key, value|
next if value != true
attributes.delete( key.to_s )
}
# fill up with search data
attributes = search_index_attribute_lookup(attributes, data)
return if !attributes
# update backend
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
else
SearchIndexBackend.add( self.class.to_s, attributes )
end
end
=begin
@ -70,16 +71,16 @@ returns
=end
def search_index_data
attributes = {}
['name', 'note'].each { |key|
if self[key] && !self[key].empty?
attributes[key] = self[key]
end
}
return if attributes.empty?
attributes
end
def search_index_data
attributes = {}
['name', 'note'].each { |key|
if self[key] && !self[key].empty?
attributes[key] = self[key]
end
}
return if attributes.empty?
attributes
end
private
@ -95,51 +96,51 @@ returns
=end
def search_index_attribute_lookup(attributes, ref_object)
def search_index_attribute_lookup(attributes, ref_object)
# default keep attributes
keep_attributes = {}
if self.class.search_index_support_config[:keep_attributes]
self.class.search_index_support_config[:keep_attributes].each {|key, value|
keep_attributes[key] = value
# default keep attributes
keep_attributes = {}
if self.class.search_index_support_config[:keep_attributes]
self.class.search_index_support_config[:keep_attributes].each {|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
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

View file

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

View file

@ -1,6 +1,7 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
module Organization::Assets
class Organization
module Assets
=begin
@ -20,33 +21,33 @@ returns
=end
def assets (data)
def assets (data)
if !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
}
if !data[ Organization.to_app_model ]
data[ Organization.to_app_model ] = {}
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 )
if !data[ User.to_app_model ]
data[ User.to_app_model ] = {}
end
}
data
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
['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

View file

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

View file

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

View file

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

View file

@ -1,90 +1,92 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Store::File < ApplicationModel
include ApplicationLib
after_destroy :destroy_provider
class Store
class File < ApplicationModel
include ApplicationLib
after_destroy :destroy_provider
# add new file
def self.add(data)
sha = Digest::SHA256.hexdigest( data )
# add new file
def self.add(data)
sha = Digest::SHA256.hexdigest( data )
file = Store::File.where( sha: sha ).first
if file == nil
file = Store::File.where( sha: sha ).first
if file == nil
# load backend based on config
adapter_name = Setting.get('storage_provider') || 'DB'
if !adapter_name
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 )
# load backend based on config
adapter_name = Setting.get('storage_provider') || 'DB'
if !adapter_name
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
}
success
end
file
end
# move file from one to other provider
# 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 }")
# 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
Store::File.all.each {|item|
next if item.provider == target
content = item.content
# 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
}
success
end
# add to new provider
adapter_target.add( content, item.sha )
# move file from one to other provider
# 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
item.update_attribute( :provider, target )
Store::File.all.each {|item|
next if item.provider == target
content = item.content
# remove from old provider
adapter_source.delete( item.sha )
# add to new provider
adapter_target.add( content, item.sha )
puts "NOTICE: Moved file #{item.sha} from #{source} to #{target}"
}
true
end
# update meta data
item.update_attribute( :provider, target )
private
# remove from old provider
adapter_source.delete( item.sha )
def destroy_provider
adapter = self.class.load_adapter("Store::Provider::#{ self.provider }")
adapter.delete( self.sha )
puts "NOTICE: Moved file #{item.sha} from #{source} to #{target}"
}
true
end
private
def destroy_provider
adapter = self.class.load_adapter("Store::Provider::#{ self.provider }")
adapter.delete( self.sha )
end
end
end

View file

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

View file

@ -1,25 +1,28 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Store::Provider::DB < ApplicationModel
self.table_name = 'store_provider_dbs'
class Store
class Provider
class DB < ApplicationModel
self.table_name = 'store_provider_dbs'
def self.add(data, sha)
Store::Provider::DB.create(
data: data,
sha: sha,
)
true
def self.add(data, sha)
Store::Provider::DB.create(
data: data,
sha: sha,
)
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
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

View file

@ -1,84 +1,87 @@
# 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)
write_to_fs(data, sha)
true
end
def self.add(data, sha)
write_to_fs(data, sha)
true
end
def self.get(sha)
read_from_fs(sha)
end
def self.get(sha)
read_from_fs(sha)
end
def self.delete(sha)
unlink_from_fs(sha)
end
def self.delete(sha)
unlink_from_fs(sha)
end
private
private
# generate file location
def self.get_locaton(sha)
# generate file location
def self.get_locaton(sha)
# generate directory
base = Rails.root.to_s + '/storage/fs/'
parts = sha.scan(/.{1,4}/)
path = parts[ 1 .. 10 ].join('/') + '/'
file = parts[ 11 .. parts.count ].join('')
location = "#{base}/#{path}"
# generate directory
base = Rails.root.to_s + '/storage/fs/'
parts = sha.scan(/.{1,4}/)
path = parts[ 1 .. 10 ].join('/') + '/'
file = parts[ 11 .. parts.count ].join('')
location = "#{base}/#{path}"
# create directory if not exists
if !File.exist?( location )
FileUtils.mkdir_p( location )
end
location += file
end
# create directory if not exists
if !File.exist?( location )
FileUtils.mkdir_p( location )
end
location += file
end
# unlink file from fs
def self.unlink_from_fs(sha)
if File.exist?( get_locaton(sha) )
puts "NOTICE: storge remove '#{ get_locaton(sha) }'"
File.delete( get_locaton(sha) )
# unlink file from fs
def self.unlink_from_fs(sha)
if File.exist?( get_locaton(sha) )
puts "NOTICE: storge remove '#{ 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
# 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

View file

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

View file

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