clean up some of the delete code, fix space_used bug (hopefully)

This commit is contained in:
Kyle Drake 2015-10-15 16:37:47 -07:00
parent b98d982e5b
commit 96478b25bb
4 changed files with 56 additions and 32 deletions

View file

@ -15,7 +15,7 @@ gem 'mail'
gem 'google-api-client', require: 'google/api_client'
gem 'tilt'
gem 'erubis'
gem 'stripe' #, source: 'https://code.stripe.com/'
gem 'stripe', '1.15.0' #, source: 'https://code.stripe.com/'
gem 'screencap', '~> 0.1.4'
gem 'cocaine'
gem 'zipruby'
@ -77,7 +77,7 @@ group :test do
gem 'capybara_minitest_spec'
gem 'rack_session_access', require: nil
gem 'webmock', require: nil
gem 'stripe-ruby-mock', '~> 2.0.1', require: 'stripe_mock'
gem 'stripe-ruby-mock', '2.0.1', require: 'stripe_mock'
gem 'timecop'
platform :mri, :rbx do

View file

@ -1263,35 +1263,9 @@ class Site < Sequel::Model
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.path == path
site_file.destroy
end
end
FileUtils.remove_dir files_path(path), true
rescue Errno::ENOENT
end
delete_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 && !site_file.size.nil? && !site_file.is_directory
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
path = scrubbed_path path
site_file = site_files_dataset.where(path: path).first
site_file.destroy if site_file
true
end

View file

@ -2,4 +2,48 @@ class SiteFile < Sequel::Model
unrestrict_primary_key
plugin :update_primary_key
many_to_one :site
def before_destroy
if is_directory
site.site_files_dataset.where(path: /^#{path}\//, is_directory: true).all.each do |site_file|
begin
site_file.destroy
rescue Sequel::NoExistingObject
end
end
site.site_files_dataset.where(path: /^#{path}\//, is_directory: false).all.each do |site_file|
site_file.destroy
end
begin
FileUtils.remove_dir site.files_path(path)
rescue Errno::ENOENT
end
else
begin
FileUtils.rm site.files_path(path)
rescue Errno::ENOENT
end
ext = File.extname(path).gsub(/^./, '')
site.screenshots_delete(path) if ext.match Site::HTML_REGEX
site.thumbnails_delete(path) if ext.match Site::IMAGE_REGEX
end
super
end
def after_destroy
super
unless is_directory
DB['update sites set space_used=space_used-? where id=?', size, site_id].first
end
site.delete_cache site.files_path(path)
SiteChangeFile.filter(site_id: site_id, filename: path).delete
end
end

View file

@ -48,13 +48,14 @@ describe 'site_files' do
DeleteCacheOrderWorker.jobs.length.must_equal 1
args = DeleteCacheOrderWorker.jobs.first['args']
args[0].must_equal @site.username
args[1].must_equal 'test.jpg'
args[1].must_equal '/test.jpg'
end
it 'flushes surf for index.html' do
uploaded_file = Rack::Test::UploadedFile.new('./tests/files/index.html', 'text/html')
upload 'files[]' => uploaded_file
delete_file filename: '/index.html'
DeleteCacheOrderWorker.jobs.length.must_equal 3
DeleteCacheOrderWorker.jobs.collect {|j| j['args'].last}.must_equal ['/index.html', '/?surf=1', '/']
end
@ -68,7 +69,12 @@ describe 'site_files' do
'dir' => '',
'files[]' => Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
)
space_used = @site.reload.space_used
delete_file filename: 'test'
@site.reload.space_used.must_equal(space_used - File.size('./tests/files/test.jpg'))
@site.site_files.select {|f| f.path == 'test'}.length.must_equal 0
@site.site_files.select {|f| f.path =~ /^test\//}.length.must_equal 0
@site.site_files.select {|f| f.path =~ /^test.jpg/}.length.must_equal 1