Permalink
Browse files

Now with simple Transloadit support

  • Loading branch information...
1 parent 92ee496 commit 5e0188db07c88690132aa51db236b993716c2491 @joerichsen committed Oct 12, 2010
View
@@ -3,3 +3,4 @@ db/*.sqlite3
log/*.log
tmp/**/*
config/s3.yml
+config/transloadit.yml
View
25 README
@@ -1,6 +1,31 @@
A sample Rails 3 app using Transloadit, Paperclip and S3 to manage images.
Instructions
+
- check out the project
+
- cp config/s3.yml.example config/s3.yml and add your Amazon credentials
+- Goto https://transloadit.com/templates/add and put in something like this
+
+ {
+ "steps":
+ { "store":
+ { "robot": "/s3/store"
+ , "bucket": "transloadit-paperclip-example"
+ , "path": "avatars/${unique_prefix}/original/${file.urlName}"
+ , "key": "YOURSECRETACCESSID"
+ , "secret": " YOUR-secRE/TACCe\ssKEy"
+ }
+ }
+ }
+
+- save the template and copy the template id
+
+- cp config/transloadit.yml.example config/transloadit.yml and add the template
+ id you just created and your Auth Key found on this page
+ https://transloadit.com/accounts/credentials
+
+TODO
+- add resizing examples
+- add authentication
@@ -6,11 +6,24 @@ def index
def new
@user = User.new
+ @transloadit_params = {
+ "auth" => { "key" => TRANSLOADIT[:auth_key] },
+ "template_id" => TRANSLOADIT[:user_avatar_template_id],
+ "redirect_url" => users_url
+ }
end
def create
@user = User.new(params[:user])
+ # Fetch the avatar info
+ avatar = ActiveSupport::JSON.decode(params[:transloadit]).symbolize_keys[:uploads].first.symbolize_keys
+ @user.update_attributes(
+ :avatar_file_name => avatar[:name],
+ :avatar_content_type => avatar[:mime],
+ :avatar_file_size => avatar[:size],
+ :avatar_unique_prefix => avatar[:id].insert(2, '/'))
+
if @user.save
redirect_to users_path, :notice => 'User was successfully created'
else
View
@@ -8,6 +8,7 @@ class User < ActiveRecord::Base
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style/:basename.:extension",
- :bucket => 'transloadit-paperclip-example'
+ :bucket => 'transloadit-paperclip-example',
+ :path => ":attachment/:unique_prefix/:style/:basename.:extension"
end
@@ -3,12 +3,13 @@
<head>
<title>TransloaditPaperclipExample</title>
<%= stylesheet_link_tag :all %>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
-<%= yield %>
+ <%= yield %>
</body>
</html>
@@ -1,12 +1,25 @@
<h1>Create new user</h1>
-<% form_for(@user, :html => { :multipart => true }) do |f| %>
+<%= form_tag('http://api2.transloadit.com/assemblies', :multipart => true, :id => 'new_user_form') do %>
+ <%= hidden_field_tag :params, @transloadit_params.to_json %>
<p>
- <%= f.label :avatar %><br />
- <%= f.file_field :avatar %>
+ <%= label_tag :avatar %><br />
+ <%= file_field_tag :avatar %>
</p>
<p>
- <%= f.submit %> or <%= link_to 'cancel', users_path %>
+ <%= submit_tag %> or <%= link_to 'cancel', users_path %>
</p>
<% end %>
+<script type="text/javascript">
+var tlProtocol = (('https:' == document.location.protocol) ? 'https://' : 'http://');
+document.write(unescape("%3Cscript src='" + tlProtocol + "assets.transloadit.com/js/jquery.transloadit2.js' type='text/javascript'%3E%3C/script%3E"));
+</script>
+<script type="text/javascript">
+ // We call .transloadit() after the DOM is initialized:
+ $(document).ready(function() {
+ $('#new_user_form').transloadit();
+ });
+</script>
+
+
@@ -0,0 +1,4 @@
+Paperclip.interpolates :unique_prefix do |attachment, style|
+ attachment.instance.send("#{attachment.name}_unique_prefix")
+end
+
@@ -0,0 +1 @@
+S3_CREDENTIALS = YAML.load_file("#{Rails.root}/config/s3.yml").symbolize_keys
@@ -0,0 +1 @@
+TRANSLOADIT = YAML.load_file("#{Rails.root}/config/transloadit.yml").symbolize_keys
@@ -0,0 +1,2 @@
+auth_key: 49e8fe5277649af761a9172ca575c22c
+user_avatar_template_id: d5a98733c464f26f9c4e98789b0f435d
@@ -6,6 +6,7 @@ def self.up
t.string :avatar_content_type
t.integer :avatar_file_size
t.datetime :avatar_updated_at
+ t.string :avatar_unique_prefix
t.timestamps
end
View
@@ -17,6 +17,7 @@
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
+ t.string "avatar_unique_prefix"
t.datetime "created_at"
t.datetime "updated_at"
end
Oops, something went wrong.

0 comments on commit 5e0188d

Please sign in to comment.