From 557e59c28e43351195a4bc5b11b991faa303dece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 13 Apr 2016 16:33:00 +0200 Subject: [PATCH] check the uploads doesn't already exists before copying it --- lib/tasks/uploads.rake | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake index 6a95469fd..d5df837a9 100644 --- a/lib/tasks/uploads.rake +++ b/lib/tasks/uploads.rake @@ -14,6 +14,12 @@ def gather_uploads_for_all_sites RailsMultisite::ConnectionManagement.each_connection { gather_uploads } end +def file_exists?(path) + File.exists?(path) && File.size(path) > 0 +rescue + false +end + def gather_uploads public_directory = "#{Rails.root}/public" current_db = RailsMultisite::ConnectionManagement.current_db @@ -30,12 +36,15 @@ def gather_uploads source = "#{public_directory}#{from}" destination = "#{public_directory}#{to}" - # create destination directory - `mkdir -p '#{File.dirname(destination)}'` - # copy the file - `cp --link '#{source}' '#{destination}'` + # create destination directory & copy file unless it already exists + unless file_exists?(destination) + `mkdir -p '#{File.dirname(destination)}'` + `cp --link '#{source}' '#{destination}'` + end + # ensure file has been succesfuly copied over - raise unless File.exists?(destination) && File.size(destination) > 0 + raise unless file_exists?(destination) + # remap links in db DbHelper.remap(from, to) rescue