114 lines
2.8 KiB
Ruby
114 lines
2.8 KiB
Ruby
require "camping"
|
|
require "fileutils"
|
|
require "markaby"
|
|
require "pathname"
|
|
|
|
Camping.goes :Plemp
|
|
|
|
Markaby::Builder.set(:indent, 2)
|
|
unless defined? BASE_DIR
|
|
BASE_DIR = Pathname.new(__FILE__).dirname
|
|
PUBLIC_DIR = BASE_DIR + "public"
|
|
UPLOAD_DIR = BASE_DIR + "upload"
|
|
|
|
Positions = Hash.new
|
|
end
|
|
|
|
|
|
module Plemp::Controllers
|
|
|
|
class Index
|
|
def get
|
|
render :main
|
|
end
|
|
end
|
|
|
|
class StaticXX
|
|
MIME_TYPES = {'.css' => 'text/css',
|
|
'.js' => 'text/javascript',
|
|
'.jpeg' => 'image/jpeg',
|
|
'.jpg' => 'image/jpeg',
|
|
'.png' => 'image/png'}
|
|
|
|
def get(type, path)
|
|
@headers['Content-Type'] = MIME_TYPES[path[/\.\w+$/, 0]] || "text/plain"
|
|
unless path.include? ".." or not ["public", "upload"].include? type
|
|
@headers['X-Sendfile'] = (BASE_DIR + type + path).to_s
|
|
else
|
|
@status = "403"
|
|
"403 - Invalid path"
|
|
end
|
|
end
|
|
end
|
|
|
|
class SaveposXXX
|
|
def post(id, top, left)
|
|
$stderr.puts("Got: id: #{id} -> top: #{top}, left: #{left}")
|
|
Positions[id] = [top, left]
|
|
$stderr.puts Positions.inspect
|
|
""
|
|
end
|
|
end
|
|
|
|
class Upload
|
|
def post
|
|
orig_ext = File.extname(@input.file[:filename]).downcase
|
|
new_file = UPLOAD_DIR + Time.now.strftime("%Y%m%d%H%M%S#{orig_ext}")
|
|
new_file.open("w") do |f|
|
|
f.write(@input.file[:tempfile].read)
|
|
end
|
|
redirect Index
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
module Plemp::Views
|
|
|
|
def layout
|
|
xhtml_strict do
|
|
head do
|
|
title "Plemp!"
|
|
link :rel => "stylesheet", :type => "text/css",
|
|
:media => "screen", :href => R(StaticXX, "public", "plemp.css")
|
|
['prototype', 'scriptaculous', 'dragreg'].each do |js|
|
|
script :src => R(StaticXX, "public", "#{js}.js"),
|
|
:type => "text/javascript"
|
|
end
|
|
end
|
|
body do
|
|
self << yield
|
|
end
|
|
end
|
|
end
|
|
|
|
def main
|
|
form :action => R(Upload), :method => "post",
|
|
:enctype => "multipart/form-data" do
|
|
p do
|
|
input :name => "file", :id => "file", :type => "file"
|
|
input :type => "submit", :value => "Upload!"
|
|
end
|
|
end
|
|
UPLOAD_DIR.entries.select { |f| (UPLOAD_DIR + f).file? }.each do |f|
|
|
next if f.to_s =~ /^\./
|
|
file = UPLOAD_DIR + f
|
|
id = file.basename.to_s
|
|
if Positions.has_key? id
|
|
top, left = Positions[id]
|
|
else
|
|
top, left = ["#{rand(300)}", "#{rand(700)}"]
|
|
Positions[id] = [top, left]
|
|
end
|
|
img.draggable :id => id,
|
|
:src => R(StaticXX, "upload", file.basename),
|
|
:alt => file.basename
|
|
script :type => "text/javascript" do
|
|
"new Draggable('#{id}', { scroll: window });\n" +
|
|
"new Effect.Move('#{id}', { x: #{left}, y: #{top}, mode: 'absolute' });"
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|