Skip to content

Commit

Permalink
Don't upload attachments that have already been uploaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdeshev committed May 21, 2012
1 parent 8690d02 commit d1ce69a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/filer/attachment.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -141,8 +141,17 @@ def display_hash
} }
end end


def uploaded?
@uploaded == true
end
def mark_uploaded!
@uploaded = true
end

def need_upload? def need_upload?
if file.nil? if uploaded?
false
elsif file.nil?
false false
elsif file.kind_of?(FileLike) elsif file.kind_of?(FileLike)
false false
Expand Down
5 changes: 4 additions & 1 deletion lib/filer/uploader.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ def json_headers


def upload_attachment(attachment) def upload_attachment(attachment)
url = "#{Progstr::Filer.url_prefix}upload/new" url = "#{Progstr::Filer.url_prefix}upload/new"
puts "upload to: #{url}"
begin begin
response = RestClient.post(url, { response = RestClient.post(url, {
:upload1 => attachment.file, :upload1 => attachment.file,
:authToken => Progstr::Filer.generate_auth_token, :authToken => Progstr::Filer.generate_auth_token,
:uploader => self.class.name, :uploader => self.class.name,
:id => attachment.id :id => attachment.id
}, json_headers) }, json_headers)
UploadStatus.new MultiJson.decode(response) result = UploadStatus.new MultiJson.decode(response)
attachment.mark_uploaded!
result
rescue => e rescue => e
if e.respond_to?(:response) && !e.response.nil? if e.respond_to?(:response) && !e.response.nil?
raise ApiError.new(MultiJson.decode(e.response)) raise ApiError.new(MultiJson.decode(e.response))
Expand Down
11 changes: 11 additions & 0 deletions test/test_upload_on_save.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ class TestUploadOnSave < UserTest
assert_equal "7933ad9a0f93457ab625a070fec3544f", saved_id assert_equal "7933ad9a0f93457ab625a070fec3544f", saved_id
end 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 should "delete the previous attachment on save" do
u = User.new u = User.new
f1 = FileMock.new f1 = FileMock.new
Expand Down
1 change: 1 addition & 0 deletions test/user_data.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def self.down
class MockUploader < Progstr::Filer::Uploader class MockUploader < Progstr::Filer::Uploader
def upload_attachment(attachment) def upload_attachment(attachment)
$lastUploadedAtachment = attachment $lastUploadedAtachment = attachment
attachment.mark_uploaded!
end end
def delete_attachment(attachment) def delete_attachment(attachment)
$lastDeletedAtachment = attachment $lastDeletedAtachment = attachment
Expand Down

0 comments on commit d1ce69a

Please sign in to comment.