From 5a5168aa5e5da629f23a13dcbb3265bb17233328 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 28 Apr 2014 21:20:09 +0200 Subject: [PATCH] Improved splitting of file name. --- app/models/store.rb | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/models/store.rb b/app/models/store.rb index e63b14609..c73da9129 100644 --- a/app/models/store.rb +++ b/app/models/store.rb @@ -158,15 +158,17 @@ class Store::File < ApplicationModel def get_locaton # generate directory - base = Rails.root.to_s + "/storage/fs/" - path = self.md5.scan(/.../).join('/') - location = "#{ base }/#{path}" + base = Rails.root.to_s + '/storage/fs/' + parts = self.md5.scan(/.{1,3}/) + path = parts[ 1 .. 7 ].join('/') + '/' + file = parts[ 8 .. parts.count ].join('') + location = "#{base}/#{path}" # create directory if not exists if !File.exist?( location ) FileUtils.mkdir_p( location ) end - location += "/file" + location += file end # read file from fs @@ -233,6 +235,20 @@ class Store::File < ApplicationModel true end + # check database data and md5, in case fix it + def self.db_check_md5(fix_it = nil) + Store::File.where( :file_system => false ).each {|item| + md5 = Digest::MD5.hexdigest( item.data ) + if md5 != item.md5 + puts "DIFF: md5 diff of Store::File.find(#{item.id}) " + if fix_it + item.update_attribute( :md5, md5 ) + end + end + } + true + end + def self.move_to_fs Store::File.where( :file_system => false ).each {|item| item.write_to_fs