fix the file size issue

This commit is contained in:
Kyle Drake 2015-06-21 00:39:02 -07:00
parent 17889dff9a
commit 098552e303
2 changed files with 59 additions and 47 deletions

View file

@ -642,41 +642,6 @@ class Site < Sequel::Model
tmpfile.path tmpfile.path
end 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) def move_files_from(oldusername)
FileUtils.mv base_files_path(oldusername), base_files_path FileUtils.mv base_files_path(oldusername), base_files_path
end end
@ -1115,26 +1080,31 @@ class Site < Sequel::Model
end end
files.each do |file| files.each do |file|
new_size += file[:tempfile].size
html_uploaded = true if file[:filename].match HTML_REGEX 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 end
if results.include? true && opts[:new_install] != true if results.include? true && opts[:new_install] != true
time = Time.now 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, html_uploaded,
time, time,
time, time,
new_size,
self.id self.id
].first ]
sql.first
#self.site_changed = true
#self.site_updated_at = Time.now
#self.updated_at = Time.now
#self.changed_count += 1
#save_changes validate: false
reload reload
#SiteChange.record self, relative_path unless opts[:new_install] #SiteChange.record self, relative_path unless opts[:new_install]
@ -1144,6 +1114,41 @@ class Site < Sequel::Model
results results
end 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 private
def store_file(path, uploaded, opts={}) def store_file(path, uploaded, opts={})

View file

@ -106,6 +106,14 @@ describe 'site_files' do
@site.title.must_equal 'Hello?' @site.title.must_equal 'Hello?'
end 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 it 'does not change title for subdir index.html' do
title = @site.title title = @site.title
upload( upload(
@ -117,7 +125,6 @@ describe 'site_files' do
it 'succeeds with valid file' do it 'succeeds with valid file' do
uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg') uploaded_file = Rack::Test::UploadedFile.new('./tests/files/test.jpg', 'image/jpeg')
puts uploaded_file.size
upload 'files[]' => uploaded_file upload 'files[]' => uploaded_file
last_response.body.must_match /successfully uploaded/i last_response.body.must_match /successfully uploaded/i
File.exists?(@site.files_path('test.jpg')).must_equal true File.exists?(@site.files_path('test.jpg')).must_equal true