forked from ssoper/jquery-binaryUpload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.rb
More file actions
51 lines (44 loc) · 1.08 KB
/
sample.rb
File metadata and controls
51 lines (44 loc) · 1.08 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'rubygems'
require 'sinatra'
require 'fileutils'
require 'json'
mime_type :json, 'application/json'
set :public, Proc.new { root }
set :views, Proc.new { root }
get '/' do
erb :index
end
post '/upload' do
content_type :json
file, name = params[:uploaded_file].slice(*[:tempfile, :filename]).values
FileUtils.cp(file.path, "#{uploaded_files_path}/#{name}")
{ :success => true }.to_json
end
get '/uploads' do
content_type :json
files = Dir.glob("#{uploaded_files_path}/*").collect do |path|
{ :size => kilo(File.size(path)), :name => path.split('/').last }
end
files.to_json
end
def kilo(n)
count = 0
while n >= 1024 and count < 4
n /= 1024.0
count += 1
end
format("%.2f",n) + %w(B KB MB GB TB)[count]
end
def uploaded_files_path
path = File.join(File.dirname(__FILE__), 'uploaded_files')
FileUtils.mkdir(path) unless File.exist?(path)
path
end
class Hash
def slice(*keys)
keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
hash = self.class.new
keys.each { |k| hash[k] = self[k] if has_key?(k) }
hash
end
end