Feature #5298 » 0001-Store-file-in-y-m-d-format-sub-dir-to-prevent-direct.patch
app/models/attachment.rb | ||
---|---|---|
15 | 15 |
# along with this program; if not, write to the Free Software |
16 | 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | 17 | |
18 |
require "digest/md5" |
|
18 |
require 'digest/md5' |
|
19 |
require 'fileutils' |
|
19 | 20 | |
20 | 21 |
class Attachment < ActiveRecord::Base |
21 | 22 |
belongs_to :container, :polymorphic => true |
... | ... | |
76 | 77 |
if @temp_file && (@temp_file.size > 0) |
77 | 78 |
logger.debug("saving '#{self.diskfile}'") |
78 | 79 |
md5 = Digest::MD5.new |
80 |
path = File.dirname diskfile |
|
81 |
FileUtils.mkdir_p(path) unless File.directory?(path) |
|
79 | 82 |
File.open(diskfile, "wb") do |f| |
80 | 83 |
buffer = "" |
81 | 84 |
while (buffer = @temp_file.read(8192)) |
... | ... | |
178 | 181 |
# keep the extension if any |
179 | 182 |
ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$} |
180 | 183 |
end |
181 |
while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}")) |
|
184 |
sub_path = "#{timestamp[0..3]}/#{timestamp[4..5]}" |
|
185 |
while File.exist?(File.join(@@storage_path, sub_path, "#{timestamp}_#{ascii}")) |
|
182 | 186 |
timestamp.succ! |
183 | 187 |
end |
184 |
"#{timestamp}_#{ascii}"
|
|
188 |
"#{sub_path}/#{timestamp}_#{filename}"
|
|
185 | 189 |
end |
186 | 190 |
end |