summaryrefslogtreecommitdiff
path: root/lib/file-store.rb
blob: 181d01952b199a4714652eae55659901fa0fe654 (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
36
37
FileUtils.mkdir_p File.join(File.dirname(__FILE__),"private")

module OpenTox

  # Base class for OpenTox services
  class FileStore < Service 

    helpers do
      def next_id
        id = Dir["./private/*.nt"].collect{|f| File.basename(f,"nt").to_i}.sort.last
        id = 0 if id.nil?
        id + 1
      end

      def file id
        File.join File.dirname(File.expand_path __FILE__), "private", "#{id.to_s}.nt"
      end

      def uri_list
        Dir["./private/*.nt"].collect{|f| url_for File.basename(f,".nt")}.join("\n")
      end
    end

    get '/?' do
      uri_list
    end

    post '/?' do
      File.open(file(next_id),"w+"){|f| f.puts request.env["rack.input"].read}
    end

    get '/:id/?' do
      send_file file(params[:id])
    end
  end

end