Class: MrMurano::File

Inherits:
SolutionBase show all
Defined in:
lib/MrMurano/Solution-File.rb

Overview

…/file

Instance Method Summary collapse

Methods inherited from SolutionBase

#dodiff, #download, #endPoint, #locallist, #removelocal, #status, #syncdown, #syncup, #tolocalpath

Methods included from Verbose

#debug, #verbose

Methods included from Http

#delete, #get, #http, #http_reset, #json_opts, #post, #postf, #put, #set_def_headers, #showHttpError, #token, #tryToPrettyJSON, #workit

Constructor Details

#initializeFile

Returns a new instance of File.



12
13
14
15
16
17
# File 'lib/MrMurano/Solution-File.rb', line 12

def initialize
  super
  @uriparts << 'file'
  @itemkey = :path
  @location = $cfg['location.files']
end

Instance Method Details

#curldebug(request) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/MrMurano/Solution-File.rb', line 55

def curldebug(request)
  # The upload will get printed out inside of upload.
  # Because we don't have the correct info here.
  if request.method != 'PUT' then
    super(request)
  end
end

#docmp(itemA, itemB) ⇒ Object



142
143
144
145
# File 'lib/MrMurano/Solution-File.rb', line 142

def docmp(itemA, itemB)
  return (itemA[:mime_type] != itemB[:mime_type] or
    itemA[:checksum] != itemB[:checksum])
end

#fetch(path, &block) ⇒ Object

Get one item of the static content.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/MrMurano/Solution-File.rb', line 27

def fetch(path, &block)
  get(path) do |request, http|
    http.request(request) do |resp|
      case resp
      when Net::HTTPSuccess
        if block_given? then
          resp.read_body &block
        else
          resp.read_body do |chunk|
            $stdout.write chunk
          end
        end
      else
        showHttpError(request, response)
        raise resp
      end
    end
    nil
  end
end

#listObject

Get a list of all of the static content



21
22
23
# File 'lib/MrMurano/Solution-File.rb', line 21

def list
  get()
end

#localitems(from) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/MrMurano/Solution-File.rb', line 147

def localitems(from)
  from = Pathname.new(from) unless from.kind_of? Pathname
  unless from.exist? then
    return []
  end
  raise "Not a directory: #{from.to_s}" unless from.directory?

  Pathname.glob(from.to_s + '/**/*').reject do |path|
    path.directory?
  end.map do |path|
    name = toRemoteItem(from, path)
    name[:local_path] = path
    name
  end
end

#remove(path) ⇒ Object

Delete a file



50
51
52
53
# File 'lib/MrMurano/Solution-File.rb', line 50

def remove(path)
  # TODO test
  delete('/'+path)
end

#synckey(item) ⇒ Object



138
139
140
# File 'lib/MrMurano/Solution-File.rb', line 138

def synckey(item)
  item[:path]
end

#tolocalname(item, key) ⇒ Object



110
111
112
113
114
# File 'lib/MrMurano/Solution-File.rb', line 110

def tolocalname(item, key)
  name = item[key]
  name = $cfg['files.default_page'] if name == '/'
  name
end

#toRemoteItem(from, path) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/MrMurano/Solution-File.rb', line 116

def toRemoteItem(from, path)
  item = super(from, path)
  name = item[:name]
  name = '/' if name == $cfg['files.default_page']
  name = "/#{name}" unless name.chars.first == '/'

  mime = MIME::Types.type_for(path.to_s)[0] || MIME::Types["application/octet-stream"][0]

  # It does not actually take the SHA1 of the file.
  # It first converts the file to hex, then takes the SHA1 of that string
  #sha1 = Digest::SHA1.file(path.to_s).hexdigest
  sha1 = Digest::SHA1.new
  path.open('rb:ASCII-8BIT') do |io|
    while chunk = io.read(1048576) do
      sha1 << Digest.hexencode(chunk)
    end
  end
  debug "Checking #{name} (#{mime.simplified} #{sha1.hexdigest})"

  {:path=>name, :mime_type=>mime.simplified, :checksum=>sha1.hexdigest}
end

#upload(local, remote) ⇒ Object

Upload a file



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/MrMurano/Solution-File.rb', line 65

def upload(local, remote)
  local = Pathname.new(local) unless local.kind_of? Pathname

  uri = endPoint('upload' + remote[:path])
  # kludge past for a bit.
  #`curl -s -H 'Authorization: token #{@token}' '#{uri.to_s}' -F file=@#{local.to_s}`

  # http://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via-http-as-multipart-form-data
  #
  # Look at: https://github.com/httprb/http
  # If it works well, consider porting over to it.
  #
  # Or just: https://github.com/httprb/form_data.rb ?
  #
  # Most of these pull into ram.  So maybe just go with that. Would guess that
  # truely large static content is rare, and we can optimize/fix that later.

  file = HTTP::FormData::File.new(local.to_s, {:mime_type=>remote[:mime_type]})
  form = HTTP::FormData.create(:file=>file)
  req = Net::HTTP::Put.new(uri)
  set_def_headers(req)
  workit(req) do |request,http|
    request.content_type = form.content_type
    request.content_length = form.content_length
    request.body = form.to_s

    if $cfg['tool.curldebug'] then
      a = []
      a << %{curl -s -H 'Authorization: #{request['authorization']}'}
      a << %{-H 'User-Agent: #{request['User-Agent']}'}
      a << %{-X #{request.method}}
      a << %{'#{request.uri.to_s}'}
      a << %{-F file=@#{local.to_s}}
      puts a.join(' ')
    end

    response = http.request(request)
    case response
    when Net::HTTPSuccess
    else
      showHttpError(request, response)
    end
  end
end