Permalink
Browse files

base

  • Loading branch information...
1 parent 616dae9 commit 027f4def7b93f5fe3c754a471a186ecf2f1bb271 Alvin S.J. Ng committed Dec 7, 2011
Showing with 141 additions and 0 deletions.
  1. +3 −0 Gemfile
  2. +17 −0 Gemfile.lock
  3. +121 −0 app.rb
  4. 0 store/abc.txt
View
@@ -0,0 +1,3 @@
+source :rubygems
+
+gem 'sinatra'
View
@@ -0,0 +1,17 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ rack (1.3.5)
+ rack-protection (1.1.4)
+ rack
+ sinatra (1.3.1)
+ rack (~> 1.3, >= 1.3.4)
+ rack-protection (~> 1.1, >= 1.1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ sinatra
View
121 app.rb
@@ -0,0 +1,121 @@
+require 'sinatra'
+
+require 'json'
+require 'fileutils'
+
+# "authentication" filter
+before /\/api\/v1\/*/ do
+ raise Sinatra::NotFound if params[:u].nil? or params[:k].nil?
+
+ raise Sinatra::NotFound unless params[:u]=="alvin" || params[:k]=="beer"
+end
+
+# login
+get '/api/v1/login' do
+ {:folders=>print_folders("store") }.to_json
+end
+
+
+# get folder info
+get '/api/v1/folder' do
+ raise Sinatra::NotFound unless validate_id( params[:folder_id] )
+
+ if File.ftype(params[:folder_id]) == "file"
+ {:error=>"it is a file" }.to_json
+ else
+ {:folders=>print_folders( params[:folder_id] ) }.to_json
+ end
+
+end
+
+# update folder/file
+put '/api/v1/manage' do
+ raise Sinatra::NotFound unless validate_id( params[:folder_id] )
+ raise Sinatra::NotFound if params[:name].nil? || params[:name].empty?
+
+ path = File.dirname(params[:folder_id])
+
+ respond_with do
+ File.rename(params[:folder_id], "#{path}/#{params[:name]}")
+ end
+
+end
+
+# delete folder/file
+delete '/api/v1/manage' do
+ raise Sinatra::NotFound unless validate_id( params[:folder_id] )
+
+ respond_with do
+ File.delete(params[:folder_id])
+ end
+end
+
+# create folder
+post '/api/v1/manage' do
+ raise Sinatra::NotFound unless validate_id( params[:folder_id], false )
+
+ respond_with do
+ FileUtils.mkdir(params[:folder_id])
+ end
+end
+
+# uploade file
+post '/api/v1/upload' do
+ raise Sinatra::NotFound unless validate_id( params[:folder_id] )
+ raise Sinatra::NotFound if params[:file].nil?
+
+ respond_with do
+ tempfile = params[:file][:tempfile]
+ name = File.basename(tempfile)
+
+ FileUtils.cp(tempfile.path, "#{params[:folder_id]}/#{name}")
+ end
+
+end
+
+# download file
+get '/api/v1/download' do
+ raise Sinatra::NotFound unless validate_id( params[:folder_id] )
+
+ send_file(params[:folder_id])
+end
+
+# wrap to response
+def respond_with
+ begin
+ yield
+ rescue
+ return {:error=>"Operation failed"}.to_json
+ end
+ {:result => "OK"}.to_json
+end
+
+# list folders
+def print_folders( root )
+ raise Sinatra::NotFound if root.nil?
+ Dir.glob("#{root}/*").map do|filename|
+ { :folder_id => filename,
+ :name => File.basename(filename) }
+ end
+end
+
+# validate folder_id
+def validate_id( folder_id, test=true )
+
+ if folder_id.nil? ||
+ folder_id.empty? ||
+ folder_id =~ /^\/.*/ || #/
+ folder_id =~ /\.\./ #..
+ !(folder_id =~ /^store.*/ )
+ false
+ else
+
+ begin
+ f = File.open(folder_id,"r")
+ rescue
+ return false
+ end if test
+
+ true
+ end
+end
View
No changes.

0 comments on commit 027f4de

Please sign in to comment.