Permalink
Browse files

Don't upload attachments that have already been uploaded.

  • Loading branch information...
1 parent 8690d02 commit d1ce69a13ad258e1a424111e8d8fb5cdbb9413cd @hdeshev hdeshev committed May 21, 2012
Showing with 26 additions and 2 deletions.
  1. +10 −1 lib/filer/attachment.rb
  2. +4 −1 lib/filer/uploader.rb
  3. +11 −0 test/test_upload_on_save.rb
  4. +1 −0 test/user_data.rb
@@ -141,8 +141,17 @@ def display_hash
}
end
+ def uploaded?
+ @uploaded == true
+ end
+ def mark_uploaded!
+ @uploaded = true
+ end
+
def need_upload?
- if file.nil?
+ if uploaded?
+ false
+ elsif file.nil?
false
elsif file.kind_of?(FileLike)
false
@@ -26,14 +26,17 @@ def json_headers
def upload_attachment(attachment)
url = "#{Progstr::Filer.url_prefix}upload/new"
+ puts "upload to: #{url}"
begin
response = RestClient.post(url, {
:upload1 => attachment.file,
:authToken => Progstr::Filer.generate_auth_token,
:uploader => self.class.name,
:id => attachment.id
}, json_headers)
- UploadStatus.new MultiJson.decode(response)
+ result = UploadStatus.new MultiJson.decode(response)
+ attachment.mark_uploaded!
+ result
rescue => e
if e.respond_to?(:response) && !e.response.nil?
raise ApiError.new(MultiJson.decode(e.response))
@@ -31,6 +31,17 @@ class TestUploadOnSave < UserTest
assert_equal "7933ad9a0f93457ab625a070fec3544f", saved_id
end
+ should "not upload already uploaded attachments" do
+ u = User.new
+ f = FileMock.new
+ u.avatar = f
+ u.save!
+
+ $lastUploadedAtachment = nil
+ u.save!
+ assert_nil $lastUploadedAtachment
+ end
+
should "delete the previous attachment on save" do
u = User.new
f1 = FileMock.new
View
@@ -25,6 +25,7 @@ def self.down
class MockUploader < Progstr::Filer::Uploader
def upload_attachment(attachment)
$lastUploadedAtachment = attachment
+ attachment.mark_uploaded!
end
def delete_attachment(attachment)
$lastDeletedAtachment = attachment

0 comments on commit d1ce69a

Please sign in to comment.