-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
103 lines (89 loc) · 2.75 KB
/
app.rb
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require 'rubygems'
require 'sinatra'
require 'mongoid'
require 'pry'
require 'net/http'
require 'json'
#Mongoid.load!("mongoid.yml")
Mongoid.load!("./mongoid.yml", :production)
require_relative './sub_directory'
require_relative './leaf'
#binding.pry
get '/root/*/*-delete' do
path = params[:splat][0] + '/' + params[:splat][1]
sd = SubDirectory.where(path: path).first
sd.destroy
redirect '/root/' + params[:splat][0]
end
get '/root/*-delete' do
path = params[:splat][0]
sd = SubDirectory.where(path: path).first
sd.destroy
redirect '/root'
end
get '/root/' do
redirect "/root"
end
get '/root' do
sd = SubDirectory.where(name: "root").first
erb :subdir, :locals => {:name => sd.name,:path => sd.path,:parents => sd.parents,:children => sd.children,:url_frag => sd.path.split("/"), :leafs => Leaf.all.desc(:timestamp)}
end
get '/root/*/' do
redirect "/root/" + params[:splat][0]
end
get '/root/*' do
path = params[:splat][0]
sd = SubDirectory.where(path: path).first
black_hole = SubDirectory.where(path: /^#{path}/i)
leafz = []
black_hole.each do |subdir|
subdir.leafs.each do |leaf|
leafz << leaf
end
end
leaf_collection = leafz.sort_by { |hsh| hsh[:timestamp] || Time.new(2012)}.reverse
erb :subdir, :locals => {:name => sd.name,:path => sd.path,:parents => sd.parents,:children => sd.children, :url_frag => sd.path.split("/"), :leafs => leaf_collection}
end
post '/root/*-upload' do
path = params[:splat][0]
f = params[:file]
lf = Leaf.new(type: f[:type] , filename: f[:filename], description: params["description"], timestamp: Time.now)
File.open('public/uploads/' + lf.id + "_" + f[:filename], "wb") do |file|
file.write(f[:tempfile].read)
end
node = SubDirectory.where(path: path).first
lf.sub_directory = node
lf.save!
redirect '/root/' + path
end
post '/root/*-link' do
path = params[:splat][0]
uri = params["link"]
lf = Leaf.new(type: 'link' , filename: uri, description: params["description"],timestamp: Time.now, embed: JSON.parse(Net::HTTP.get_response(URI.parse("http://opendirectory.me:8081/parse?content=#{uri}")).body).first)
node = SubDirectory.where(path: path).first
lf.sub_directory = node
lf.save!
redirect '/root/' + path
end
def make_absolute( href, root )
URI.parse(root).merge(URI.parse(href)).to_s
end
post '/root/*' do
path = params[:splat][0]
sd = SubDirectory.new(path: path +"/"+ params[:name], name: params[:name], timestamp: Time.now)
sdP = SubDirectory.where(path: path).first
sd.parents << sdP
sd.save!
redirect '/root/' + path
end
post '/root' do
path = 'root'
sd = SubDirectory.new(path: params[:name], name: params[:name], timestamp: Time.now)
sdP = SubDirectory.where(path: path).first
sd.parents << sdP
sd.save!
redirect '/root'
end
get '/*' do
redirect '/root'
end