Skip to content

Instantly share code, notes, and snippets.

@smartinez87
Created February 1, 2010 18:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save smartinez87/291877 to your computer and use it in GitHub Desktop.
Save smartinez87/291877 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'datamapper'
require 'dm-paperclip'
require 'haml'
require 'fileutils'
APP_ROOT = File.expand_path(File.dirname(__FILE__))
DataMapper::setup(:default, "sqlite3://#{APP_ROOT}/db.sqlite3")
class Resource
include DataMapper::Resource
include Paperclip::Resource
property :id, Serial
has_attached_file :file,
:url => "/:attachment/:id/:style/:basename.:extension",
:path => "#{APP_ROOT}/public/:attachment/:id/:style/:basename.:extension"
end
Resource.auto_migrate!
def make_paperclip_mash(file_hash)
mash = Mash.new
mash['tempfile'] = file_hash[:tempfile]
mash['filename'] = file_hash[:filename]
mash['content_type'] = file_hash[:type]
mash['size'] = file_hash[:tempfile].size
mash
end
post '/upload' do
halt 409, "File seems to be emtpy" unless params[:file][:tempfile].size > 0
@resource = Resource.new(:file => make_paperclip_mash(params[:file]))
halt 409, "There were some errors processing your request...\n#{resource.errors.inspect}" unless @resource.save
haml :upload
end
get '/' do
haml :index
end
__END__
@@ index
%form{:action => '/upload', :enctype => 'multipart/form-data', :method => 'POST'}
%input{:type => 'file', :name => 'file'}
%input{:type => 'submit'}
@@ upload
%p= "Congrats! This is your #{@resource.file.name} uploaded file!"
%img{:src => @resource.file.url }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment