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!" ['drag', 'prototype', 'effects', 'controls'].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| file = UPLOAD_DIR + f id = file.basename.to_s if Positions.has_key? id 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 def _savepos(id = nil) "new Ajax.Request('http://almaz.spacelabs.nl:3301/savepos/' + " + "this.id + '/' + this.style.top + '/' + this.style.left); " + "return false;" end end