From 21fb2aa865555cbbfc8779178415ae7b6e3abb86 Mon Sep 17 00:00:00 2001
From: Karel Picman
Date: Fri, 2 Jun 2017 09:30:32 +0200
Subject: [PATCH] Make the storage path Rails.root related #733
---
CHANGELOG.md | 5 +++++
app/models/dmsf_file.rb | 10 ++++++----
app/models/dmsf_file_revision.rb | 6 +++---
app/views/settings/_dmsf_settings.html.erb | 10 +++++-----
db/migrate/07_dmsf_1_4_4.rb | 8 ++++----
init.rb | 4 ++--
6 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 893fce93..28347ec1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,11 @@ Changelog for Redmine DMSF
1.6.0 *????-??-??*
------------------
+IMPORTANT
+
+1. Files in the filesystem are re-organized by a new system based on date.
+2. DMS storage directory plugin option is related to the rails root directory.
+
1.5.9 *2016-03-01*
------------------
diff --git a/app/models/dmsf_file.rb b/app/models/dmsf_file.rb
index f12d4954..46679c7c 100644
--- a/app/models/dmsf_file.rb
+++ b/app/models/dmsf_file.rb
@@ -102,10 +102,12 @@ class DmsfFile < ActiveRecord::Base
def self.storage_path
return @@storage_path if @@storage_path.present?
path = Setting.plugin_redmine_dmsf['dmsf_storage_directory']
- path = Pathname(Redmine::Configuration['attachments_storage_path']).join('dmsf') if path.blank? && Redmine::Configuration['attachments_storage_path'].present?
- path = Rails.root.join('files/dmsf').to_s if path.blank?
- path.strip if path
- path
+ if path.blank?
+ path = 'dmsf'
+ else
+ path.strip!
+ end
+ Rails.root.join(path)
end
# Lets introduce a write for storage path, that way we can also
diff --git a/app/models/dmsf_file_revision.rb b/app/models/dmsf_file_revision.rb
index c97f4a60..dcf659b9 100644
--- a/app/models/dmsf_file_revision.rb
+++ b/app/models/dmsf_file_revision.rb
@@ -133,20 +133,20 @@ class DmsfFileRevision < ActiveRecord::Base
def storage_base_path
time = self.created_at || DateTime.now
path = time.strftime('%Y/%m')
- "#{DmsfFile.storage_path}/#{path}"
+ DmsfFile.storage_path.join path
end
def disk_file
path = self.storage_base_path
FileUtils.mkdir_p(path) unless File.exist?(path)
- "#{path}/#{self.disk_filename}"
+ path.join self.disk_filename
end
def new_storage_filename
raise DmsfAccessError, 'File id is not set' unless self.dmsf_file.id
filename = DmsfHelper.sanitize_filename(self.name)
timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
- while File.exist?(File.join(storage_base_path, "#{timestamp}_#{self.dmsf_file.id}_#{filename}"))
+ while File.exist?(storage_base_path.join("#{timestamp}_#{self.dmsf_file.id}_#{filename}"))
timestamp.succ!
end
"#{timestamp}_#{self.dmsf_file.id}_#{filename}"
diff --git a/app/views/settings/_dmsf_settings.html.erb b/app/views/settings/_dmsf_settings.html.erb
index 719c4f6d..f3834af1 100644
--- a/app/views/settings/_dmsf_settings.html.erb
+++ b/app/views/settings/_dmsf_settings.html.erb
@@ -62,21 +62,21 @@
<%= content_tag(:label, l(:label_file_storage_directory)) %>
<%
storage_dir = @settings['dmsf_storage_directory'].strip if @settings['dmsf_storage_directory'].present?
- storage_dir = "#{Rails.root}/files/dmsf" if storage_dir.blank?
+ storage_dir = 'dmsf' if storage_dir.blank?
%>
<%= text_field_tag 'settings[dmsf_storage_directory]', storage_dir, :size => 50 %>
- <%= l(:label_default) %>: <%= "#{Rails.root}/files/dmsf" %>
+ <%= l(:label_default) %>: dmsf
-<% unless File.exist?(storage_dir) %>
+<% unless File.exist?(DmsfFile.storage_path) %>
<% begin %>
- <% Dir.mkdir(storage_dir) %>
+ <% Dir.mkdir(DmsfFile.storage_path) %>
<% rescue %>
<%= l(:error_file_storage_directory_does_not_exist) %>
<% end %>
<% end %>
-<% testfilename = "#{storage_dir}/test.test" %>
+<% testfilename = DmsfFile.storage_path.join('test.test') %>
<% if File.exist?(storage_dir) %>
<% begin %>
<% File.open(testfilename, 'wb') do |file| %>
diff --git a/db/migrate/07_dmsf_1_4_4.rb b/db/migrate/07_dmsf_1_4_4.rb
index 5e6948d7..9abe7166 100644
--- a/db/migrate/07_dmsf_1_4_4.rb
+++ b/db/migrate/07_dmsf_1_4_4.rb
@@ -88,7 +88,7 @@ class Dmsf144 < ActiveRecord::Migration
begin
DmsfFileRevision.visible.each {|rev|
next if rev.project.nil?
- existing = "#{DmsfFile.storage_path}/#{rev.disk_filename}"
+ existing = DmsfFile.storage_path.join rev.disk_filename
new_path = rev.disk_file
begin
if File.exist?(existing)
@@ -142,13 +142,13 @@ class Dmsf144 < ActiveRecord::Migration
DmsfFileRevision.visible.each {|rev|
next if rev.project.nil?
project = rev.project.identifier.gsub(/[^\w\.\-]/,'_')
- existing = "#{DmsfFile.storage_path}/p_#{project}/#{rev.disk_filename}"
- new_path = "#{DmsfFile.storage_path}/#{rev.disk_filename}"
+ existing = DmsfFile.storage_path.jopin("p_#{project}/#{rev.disk_filename}")
+ new_path = DmsfFile.storage_path.join(rev.disk_filename)
if File.exist?(existing)
if File.exist?(new_path)
rev.disk_filename = rev.new_storage_filename
rev.save!
- new_path = "#{DmsfFile.storage_path}/#{rev.disk_filename}"
+ new_path = DmsfFile.storage_path.join(rev.disk_filename)
end
FileUtils.mv(existing, new_path)
end
diff --git a/init.rb b/init.rb
index e6eeb30b..ae580d2b 100644
--- a/init.rb
+++ b/init.rb
@@ -40,8 +40,8 @@ Redmine::Plugin.register :redmine_dmsf do
'dmsf_max_file_download' => '0',
'dmsf_max_email_filesize' => '0',
'dmsf_max_ajax_upload_filesize' => '100',
- 'dmsf_storage_directory' => Rails.root.join('files/dmsf').to_s,
- 'dmsf_index_database' => Rails.root.join('files/dmsf_index').to_s,
+ 'dmsf_storage_directory' => 'dmsf',
+ 'dmsf_index_database' => 'dmsf_index',
'dmsf_stemming_lang' => 'english',
'dmsf_stemming_strategy' => 'STEM_NONE',
'dmsf_webdav' => '1',