Permalink
Browse files

:overwrite key added as a parameter to the upload() method

  • Loading branch information...
1 parent 55885e2 commit 601cb1fef34ca7e6fdcb394c1a8e28e2088c76ff @odiszapc committed Nov 24, 2012
View
@@ -1,3 +1,7 @@
+## ver. 0.0.6 2012-11-25
+
+* [changed] upload(): :overwrite parameter added
+
## ver. 0.0.5 2012-11-21
* [fixed] remove_orphans!()
View
@@ -73,6 +73,28 @@ By default, file is uploaded into the root folder:
api.upload("/home/odiszapc/my_damn_humster.mov")
```
+Rapidshare allows to store multiple files having equal names under the same folder. I believe this behaviour is absolutely wrong.
+Therefore, each time upload performed it checks if the file with the given name already exists in a folder.
+If it's true, the upload() just returns info about the existing file with the :already_exists? flag is set to true without any real upload being performed.
+To force file being overwritten set the :overwrite parameter to true:
+
+```ruby
+api.upload "/home/odiszapc/my_damn_cat.mov",
+ :to => "/gallery/video",
+ :as => "cat1.mov"
+
+# No upload will be performed
+api.upload "/home/odiszapc/my_damn_cat.mov",
+ :to => "/gallery/video",
+ :as => "cat1.mov"
+
+# With the following notation file will be uploaded with overwriting the existing one
+api.upload "/home/odiszapc/my_damn_cat.mov",
+ :to => "/gallery/video",
+ :as => "cat1.mov",
+ :overwrite => true
+```
+
Deleting files:
```ruby
api.remove_file("/putin/is/a/good/reason/to/live/abroad/ticket_to_Nicaragua.jpg")
@@ -113,13 +113,16 @@ def move_folder(source_path, params = {})
# Folder to place uploaded file to, default: "/"
# <tt>:as</tt>::
# The name file will have in storage after it has been uploaded
+ # <tt>:overwrite</tt>::
+ # Overwrite file if it already exists in the given folder
#
# api.upload("/home/odiszapc/my_damn_cat.mov", :to => "/gallery/video", :as => "cat1.mov")
def upload(file_path, params = {})
raise Exception unless File.exist? file_path
dest_path = path_trim(params.delete(:to) || '/')
folder_id = self.add_folder dest_path
file_name = params.delete(:as) || File.basename(file_path)
+ overwrite = params.delete :overwrite
# Check file already exists within a folder
listfiles_params = {
@@ -130,11 +133,14 @@ def upload(file_path, params = {})
}
listfiles_response = self.listfiles listfiles_params
- # In case of file is not existing upload it
- if "NONE" == listfiles_response[0][0]
+ file_already_exists = ("NONE" != listfiles_response[0][0])
+ remove_file "#{dest_path}/#{file_name}" if file_already_exists && overwrite
+
+ # In case of file is not existing then upload it
+ if !file_already_exists || overwrite
upload_server = "rs#{self.nextuploadserver}.rapidshare.com"
- upload_params = {
+ upload_params = {
:server => upload_server,
:folder => folder_id,
:filename => file_name,
@@ -1,5 +1,5 @@
module Rapidshare
module Ext
- VERSION = "0.0.5"
+ VERSION = "0.0.6"
end
end
@@ -0,0 +1 @@
+Another file to test upload
@@ -17,6 +17,10 @@ def setup
@upload_file_1 = File.expand_path(File.dirname(__FILE__) + "/../fixtures/files/upload1.txt")
@upload_file_1_md5 = Digest::MD5.hexdigest(File.read(@upload_file_1))
@upload_file_1_size = File.size @upload_file_1
+
+ @upload_file_2 = File.expand_path(File.dirname(__FILE__) + "/../fixtures/files/upload2.txt")
+ @upload_file_2_md5 = Digest::MD5.hexdigest(File.read(@upload_file_2))
+ @upload_file_2_size = File.size @upload_file_2
end
context "Api" do
@@ -58,8 +62,17 @@ def setup
# Upload the same file again
response = @rs.upload @upload_file_1, :to => remote_dir, :as => remote_filename
- upload_assertion.call response, @upload_file_1_size, @upload_file_1_md5, remote_filename
assert_true response[:already_exists?]
+ upload_assertion.call response, @upload_file_1_size, @upload_file_1_md5, remote_filename
+
+ # Upload another file under the same path but with overwriting the existing one
+ response = @rs.upload @upload_file_2,
+ :to => remote_dir,
+ :as => remote_filename,
+ :overwrite => true
+
+ assert_false response[:already_exists?]
+ upload_assertion.call response, @upload_file_2_size, @upload_file_2_md5, remote_filename
end
should "download file" do
@@ -354,6 +367,9 @@ def setup
assert_raise_message(msg) do
@rs.reload! :validate => true
end
+
+ # Remedy
+ @rs.erase_all_data!
end
end
end

0 comments on commit 601cb1f

Please sign in to comment.