diff --git a/Gemfile b/Gemfile index 68a1e04..db202f1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,12 +1,22 @@ source 'https://rubygems.org' +# Application / Server gem 'sinatra' +gem 'shotgun' +gem 'unicorn' + +# Database gem 'sequel' gem 'pg' + +# Encryption / Decryption +gem 'encryptor' + +# Syntax Highlighting gem 'pygments.rb' +# Sugar gem 'haml' gem 'sass' -gem 'unicorn' -gem 'shotgun' + diff --git a/Gemfile.lock b/Gemfile.lock index d0dc520..9f846e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + encryptor (1.1.3) haml (4.0.3) tilt kgio (2.8.0) @@ -32,6 +33,7 @@ PLATFORMS ruby DEPENDENCIES + encryptor haml pg pygments.rb diff --git a/app/database.rb b/app/database.rb index 85b70f9..ec28011 100644 --- a/app/database.rb +++ b/app/database.rb @@ -1,8 +1,9 @@ DB = Sequel.connect(CONFIG[:database_url]) +# DB.drop_table(:pastes) if DB.table_exists?(:pastes) + DB.create_table(:pastes) do primary_key :id - String :handle String :type Text :contents end unless DB.table_exists?(:pastes) diff --git a/app/paste.rb b/app/paste.rb index ca3af79..8a82182 100644 --- a/app/paste.rb +++ b/app/paste.rb @@ -1,20 +1,37 @@ -module Paste - def get(params) - table.where(params).first +class Paste + attr_reader :contents + + class << self + def find(id) + record = DB[:pastes].where(id: id).first + record ? new(record[:contents]) : nil + end end - def add(params) - id = table.insert(params) - return unless id.is_a?(Integer) - - get(id: id) + def initialize(contents) + @contents = contents end - private - - def table - DB[:pastes] + def save + encrypt! + DB[:pastes].insert(contents: contents) end - extend self + def decrypt(key) + @key = key + decrypt! + self + end + + def key + @key ||= SecureRandom.hex + end + + def encrypt! + @contents = Base64.encode64(Encryptor.encrypt(value: contents, key: key)) + end + + def decrypt! + @contents = Encryptor.decrypt(value: Base64.decode64(contents), key: key) + end end diff --git a/pastemaster.rb b/pastemaster.rb index 924f2d3..5987f43 100644 --- a/pastemaster.rb +++ b/pastemaster.rb @@ -1,8 +1,10 @@ require 'securerandom' +require 'base64' require 'bundler/setup' require 'sinatra' require 'sequel' +require 'encryptor' require 'haml' require 'app/config' @@ -17,12 +19,17 @@ class Pastemaster < Sinatra::Application end post '/' do - record = Paste.add(handle: SecureRandom.hex, contents: params[:contents]) - redirect record ? "/#{id}/#{record[:handle]}" : '' + paste = Paste.new(params[:contents]) + id = paste.save + + redirect "/#{id}/#{paste.key}" end - get '/:id/:handle' do - @record = Paste.get(id: params[:id], handle: params[:handle]) + get '/:id/:key' do + @paste = Paste.find(params[:id]) + redirect '/' unless @paste + + @paste.decrypt(params[:key]) haml :show, layout: :default end end diff --git a/views/show.haml b/views/show.haml index 1ba719b..7c06e4d 100644 --- a/views/show.haml +++ b/views/show.haml @@ -1,5 +1,5 @@ %p - - if @record - = @record[:contents] + - if @paste + = @paste.contents - else - Sorry no have mister + .nohave Sorry no have mister