From 95a30d4d0bdb8be57e32471c13fba5b5471a6103 Mon Sep 17 00:00:00 2001 From: Karel Picman Date: Fri, 21 Jul 2017 09:02:29 +0200 Subject: [PATCH] #48 revisions without a physical file --- app/controllers/dmsf_files_controller.rb | 2 +- app/helpers/dmsf_upload_helper.rb | 4 ++-- app/models/dmsf_file.rb | 2 +- app/models/dmsf_file_revision.rb | 15 +++++++++++++-- db/migrate/07_dmsf_1_4_4.rb | 4 ++-- db/migrate/20170418104901_migrate_documents.rb | 4 ++-- lib/redmine_dmsf/webdav/dmsf_resource.rb | 2 +- lib/tasks/dmsf_convert_documents.rake | 2 +- 8 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/controllers/dmsf_files_controller.rb b/app/controllers/dmsf_files_controller.rb index 2684c3fa..ae9e955d 100644 --- a/app/controllers/dmsf_files_controller.rb +++ b/app/controllers/dmsf_files_controller.rb @@ -151,7 +151,7 @@ class DmsfFilesController < ApplicationController if revision.save revision.assign_workflow(params[:dmsf_workflow_id]) if upload - FileUtils.mv(upload.tempfile_path, revision.disk_file) + FileUtils.mv(upload.tempfile_path, revision.disk_file(false)) end if @file.locked? && !@file.locks.empty? begin diff --git a/app/helpers/dmsf_upload_helper.rb b/app/helpers/dmsf_upload_helper.rb index a595d93d..6a900c42 100644 --- a/app/helpers/dmsf_upload_helper.rb +++ b/app/helpers/dmsf_upload_helper.rb @@ -96,8 +96,8 @@ module DmsfUploadHelper if new_revision.save new_revision.assign_workflow(commited_file[:dmsf_workflow_id]) begin - FileUtils.mv commited_file[:tempfile_path], new_revision.disk_file - FileUtils.chmod 'u=wr,g=r', new_revision.disk_file + FileUtils.mv commited_file[:tempfile_path], new_revision.disk_file(false) + FileUtils.chmod 'u=wr,g=r', new_revision.disk_file(false) file.set_last_revision new_revision files.push(file) if file.container.is_a?(Issue) diff --git a/app/models/dmsf_file.rb b/app/models/dmsf_file.rb index 138b3e2a..bc5a91d3 100644 --- a/app/models/dmsf_file.rb +++ b/app/models/dmsf_file.rb @@ -271,7 +271,7 @@ class DmsfFile < ActiveRecord::Base new_revision.dmsf_file = file new_revision.disk_filename = new_revision.new_storage_filename if File.exist? self.last_revision.disk_file - FileUtils.cp self.last_revision.disk_file, new_revision.disk_file + FileUtils.cp self.last_revision.disk_file, new_revision.disk_file(false) end new_revision.comment = l(:comment_copied_from, :source => "#{project.identifier}: #{self.dmsf_path_str}") new_revision.custom_values = [] diff --git a/app/models/dmsf_file_revision.rb b/app/models/dmsf_file_revision.rb index edb90bc5..df214c69 100644 --- a/app/models/dmsf_file_revision.rb +++ b/app/models/dmsf_file_revision.rb @@ -140,10 +140,21 @@ class DmsfFileRevision < ActiveRecord::Base DmsfFile.storage_path.join path end - def disk_file + def disk_file(search_if_not_exists = true) path = self.storage_base_path FileUtils.mkdir_p(path) unless File.exist?(path) - path.join self.disk_filename + filename = path.join(self.disk_filename) + if search_if_not_exists + unless File.exist?(filename) + # Let's search for the physical file in source revisions + revisions = self.dmsf_file.dmsf_file_revisions.where(['id < ?', self.id]).order(:id => :desc) + revisions.each do |rev| + filename = rev.disk_file + break if File.exist?(filename) + end + end + end + filename end def new_storage_filename diff --git a/db/migrate/07_dmsf_1_4_4.rb b/db/migrate/07_dmsf_1_4_4.rb index af6c8396..2716bce2 100644 --- a/db/migrate/07_dmsf_1_4_4.rb +++ b/db/migrate/07_dmsf_1_4_4.rb @@ -89,12 +89,12 @@ class Dmsf144 < ActiveRecord::Migration DmsfFileRevision.visible.each {|rev| next if rev.project.nil? existing = DmsfFile.storage_path.join rev.disk_filename - new_path = rev.disk_file + new_path = rev.disk_file(false) begin if File.exist?(existing) if File.exist?(new_path) rev.disk_filename = rev.new_storage_filename - new_path = rev.disk_file + new_path = rev.disk_file(false) rev.save! end #Ensure the project path exists diff --git a/db/migrate/20170418104901_migrate_documents.rb b/db/migrate/20170418104901_migrate_documents.rb index 02a2757e..9f9e7a79 100644 --- a/db/migrate/20170418104901_migrate_documents.rb +++ b/db/migrate/20170418104901_migrate_documents.rb @@ -28,7 +28,7 @@ class MigrateDocuments < ActiveRecord::Migration origin = self.disk_file(dmsf_file_revision) if origin if File.exist?(origin) - target = dmsf_file_revision.disk_file + target = dmsf_file_revision.disk_file(false) if target unless File.exist?(target) begin @@ -64,7 +64,7 @@ class MigrateDocuments < ActiveRecord::Migration DmsfFileRevision.find_each do |dmsf_file_revision| if dmsf_file_revision.dmsf_file if dmsf_file_revision.dmsf_file.project - origin = dmsf_file_revision.disk_file + origin = dmsf_file_revision.disk_file(false) if origin if File.exist?(origin) target = self.disk_file(dmsf_file_revision) diff --git a/lib/redmine_dmsf/webdav/dmsf_resource.rb b/lib/redmine_dmsf/webdav/dmsf_resource.rb index 85097fb1..dc9f3788 100644 --- a/lib/redmine_dmsf/webdav/dmsf_resource.rb +++ b/lib/redmine_dmsf/webdav/dmsf_resource.rb @@ -652,7 +652,7 @@ module RedmineDmsf # implementation of service for request, which allows for us to pipe a single file through # also best-utilising DAV4Rack's implementation. def download - raise NotFound unless (file && file.last_revision && file.last_revision.disk_file) + raise NotFound unless (file && file.last_revision && file.last_revision.disk_file(false)) raise Forbidden unless (!parent.exist? || !parent.folder || DmsfFolder.permissions?(parent.folder)) # If there is no range (start of ranged download, or direct download) then we log the diff --git a/lib/tasks/dmsf_convert_documents.rake b/lib/tasks/dmsf_convert_documents.rake index a4613f40..2b3d757f 100644 --- a/lib/tasks/dmsf_convert_documents.rake +++ b/lib/tasks/dmsf_convert_documents.rake @@ -159,7 +159,7 @@ class DmsfConvertDocuments unless dry FileUtils.cp(attachment.diskfile, revision.disk_file) - revision.size = File.size(revision.disk_file) + revision.size = File.size(revision.disk_file(false)) end if dry