Permalink
Browse files

Working, getting postgres binary (bytea) out as images

  • Loading branch information...
0 parents commit 058e989e709555a365f8f538c44127509e6ff809 @lyle committed Feb 25, 2012
Showing with 287 additions and 0 deletions.
  1. +60 −0 .rvmrc
  2. +8 −0 Gemfile
  3. +86 −0 Gemfile.lock
  4. 0 RMagick
  5. +4 −0 how to get shit out of postgres.html
  6. BIN public/favicon.ico
  7. +106 −0 viewimage.rb
  8. +23 −0 views/index.erb
60 .rvmrc
@@ -0,0 +1,60 @@
+#!/usr/bin/env bash
+
+# This is an RVM Project .rvmrc file, used to automatically load the ruby
+# development environment upon cd'ing into the directory
+
+# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
+environment_id="ruby-1.9.2-p180@sinatra-geekspeak"
+
+#
+# First we attempt to load the desired environment directly from the environment
+# file. This is very fast and efficicent compared to running through the entire
+# CLI and selector. If you want feedback on which environment was used then
+# insert the word 'use' after --create as this triggers verbose mode.
+#
+if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
+then
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
+
+ if [[ -s ".rvm/hooks/after_use" ]]
+ then
+ . ".rvm/hooks/after_use"
+ fi
+else
+ # If the environment file has not yet been created, use the RVM CLI to select.
+ if ! rvm --create use "$environment_id"
+ then
+ echo "Failed to create RVM environment ''."
+ fi
+fi
+
+#
+# If you use an RVM gemset file to install a list of gems (*.gems), you can have
+# it be automatically loaded. Uncomment the following and adjust the filename if
+# necessary.
+#
+# filename=".gems"
+# if [[ -s "$filename" ]] ; then
+# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
+# fi
+
+#
+# If you use bundler and would like to run bundle each time you enter the
+# directory, you can uncomment the following code.
+#
+# export PATH="./bin:$PATH"
+#
+# # Ensure that Bundler is installed. Install it if it is not.
+# if ! command -v bundle >/dev/null; then
+# printf "The rubygem 'bundler' is not installed. Installing it now.\n"
+# gem install bundler
+# fi
+#
+# # Bundle while reducing excess noise.
+# printf "Bundling your gems. This may take a few minutes on a fresh clone.\n"
+# bundle --binstubs | grep -v '^Using ' | grep -v ' is complete' | sed '/^$/d'
+#
+# alias bundle="bundle --binstubs"
+#
+
@@ -0,0 +1,8 @@
+source "http://rubygems.org"
+
+gem 'rack'
+gem 'sinatra'
+gem 'datamapper'
+gem 'pry'
+gem 'dm-postgres-adapter'
+gem 'pg'
@@ -0,0 +1,86 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.7)
+ bcrypt-ruby (3.0.1)
+ coderay (1.0.5)
+ data_objects (0.10.8)
+ addressable (~> 2.1)
+ datamapper (1.2.0)
+ dm-aggregates (~> 1.2.0)
+ dm-constraints (~> 1.2.0)
+ dm-core (~> 1.2.0)
+ dm-migrations (~> 1.2.0)
+ dm-serializer (~> 1.2.0)
+ dm-timestamps (~> 1.2.0)
+ dm-transactions (~> 1.2.0)
+ dm-types (~> 1.2.0)
+ dm-validations (~> 1.2.0)
+ dm-aggregates (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-constraints (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-core (1.2.0)
+ addressable (~> 2.2.6)
+ dm-do-adapter (1.2.0)
+ data_objects (~> 0.10.6)
+ dm-core (~> 1.2.0)
+ dm-migrations (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-postgres-adapter (1.2.0)
+ dm-do-adapter (~> 1.2.0)
+ do_postgres (~> 0.10.6)
+ dm-serializer (1.2.1)
+ dm-core (~> 1.2.0)
+ fastercsv (~> 1.5.4)
+ json (~> 1.6.1)
+ json_pure (~> 1.6.1)
+ multi_json (~> 1.0.3)
+ dm-timestamps (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-transactions (1.2.0)
+ dm-core (~> 1.2.0)
+ dm-types (1.2.1)
+ bcrypt-ruby (~> 3.0.0)
+ dm-core (~> 1.2.0)
+ fastercsv (~> 1.5.4)
+ json (~> 1.6.1)
+ multi_json (~> 1.0.3)
+ stringex (~> 1.3.0)
+ uuidtools (~> 2.1.2)
+ dm-validations (1.2.0)
+ dm-core (~> 1.2.0)
+ do_postgres (0.10.8)
+ data_objects (= 0.10.8)
+ fastercsv (1.5.4)
+ json (1.6.5)
+ json_pure (1.6.5)
+ method_source (0.7.0)
+ multi_json (1.0.4)
+ pg (0.13.2)
+ pry (0.9.8.2)
+ coderay (~> 1.0.5)
+ method_source (~> 0.7)
+ slop (>= 2.4.4, < 3)
+ rack (1.4.1)
+ rack-protection (1.2.0)
+ rack
+ sinatra (1.3.2)
+ rack (~> 1.3, >= 1.3.6)
+ rack-protection (~> 1.2)
+ tilt (~> 1.3, >= 1.3.3)
+ slop (2.4.4)
+ stringex (1.3.2)
+ tilt (1.3.3)
+ uuidtools (2.1.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ datamapper
+ dm-postgres-adapter
+ pg
+ pry
+ rack
+ sinatra
No changes.
@@ -0,0 +1,4 @@
+\copy (select encode(data, 'hex') from images where id=5) TO '/Users/lyle/Desktop/image.hex';
+
+
+xxd -p -r image.hex > image.jpg
Binary file not shown.
@@ -0,0 +1,106 @@
+#!/usr/bin/env ruby
+
+require "bundler/setup"
+require 'sinatra'
+require 'rubygems'
+require 'data_mapper'
+require 'dm-core'
+#require 'quick_magick'
+require 'pry'
+require 'pg'
+
+
+ DataMapper::Logger.new(STDOUT, :debug)
+ DataMapper.setup(:default, 'postgres://readgs:readme@localhost/geekspeak_production')
+
+class Show
+ include DataMapper::Resource
+ property :id, Serial
+ property :title, String
+ property :abstract, Text
+ property :content, Text
+ property :showtime, DateTime
+ belongs_to :teaser, 'Image',
+ :child_key => [:teaser_id],
+ :required => false
+ has n, :imagesshow, "Images_Shows"
+
+ def to_param
+ self.showtime.strftime("%Y/%m/%d")
+ end
+end
+
+class Image
+ include DataMapper::Resource
+ property :id, Serial
+ property :name, String
+ property :data, Binary, :lazy => true
+
+ has n, :teaseshow, 'Show',
+ :parent_key => [ :id ], # local to this model (Blog)
+ :child_key => [ :teaser_id ] # in the remote model (Post)
+ has n, :imagesshow, 'Images_Shows'
+
+end
+
+class Images_Shows
+ include DataMapper::Resource
+ property :show_id, Serial
+ property :image_id, Serial
+ belongs_to :show
+ belongs_to :image
+end
+
+DataMapper.finalize
+
+get '/' do
+ @shows = Show.all(:limit => 400)
+ erb :index
+end
+
+get %r{/shows/(\d\d\d\d/\d\d/\d\d)/teaser.jpg} do
+ @theDate = params[:captures].first
+ @dayStart = DateTime.parse("#{@theDate}T00:00:00")
+ @dayEnd = DateTime.parse("#{@theDate}T24:00:00")
+ @show = Show.first(:showtime => @dayStart..@dayEnd)
+
+ content_type 'text/plain'
+ PGconn.unescape_bytea(@show.teaser.data)
+end
+
+get %r{/shows/(\d\d\d\d/\d\d/\d\d)/(.*\.jpg)} do
+ @theDate = params[:captures].first
+ @dayStart = DateTime.parse("#{@theDate}T00:00:00")
+ @dayEnd = DateTime.parse("#{@theDate}T24:00:00")
+ @show = Show.first(:showtime => @dayStart..@dayEnd)
+ @image = @show.imagesshow.images.first(:name=>params[:captures].last)
+ content_type 'image/jpg'
+ PGconn.unescape_bytea(@image.data)
+
+end
+
+get %r{/shows/(\d\d\d\d/\d\d/\d\d)/} do
+ @theDate = params[:captures].first
+ @dayStart = DateTime.parse("#{@theDate}T00:00:00")
+ @dayEnd = DateTime.parse("#{@theDate}T24:00:00")
+ @show = Show.first(:showtime => @dayStart..@dayEnd)
+ "Hello, '#{params[:captures].first}'! #{@show.title}"
+ # - #{@show.title}"
+
+end
+
+def mtext_process(text)
+ acronym_matcher= /\[\s?([A-Z]*)\s?\|\s?([\w\s\/]*)\s?\]/
+ link_matcher= /\[([^|\]]*)\|\s?(https*:\/\/[^\]]*)\]/
+
+ text.gsub!(acronym_matcher, '<acronym title="\2">\1</acronym>')
+ text.gsub!(link_matcher, '<a href="\2">\1</a>')
+
+end
+
+def teaser_image(show)
+ if show.teaser_id then
+ return "<img src='/shows/#{show.to_param}/#{show.teaser.name}' />"
+ end
+end
+
@@ -0,0 +1,23 @@
+Going to step through the shows...
+<hr>
+<% if @shows then%>
+ <% @shows.each do |show| %>
+ <h3><%= show.title %></h3>
+ <p><!--%= show.abstract %--></p>
+
+ <p><%= mtext_process(show.abstract) %></p>
+ <pre>'<%= show.to_param %>'</pre>
+ <%= teaser_image(show) %>
+ <ul>
+ <% show.imagesshow.images.each do |img| %>
+ <li><%= img.name%> <img src='/shows/<%=show.to_param%>/<%= img.name%>' />"</li>
+ <% end %>
+ </ul>
+ <p><%= mtext_process(show.content) unless show.content.nil? %></p>
+ <% end %>
+<% else %>
+ Shows is empty - or null
+<% end %>
+
+
+<hr>

0 comments on commit 058e989

Please sign in to comment.