-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.ru
99 lines (79 loc) · 2.28 KB
/
config.ru
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
# -*- coding: UTF-8 -*-
require 'rubygems'
require 'bundler'
require 'pathname'
require 'json'
require "sinatra"
Bundler.require
Sinatra::Base.set(:run, false)
Sinatra::Base.set(:env, :production)
class Impact < Sinatra::Base
set :public_dir, File.dirname(__FILE__) + '/'
# This is where you could serve different pages depending on the device accessing the page,
# such as for iPads and mobile devices.
# get '/', :agent => /iphone|android/i do
# File.read('mobile.html')
# end
get '/' do
File.read('index.html')
end
get '/weltmeister' do
File.read('weltmeister.html')
end
get '/lib/weltmeister/api/glob' do
@files = params[:glob].inject([]) do |memo, glob|
Pathname.glob(glob).each { |d| memo << d.cleanpath.to_s }
memo
end
content_type :json
@files.to_json
end
get '/lib/weltmeister/api/browse' do
@dir = from_impact_basedir(params[:dir])
@type = params[:type]
extensions = []
case @type
when 'images' then extensions += %w{png gif jpg jpeg bmp}
when 'scripts' then extensions += %w{js}
else extensions += "*".to_a
end
parent = @dir ? Pathname.new(@dir).parent.cleanpath : false
directories = Pathname.new(@dir).children.select { |c| c.directory? }.map { |d| d.cleanpath }
files = Pathname.glob(File.join(@dir, "*.{#{extensions.join(',')}}")).map { |f| f.cleanpath }
content_type :json
{
:parent => parent,
:dirs => directories,
:files => files
}.to_json
end
post '/lib/weltmeister/api/save' do
response = { :error => 0 }
if params[:path] && params[:data]
@dir = from_impact_basedir(params[:path])
if File.extname(@dir) == ".js"
begin
File.open(@dir, 'w') { |f| f.write(params[:data]) }
rescue => e
response[:error] = 2
response[:msg] = "Could not save the level file. " + e.message
end
else
response[:error] = 3
response[:msg] = "File must have a .js suffix"
end
else
response[:error] = 1
response[:msg] = "No Data or Path specified"
end
content_type :json
response.to_json
end
helpers do
def from_impact_basedir(dir)
@folder = dir.to_s.sub(%r{\.\./?},"")
@folder.empty? ? "." : @folder
end
end
end
run Impact