Added upload support; load and show uploaded images instead of dummies.
This commit is contained in:
parent
ab0fecd06e
commit
039bd6f0aa
65
plemp.rb
65
plemp.rb
|
@ -6,8 +6,14 @@ require "pathname"
|
||||||
Camping.goes :Plemp
|
Camping.goes :Plemp
|
||||||
|
|
||||||
Markaby::Builder.set(:indent, 2)
|
Markaby::Builder.set(:indent, 2)
|
||||||
PUBLIC_DIR = Pathname.new(__FILE__).dirname + "public"
|
unless defined? BASE_DIR
|
||||||
UPLOAD_DIR = Pathname.new(__FILE__).dirname + "upload"
|
BASE_DIR = Pathname.new(__FILE__).dirname
|
||||||
|
PUBLIC_DIR = BASE_DIR + "public"
|
||||||
|
UPLOAD_DIR = BASE_DIR + "upload"
|
||||||
|
|
||||||
|
Positions = Hash.new
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
module Plemp::Controllers
|
module Plemp::Controllers
|
||||||
|
|
||||||
|
@ -17,15 +23,17 @@ module Plemp::Controllers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class PublicX
|
class StaticXX
|
||||||
MIME_TYPES = {'.css' => 'text/css',
|
MIME_TYPES = {'.css' => 'text/css',
|
||||||
'.js' => 'text/javascript',
|
'.js' => 'text/javascript',
|
||||||
'.jpg' => 'image/jpeg'}
|
'.jpeg' => 'image/jpeg',
|
||||||
|
'.jpg' => 'image/jpeg',
|
||||||
|
'.png' => 'image/png'}
|
||||||
|
|
||||||
def get(path)
|
def get(type, path)
|
||||||
@headers['Content-Type'] = MIME_TYPES[path[/\.\w+$/, 0]] || "text/plain"
|
@headers['Content-Type'] = MIME_TYPES[path[/\.\w+$/, 0]] || "text/plain"
|
||||||
unless path.include? ".." # prevent directory traversal attacks
|
unless path.include? ".." or not ["public", "upload"].include? type
|
||||||
@headers['X-Sendfile'] = "#{PUBLIC_DIR}/#{path}"
|
@headers['X-Sendfile'] = (BASE_DIR + type + path).to_s
|
||||||
else
|
else
|
||||||
@status = "403"
|
@status = "403"
|
||||||
"403 - Invalid path"
|
"403 - Invalid path"
|
||||||
|
@ -35,14 +43,16 @@ module Plemp::Controllers
|
||||||
|
|
||||||
class SaveposXXX
|
class SaveposXXX
|
||||||
def post(id, top, left)
|
def post(id, top, left)
|
||||||
$stderr.puts("Got: top: #{top}, left: #{left}")
|
$stderr.puts("Got: id: #{id} -> top: #{top}, left: #{left}")
|
||||||
|
Positions[id] = [top, left]
|
||||||
|
$stderr.puts Positions.inspect
|
||||||
""
|
""
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Upload
|
class Upload
|
||||||
def post
|
def post
|
||||||
orig_ext = File.extname(@input.file[:filename])
|
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 = UPLOAD_DIR + Time.now.strftime("%Y%m%d%H%M%S#{orig_ext}")
|
||||||
new_file.open("w") do |f|
|
new_file.open("w") do |f|
|
||||||
f.write(@input.file[:tempfile].read)
|
f.write(@input.file[:tempfile].read)
|
||||||
|
@ -60,7 +70,8 @@ module Plemp::Views
|
||||||
head do
|
head do
|
||||||
title "Plemp!"
|
title "Plemp!"
|
||||||
['drag', 'prototype', 'effects', 'controls'].each do |js|
|
['drag', 'prototype', 'effects', 'controls'].each do |js|
|
||||||
script :src => R(PublicX, "#{js}.js"), :type => "text/javascript"
|
script :src => R(StaticXX, "public", "#{js}.js"),
|
||||||
|
:type => "text/javascript"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
body do
|
body do
|
||||||
|
@ -77,18 +88,28 @@ module Plemp::Views
|
||||||
input :type => "submit", :value => "Upload!"
|
input :type => "submit", :value => "Upload!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
div.first! :style => "position:absolute;top:120px;left:100px",
|
UPLOAD_DIR.entries.select { |f| (UPLOAD_DIR + f).file? }.each do |f|
|
||||||
:onmousedown => "drag(event)" do "First" end
|
file = UPLOAD_DIR + f
|
||||||
div.second! :style => "position:absolute;top:170px;left:300px",
|
id = file.basename.to_s
|
||||||
:onmousedown => "drag(event)",
|
if Positions.has_key? id
|
||||||
:onmouseup => _savepos do "Second" end
|
top, left = Positions[id]
|
||||||
|
else
|
||||||
|
top, left = ["#{rand(800)}px", "#{rand(400)}px"]
|
||||||
|
Positions[id] = [top, left]
|
||||||
|
end
|
||||||
|
img :id => id,
|
||||||
|
:src => R(StaticXX, "upload", file.basename),
|
||||||
|
:alt => file.basename,
|
||||||
|
:style => "position:fixed;top:#{top};left:#{left};border:thin solid black;padding:15px;background-color:white;width:300px",
|
||||||
|
:onmousedown => "drag(event)",
|
||||||
|
:onmouseup => _savepos
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def _savepos
|
def _savepos(id = nil)
|
||||||
"new Ajax.Request('http://localhost:3301/savepos/' + " +
|
"new Ajax.Request('http://almaz.spacelabs.nl:3301/savepos/' + " +
|
||||||
"this.id + '/' + this.style.top + '/' + this.style.left); " +
|
"this.id + '/' + this.style.top + '/' + this.style.left); " +
|
||||||
"return false;"
|
"return false;"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Reference in New Issue