Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  def upload_name_for_file(full_path)
    require "#{ENV['TM_SUPPORT_PATH']}/lib/dialog.rb"

    # WordPress automatically places files into dated paths
    prefix = mode == 'wp' ? '' : Time.now.strftime('%F_')
    file   = File.basename(full_path)

    suggested_name = prefix + file.gsub(/[ ]+/, '_')
    result = Dialog.request_string(:title => "Upload File", :prompt => "Name to use for uploaded file:", :default => suggested_name)
    TextMate.exit_discard if result.nil?

    [ result, 'Download: ' + result ]

  end 
  
  def upload_file
      require "#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb"
      require "#{ENV['TM_SUPPORT_PATH']}/lib/escape.rb"
      require "#{ENV['TM_SUPPORT_PATH']}/lib/dialog.rb"
      require 'xmlrpc/base64'
      choices = ["Upload File and link","Insert Filepath/Filename"]
       item = Dialog.request_item(:title => "Make a selection", :prompt => "What should I do with this file?", :items => choices)
       TextMate.exit_discard if item.nil?
       item = item.split
       case item[0]
        when "Insert"
          print ENV['TM_DROPPED_FILEPATH']
        when "Upload"
          # Makes sure endpoint is determined and elements are parsed
          current_password = password

          # The packet we will be constructing
          data = {}

          full_path = ENV['TM_DROPPED_FILEPATH']
          upload_name, title = upload_name_for_file(full_path)

          data['name'] = upload_name
          data['bits'] = XMLRPC::Base64.new(IO.read(full_path))
          TextMate.call_with_progress(:title => "Upload File", :message => "Uploading to Server “#{@host}”…") do
            begin
              result = client.newMediaObject(self.blog_id, self.username, current_password, data)
              url = result['url']
              if url
                case ENV['TM_SCOPE']
                  when /\.markdown/
                    print "[${1:#{CGI::escapeHTML title}}](#{url})"
                  else
                    print %Q{<a href="#{url}" title="${1:#{CGI::escapeHTML title}}">${2:#{CGI::escapeHTML title}}</a>$0}
                end
              else
                TextMate.exit_show_tool_tip("Error uploading file.")
              end
            rescue XMLRPC::FaultException => e
              TextMate.exit_show_tool_tip("Error uploading file. Check your configuration and try again. " + e)
            end
       end
  end
end