aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/share/ruby/aserv.rb
blob: 1ac3d6066ce2aabf942cb6b11417db9544418db0 (plain)
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
require "webrick"

#
# Dummy asset server
#

class AssetServlet < WEBrick::HTTPServlet::AbstractServlet
  def do_GET(req, res)
    uuid = req.path.split("/")[2].downcase.gsub(/[^0-9a-f]+/, "")
    if uuid.length == 32
      # length is correct
      File.open("assets/#{uuid}/data") do |f|
        res.body = f.read
      end
    end
    # res["content-type"] = "text/plain" # or what do we set it to ?
  end
  def do_POST(req, res)
    uuid = req.path.split("/")[2].downcase.gsub(/[^0-9a-f]+/, "")
    if uuid.length == 32
      Dir.mkdir("assets/#{uuid}")
      File.open("assets/#{uuid}/data", "wb") do |f|
        f.write req.body
	STDERR.print "Written #{req.body.length} bytes for uuid #{uuid}\n\n"
      end
    end
  end
end


svr = WEBrick::HTTPServer.new(:Port=>8003)
svr.mount("/assets", AssetServlet, 5000000)
trap(:INT){ svr.shutdown }
svr.start