Permalink
Browse files

Add temp file validation

  • Loading branch information...
1 parent 8a304b3 commit e620903fbe0a09285b8d15590f450d4c31ed404e @jamiehodge committed May 25, 2012
Showing with 11 additions and 2 deletions.
  1. +11 −2 lib/storable.rb
View
13 lib/storable.rb
@@ -11,11 +11,15 @@ def path
File.join dir, filename
end
+ def file_size
+ File.file?(path) ? File.size(path) : 0
+ end
+
def complete?
- File.file?(path) && File.size(path) == size
+ file_size == size
end
- def before_save
+ def before_validation
self.size ||= tempfile.size if tempfile
super
end
@@ -31,6 +35,11 @@ def before_destroy
super
end
+ def validate
+ super
+ errors.add(:tempfile, 'invalid patch') if tempfile && (complete? || tempfile.size + file_size > size)
+ end
+
def append(data)
data.rewind
File.open path, 'a' do |f|

0 comments on commit e620903

Please sign in to comment.