From 098552e30334177881586ae93c1ecf51476236b4 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Sun, 21 Jun 2015 00:39:02 -0700 Subject: [PATCH] fix the file size issue --- models/site.rb | 97 +++++++++++++++++++++------------------- tests/site_file_tests.rb | 9 +++- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/models/site.rb b/models/site.rb index 6c214361..fc8de90e 100644 --- a/models/site.rb +++ b/models/site.rb @@ -642,41 +642,6 @@ class Site < Sequel::Model tmpfile.path end - def delete_file(path) - return false if files_path(path) == files_path - begin - FileUtils.rm files_path(path) - rescue Errno::EISDIR - site_files.each do |site_file| - if site_file.path.match /^#{path}\// - site_file.destroy - end - end - FileUtils.remove_dir files_path(path), true - rescue Errno::ENOENT - end - - purge_cache path - - ext = File.extname(path).gsub(/^./, '') - - screenshots_delete(path) if ext.match HTML_REGEX - thumbnails_delete(path) if ext.match IMAGE_REGEX - - path = path[1..path.length] if path[0] == '/' - - DB.transaction do - site_file = site_files_dataset.where(path: path).first - if site_file - DB['update sites set space_used=space_used-? where id=?', site_file.size, self.id].first - site_file.delete - end - SiteChangeFile.filter(site_id: self.id, filename: path).delete - end - - true - end - def move_files_from(oldusername) FileUtils.mv base_files_path(oldusername), base_files_path end @@ -1115,26 +1080,31 @@ class Site < Sequel::Model end files.each do |file| - new_size += file[:tempfile].size html_uploaded = true if file[:filename].match HTML_REGEX - results << store_file(file[:filename], file[:tempfile], file[:opts] || opts) + + existing_size = 0 + site_file = site_files_dataset.where(path: scrubbed_path(file[:filename])).first + if site_file + existing_size = site_file.size + end + + res = store_file(file[:filename], file[:tempfile], file[:opts] || opts) + if res == true + new_size -= existing_size + new_size += file[:tempfile].size + end + results << res end if results.include? true && opts[:new_install] != true time = Time.now - DB['update sites set site_changed=?, site_updated_at=?, updated_at=?, changed_count=changed_count+1, space_used=space_used+? where id=?', + sql = DB["update sites set site_changed=?, site_updated_at=?, updated_at=?, changed_count=changed_count+1, space_used=space_used#{new_size < 0 ? new_size.to_s : '+'+new_size.to_s} where id=?", html_uploaded, time, time, - new_size, self.id - ].first - - #self.site_changed = true - #self.site_updated_at = Time.now - #self.updated_at = Time.now - #self.changed_count += 1 - #save_changes validate: false + ] + sql.first reload #SiteChange.record self, relative_path unless opts[:new_install] @@ -1144,6 +1114,41 @@ class Site < Sequel::Model results end + def delete_file(path) + return false if files_path(path) == files_path + begin + FileUtils.rm files_path(path) + rescue Errno::EISDIR + site_files.each do |site_file| + if site_file.path.match /^#{path}\// + site_file.destroy + end + end + FileUtils.remove_dir files_path(path), true + rescue Errno::ENOENT + end + + purge_cache path + + ext = File.extname(path).gsub(/^./, '') + + screenshots_delete(path) if ext.match HTML_REGEX + thumbnails_delete(path) if ext.match IMAGE_REGEX + + path = path[1..path.length] if path[0] == '/' + + DB.transaction do + site_file = site_files_dataset.where(path: path).first + if site_file + DB['update sites set space_used=space_used-? where id=?', site_file.size, self.id].first + site_file.delete + end + SiteChangeFile.filter(site_id: self.id, filename: path).delete + end + + true + end + private def store_file(path, uploaded, opts={}) diff --git a/tests/site_file_tests.rb b/tests/site_file_tests.rb index 77278c6f..3e9fe636 100644 --- a/tests/site_file_tests.rb +++ b/tests/site_file_tests.rb @@ -106,6 +106,14 @@ describe 'site_files' do @site.title.must_equal 'Hello?' end + it 'provides the correct space used after overwriting an existing file' do + uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg') + upload 'files[]' => uploaded_file + second_uploaded_file = Rack::Test::UploadedFile.new('./tests/files/img/test.jpg', 'image/jpeg') + upload 'files[]' => second_uploaded_file + @site.reload.space_used.must_equal second_uploaded_file.size + end + it 'does not change title for subdir index.html' do title = @site.title upload( @@ -117,7 +125,6 @@ describe 'site_files' do it 'succeeds with valid file' do uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg') - puts uploaded_file.size upload 'files[]' => uploaded_file last_response.body.must_match /successfully uploaded/i File.exists?(@site.files_path('test.jpg')).must_equal true