lib/rapidshare-ext/api.rb in rapidshare-ext-0.0.5 vs lib/rapidshare-ext/api.rb in rapidshare-ext-0.0.6
- old
+ new
@@ -111,31 +111,37 @@
# @param [Hash] params
# <tt>:to</tt>::
# 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 = {
:realfolder => folder_id,
:filename => "#{file_name}",
:fields => "md5hex,size",
:parser => :csv
}
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,
:filecontent => file_path,
:method => :post,
\ No newline at end of file