Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 182 lines (156 sloc) 3.48 KB
require 'rubygems'
require 'sinatra'
require 'sqlite3'
require 'sinatra/activerecord'
require 'json'
require 'aws/s3'
require 'uri'
require 'base64'
require 'kramdown'
require 'coderay'
require 'rack/csrf'
configure do
@@config = JSON.parse(File.read('./app-config.json'))
use Rack::Session::Cookie, :secret => "We should create an academy of gifted people, on a new world."
use Rack::Csrf, :raise => true
end
helpers do
def csrf_token
Rack::Csrf.csrf_token(env)
end
def csrf_tag
Rack::Csrf.csrf_tag(env)
end
end
set :database, 'sqlite:///db/app.db'
ActiveRecord::Base.partial_updates = true
ActiveRecord::Base.include_root_in_json = false
class Asset < ActiveRecord::Base
validates_uniqueness_of :token
before_create :set_token
def set_token
self.token = rand(36**6).to_s(36)
end
def url_present?
self.url.present?
end
end
not_found do
erb :not_found, :layout => false
end
error do
erb :error, :layout => false
end
get '/' do
erb :index
end
get '/shorten/?' do
erb :shorten
end
post '/shorten/?' do
unless (params[:url] =~ URI::regexp).nil?
asset = Asset.new do |a|
a.url = params[:url]
end
if asset.save
redirect('/share/' + asset.token)
else
@error = "Error shortening URL."
erb :shorten
end
else
@error = "Please enter a valid URL."
erb :shorten
end
end
get '/upload/?' do
erb :upload
end
post '/upload/?' do
if params && params[:file]
tmpfile = params[:file][:tempfile]
name = Time.now.utc.to_i.to_s + '-' + params[:file][:filename]
while files = tmpfile.read(65536)
AWS::S3::Base.establish_connection!(
:access_key_id => @@config["s3"]["key"],
:secret_access_key => @@config["s3"]["secret"]
)
AWS::S3::S3Object.store(name, open(tmpfile), @@config["s3"]["bucket"], :key => "value", :access => :public_read)
end
asset = Asset.new do |a|
a.filename = name
end
asset.save!
redirect('/share/' + asset.token)
else
@error = "Error uploading file."
erb :upload
end
end
get '/text/?' do
erb :text
end
post '/text/?' do
unless params[:content].empty?
asset = Asset.new do |a|
a.content = params[:content].gsub(/<\/?[^>]*>/, "")
end
if asset.save
redirect('/share/' + asset.token)
else
@error = "Error saving text."
erb :text
end
else
@error = "Please enter some text."
erb :text
end
end
get '/code/?' do
erb :code
end
post '/code/?' do
unless params[:content].empty?
asset = Asset.new do |a|
a.language = params[:language]
a.content = params[:content]
end
if asset.save
redirect('/share/' + asset.token)
else
@error = "Error saving code."
erb :code
end
else
@error = "Please enter some code."
erb :code
end
end
get '/share/:token?' do
asset = Asset.find_by_token(params[:token])
if asset
@url = @@config["base_url"] + asset.token
erb :success
else
not_found
end
end
get '/:token' do
asset = Asset.find_by_token(params[:token])
if asset
if asset.content
if asset.language
@content = CodeRay.scan(asset.content, asset.language.to_sym).div(:line_numbers => :table)
else
@content = Kramdown::Document.new(asset.content).to_html
end
erb :display_text
elsif asset.url
redirect(asset.url)
else asset.filename
redirect("https://s3.amazonaws.com/#{@@config["s3"]["bucket"]}/" + asset.filename)
end
else
not_found
end
end