Project

General

Profile

Feature #5298 » 0001-Store-file-in-y-m-d-format-sub-dir-to-prevent-direct.patch

Istvan DEMETER, 2010-04-13 01:22

View differences:

app/models/attachment.rb
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require "digest/md5"
require 'digest/md5'
require 'fileutils'
class Attachment < ActiveRecord::Base
belongs_to :container, :polymorphic => true
......
if @temp_file && (@temp_file.size > 0)
logger.debug("saving '#{self.diskfile}'")
md5 = Digest::MD5.new
path = File.dirname diskfile
FileUtils.mkdir_p(path) unless File.directory?(path)
File.open(diskfile, "wb") do |f|
buffer = ""
while (buffer = @temp_file.read(8192))
......
# keep the extension if any
ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
end
while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}"))
sub_path = "#{timestamp[0..3]}/#{timestamp[4..5]}"
while File.exist?(File.join(@@storage_path, sub_path, "#{timestamp}_#{ascii}"))
timestamp.succ!
end
"#{timestamp}_#{ascii}"
"#{sub_path}/#{timestamp}_#{filename}"
end
end
(1-1/3)