Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial working implementation of hid.im

  • Loading branch information...
commit a3bc2ffce4dc2901319293df9e894e6ddb1a0dba 1 parent 2313571
@mnutt authored
View
2  .gitignore
@@ -9,7 +9,7 @@ config/database.yml
config/development.sphinx.conf
config/test.sphinx.conf
private/*.txt
-public/logos/*
+public/system/*
db/*.sqlite3
db/sphinx/*
.#*
View
6 .gitmodules
@@ -0,0 +1,6 @@
+[submodule "vendor/plugins/paperclip"]
+ path = vendor/plugins/paperclip
+ url = git://github.com/thoughtbot/paperclip.git
+[submodule "vendor/plugins/png"]
+ path = vendor/plugins/png
+ url = git://github.com/seattlerb/png.git
View
18 app/controllers/piings_controller.rb
@@ -1,2 +1,20 @@
class PiingsController < ApplicationController
+ def new
+ @piing = Piing.new
+ end
+
+ def create
+ @piing = Piing.create( params[:piing] )
+ redirect_to @piing
+ end
+
+ def show
+ @piing = Piing.find params[:id]
+ end
+
+ def index
+ @piing = Piing.new
+
+ @piings = Piing.find(:all, :limit => 10, :order => "created_at DESC")
+ end
end
View
45 app/models/piing.rb
@@ -1,2 +1,47 @@
+require 'png'
+require 'png/reader'
+require 'paperclip_file'
+
class Piing < ActiveRecord::Base
+ has_attached_file :png
+ has_attached_file :torrent
+
+ before_save :convert_to_png
+
+ def convert_to_png
+ content = self.torrent.queued_for_write[:original].read
+ decimal = content.unpack("C*")
+
+ font_width = 5
+ height = 30
+ width = decimal.size / 3 / height + 1
+
+ canvas = PNG::Canvas.new(width + font_width + 2, height + 2, PNG::Color::Black)
+ pixels = decimal.in_groups_of(3)
+ lines = pixels.in_groups_of(height)
+
+ lines.each_with_index do |line, i|
+ line.each_with_index do |pixel, j|
+ pixel ||= []
+ pixel[0] ||= 0
+ pixel[1] ||= 0
+ pixel[2] ||= 0
+ canvas[i + font_width + 1, j + 1] = PNG::Color.new(pixel[0], pixel[1], pixel[2], 255)
+ end
+ end
+
+ logo_path = File.join(File.dirname(__FILE__), '..', '..', 'public', 'images', 'hidim.png')
+ logo = PNG.load_file logo_path
+
+ canvas.composite logo, 1, 1
+
+ png = PNG.new canvas
+ pngfile = PaperClipFile.new
+ pngfile.original_filename = "torrent.png"
+ tmpfile = Tempfile.new('torrent')
+ tmpfile.write(png.to_blob)
+ pngfile.to_tempfile = tmpfile
+
+ self.attachment_for(:png).assign(pngfile)
+ end
end
View
44 app/views/layouts/application.html.erb
@@ -0,0 +1,44 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <title>
+ hid.im
+ </title>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+ <meta name="description" content="Encode your torrents to images"/>
+
+ <%= stylesheet_link_tag "application" -%>
+
+ <!--[if IE 6]>
+ <%= stylesheet_link_tag 'base_ie6', 'application_ie6' -%>
+ <![endif]-->
+ <!--[if IE 7]>
+ <%= stylesheet_link_tag 'base_ie7', 'application_ie7' -%>
+ <![endif]-->
+
+ <%= javascript_include_tag 'jquery',
+ :cache => 'jquery-all' -%>
+</head>
+
+<body>
+
+ <h1>hid.im</h1>
+
+ <div id="content">
+
+ <% unless flash[:notice].blank? -%>
+ <div id="notice">
+ <% if Array === flash[:notice] -%>
+ <%= flash[:notice].map{|line| "<p>#{line}</p>"} -%>
+ <% else -%>
+ <%= flash[:notice] -%>
+ <% end -%>
+ </div>
+ <% end -%>
+
+ <%= yield %>
+
+ </div>
+
+</body>
+</html>
View
4 app/views/piings/_form.html.erb
@@ -0,0 +1,4 @@
+<% form_for @piing, :html => { :multipart => true } do |form| %>
+ <%= form.file_field :torrent %>
+ <%= submit_tag "Submit" -%>
+<% end %>
View
14 app/views/piings/index.html.erb
@@ -0,0 +1,14 @@
+<div id="create">
+ <h2>Make:</h2>
+ <%= render :partial => 'form' -%>
+</div>
+
+<div id="recent">
+ <h2>Sample hidim'd torrents</h2>
+ <ul>
+ <% @piings.each do |piing| -%>
+ <li><%= image_tag piing.png.url -%></li>
+ <% end -%>
+ </ul>
+</div>
+
View
1  app/views/piings/show.html.erb
@@ -0,0 +1 @@
+<%= image_tag @piing.png.url -%>
View
42 config/routes.rb
@@ -1,45 +1,5 @@
ActionController::Routing::Routes.draw do |map|
map.resources :piings
- # The priority is based upon order of creation: first created -> highest priority.
-
- # Sample of regular route:
- # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # map.resources :products
-
- # Sample resource route with options:
- # map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
-
- # Sample resource route with sub-resources:
- # map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
-
- # Sample resource route with more complex sub-resources
- # map.resources :products do |products|
- # products.resources :comments
- # products.resources :sales, :collection => { :recent => :get }
- # end
-
- # Sample resource route within a namespace:
- # map.namespace :admin do |admin|
- # # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
- # admin.resources :products
- # end
-
- # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
- # map.root :controller => "welcome"
-
- # See how all your routes lay out with "rake routes"
-
- # Install the default routes as the lowest priority.
- # Note: These default routes make all actions in every controller accessible via GET requests. You should
- # consider removing the them or commenting them out if you're using named routes and resources.
- map.connect ':controller/:action/:id'
- map.connect ':controller/:action/:id.:format'
+ map.root :controller => 'piings', :action => 'index'
end
View
23 db/migrate/20090621160442_add_attachments_torrent_and_png_to_piing.rb
@@ -0,0 +1,23 @@
+class AddAttachmentsTorrentAndPngToPiing < ActiveRecord::Migration
+ def self.up
+ add_column :piings, :torrent_file_name, :string
+ add_column :piings, :torrent_content_type, :string
+ add_column :piings, :torrent_file_size, :integer
+ add_column :piings, :torrent_updated_at, :datetime
+ add_column :piings, :png_file_name, :string
+ add_column :piings, :png_content_type, :string
+ add_column :piings, :png_file_size, :integer
+ add_column :piings, :png_updated_at, :datetime
+ end
+
+ def self.down
+ remove_column :piings, :torrent_file_name
+ remove_column :piings, :torrent_content_type
+ remove_column :piings, :torrent_file_size
+ remove_column :piings, :torrent_updated_at
+ remove_column :piings, :png_file_name
+ remove_column :piings, :png_content_type
+ remove_column :piings, :png_file_size
+ remove_column :piings, :png_updated_at
+ end
+end
View
10 db/schema.rb
@@ -9,11 +9,19 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090621160009) do
+ActiveRecord::Schema.define(:version => 20090621160442) do
create_table "piings", :force => true do |t|
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "torrent_file_name"
+ t.string "torrent_content_type"
+ t.integer "torrent_file_size"
+ t.datetime "torrent_updated_at"
+ t.string "png_file_name"
+ t.string "png_content_type"
+ t.integer "png_file_size"
+ t.datetime "png_updated_at"
end
end
View
7 lib/paperclip_file.rb
@@ -0,0 +1,7 @@
+class PaperClipFile
+ attr_accessor :to_tempfile
+
+ attr_accessor_with_default :original_filename, ""
+ attr_accessor_with_default :content_type, ""
+ attr_accessor_with_default :size, 0
+end
View
BIN  public/images/background.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/images/hidim.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/images/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/images/pngcc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/images/repeat.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
23 public/stylesheets/application.css
@@ -0,0 +1,23 @@
+body {
+ background: #333 url('../images/repeat.png') top left repeat-y;
+ margin: 0;
+ color: #FFF;
+}
+#recent ul li {
+ list-style: none;
+}
+#recent ul {
+ margin: 0;
+ padding: 0;
+ text-indent: 0;
+}
+h1 {
+ width: 224px;
+ height: 64px;
+ text-indent: -10000px;
+ background: url('../images/background.png') top left no-repeat;
+ margin: 0 0;
+}
+#content {
+ margin-left: 250px;
+}
1  vendor/plugins/paperclip
@@ -0,0 +1 @@
+Subproject commit 121609fab38f2aba5ac3cbe0d6b26fd664ee3090
1  vendor/plugins/png
@@ -0,0 +1 @@
+Subproject commit 16d3e5a8b9058b97e755e9a521218a5a4514b3d1
Please sign in to comment.
Something went wrong with that request. Please try again.