Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Copy a file from one web server to another (See related posts)

This Ruby code is designed to copy a file from one web server to another. There's nothing special about the code however it can be quite helpful when 2 web servers can easily transfer files between each other using this approach.

#!/usr/bin/ruby
#file: getwebfile.rb

require 'rio'
require 'tempfile'

class GetWebFile

  def initialize()
  end

  def get_file(url)
    filedir = '../temp/'
    filename = url[/\w+\.\w+$/]
    rio(url) > rio(filedir + filename)
    filedir + filename
  end  

  # use with cgi; retrieve the projectx xml interface parameter 'file_url'
  def call_get_web_file(params)
    h = Hash.new
    doc = Document.new(params)
    file_url = get_param(doc,'file_url')
    get_file(file_url)
  end

  def get_param(doc, var)
    node = doc.root.elements["param[@var='#{var}']"]
    node.text.to_s
  end 
end

if __FILE__ == $0
    g = GetWebFile.new()
    g.get_file('http://192.168.1.107/audio/prompt01.wav')
end


Output (ls ../temp -ltr):
-rw-r--r-- 1 root   root   161804 Oct  6 17:45 prompt01.wav


Here's a sample ProjectX interface used for querying the ProjectX web service
<project name="convert_audio">
  <methods>
    <method name="get_web_file">
      <params>
        <param var='file_url'>http://192.168.1.107/audio/prompt01.wav</param>
      </params>
    </method>
  </method>
</project>


Background:
I intended to convert a wav file to ogg but rather than install the necessary vorbis tools for each machine I realised it would be easier just to call a web service. Each web server will be set up with a 'get_web_file' kind of service, the 1st web server will request the 2nd web server to fetch the wav file. That will then convert it to ogg and request the 1st web server to fetch the ogg file from it's location.

Resources:
- Rio [rubyforge.org]
- Using Rio from Ruby to easily save a Web page to file [dzone.com]

You need to create an account or log in to post comments to this site.


Click here to browse all 6645 code snippets

Related Posts