Trên GitHub, transloadit/rails-sdk đã đạt 47 sao, ngôn ngữ Ruby. Transloadit's official Rails SDK, maintained by the community
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
A Rails Integration for Transloadit's file uploading and encoding service
Intro
Transloadit is a service that helps you handle file uploads, resize, crop and watermark your images, make GIFs, transcode your videos, extract thumbnails, generate audio waveforms, and so much more. In short, Transloadit is the Swiss Army Knife for your files.
This is a Rails SDK to make it easy to talk to the Transloadit REST API.
This gem provides browser integration. If you're looking to integrate Transloadit from your own serverside Ruby code checkout the ruby-sdk.
Install
$ gem install transloadit-rails
or add the transloadit-rails Gem to your Rails project's Gemfile and update your bundle.
After installation you need to run the transloadit:install generator to complete the setup.
$ rails g transloadit:install
Configuration
Edit config/transloadit.yml. It has an auth section for your transloadit
credentials and a templates section to define or refer to existing
templates. It is highly recommended to enable authentication and signing for
the upload forms.
auth:
key : 'TRANSLOADIT_KEY'
secret : 'TRANSLOADIT_SECRET' # optional, but highly recommended
duration: 1800 # 30 minute validity period for signed upload forms
templates:
# template identified by template_id
s3_store: 'YOUR_TEMPLATE_ID'
# template defined inline
image_resize:
steps:
resize:
robot : '/image/resize'
width : 320
height: 200
Configuration by Environment
The transloadit configurations can be further broken up by environment tags to
match the environments used by the application (i.e. development, test,
production).
Please note, the environment tags must match the application's environments
exactly in order to be used.
This requires jQuery, and loads the Transloadit jQuery plugin. Be sure to exclude the <%= javascript_include_tag '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js' %> tag if you already have jQuery loaded.
JavaScript also ensures your form is encoded as multipart/form-data.
If you want to use the automatic transloadit parameter decoding, you have to include
the Transloadit::Rails::ParamsDecoder module into your controller
class YourController
include Transloadit::Rails::ParamsDecoder
end
That way the param[:transloadit] is automatically decoded for you, if it exists.
ParamsDecoder only decodes. The data comes from the visitor's browser and is not
authenticated, so do not use it to decide anything you would not let a visitor decide.
Verifying notifications
When Transloadit POSTs the Assembly status to your notify_url, verify its signature
before acting on it. Include Transloadit::Rails::NotificationVerifier in the controller
that receives notifications:
class TransloaditNotificationsController < ApplicationController
include Transloadit::Rails::NotificationVerifier
# Transloadit cannot send your CSRF token. On Rails 3, use skip_before_filter.
skip_before_action :verify_authenticity_token
def create
status = transloadit_notification
# Look up your record by status[:assembly_id] and update it idempotently.
head :ok
end
end
It checks the signature over the raw transloadit form field with the Auth Secret
from config/transloadit.yml before any action runs. Notifications that are missing
a signature, are malformed or fail verification get an empty 401 Unauthorized.
A missing Auth Secret raises Transloadit::Exception::MissingSecret as a server error,
because it is a configuration problem. The verified status is available as
transloadit_notification, with indifferent access.
This requires the transloadit gem 4.1.0 or newer; with an older version, the
verification raises NotImplementedError explaining the requirement.
Keep the controller separate from the actions that receive your upload form:
browser form submissions carry no notification signature and would be rejected.
Including ParamsDecoder in the same controller is safe, since verification reads the
raw form field rather than the decoded params[:transloadit].
If notifications are signed with another Auth Key than the one in your configuration,
override transloadit_notification_client to return a client with that key's secret:
def transloadit_notification_client
Transloadit.new(key: ENV["TRANSLOADIT_KEY"], secret: ENV["TRANSLOADIT_SECRET"])
end
Pick the secret from your own configuration, never from the notification itself,
which is not trusted until it has been verified.
Transloadit retries failed notifications, and replayed notifications are signed and
verified the same way. A valid signature does not prevent duplicate deliveries, so
make your handling idempotent, for example by keying it on the Assembly ID.
Note: Since it's still very young, the Transloadit Rails SDK does not include batteries for it yet, but
if you're looking for a jQuery-less integration, check out Uppy, our next-gen file uploader for the web.
Tutorial
In this tutorial, you are going to learn how to use transloadit in a freshly
setup rails project.
If you haven't already done so, go ahead and install Rails.
$ gem install rails
With rails installed, let's create a new app called 'my_app'.
$ rails new my_app
$ cd my_app
In order to use transloadit in this app, we need to add the gem to our Gemfile
and bundle things up.
With that in place, it's time to generate our transloadit configuration, as
well as a basic UploadsController and a dummy Upload model.
$ rails g transloadit:install
$ rails g controller uploads new create
$ rails g model upload
$ rake db:migrate
The controller generator we just executed has probably put two GET routes into
your config/routes.rb. We don't want those, so lets go ahead an overwrite
them with this.
MyApp::Application.routes.draw do
resources :uploads
end
Next we need to configure our config/transloadit.yml file. For this tutorial,
just put in your credentials, and define an image resize step as indicated
below:
auth:
key : '4d2e...'
secret : '8ad1...'
duration: 1800 # 30 minute validity period for signed upload forms
templates:
image_resize:
steps:
resize:
robot : '/image/resize'
format: 'jpg'
width : 320
height: 200
Note that we encourage you to enable authentication in your Transloadit Account
and put your secret into the config/transloadit.yml to have your requests
signed.
Alright, time to create our upload form. In order to do that, please open
app/views/uploads/new.html.erb, and put the following code in:
In order to use the transloadit params in your controller and views you
have to include the ParamsDecoder into your controller. Let's do that for our
UploadsController.
Open up app/controllers/uploads_controller.rb and adapt it like that:
class UploadsController < ApplicationController
include Transloadit::Rails::ParamsDecoder
def new
end
def create
end
end
That's it. If you've followed the steps closely, you should now be able to
try your first upload. Don't forget do start your rails server first:
$ rails server
Then go to http://localhost:3000/uploads/new, and upload an image. If you did
everything right, you should see the uploaded and resized file as soon as the
upload finishes.
Example
An example rails application following the tutorial above can be found in the examples directory.
Testing
RSpec request specs
If you want to test your file uploads without relying on the network (a
good idea to keep them fast and lean) you can include some request spec
helpers to allow you to easily populate the transloadit_params and
params[:transloadit] in your actions.
First, in your spec/spec_helper.rb :
require 'transloadit/rspec/helpers'
Now, in your request spec :
# NOTE: It's important that you don't use :js => true, otherwise your
# test will actually hit out using AJAX, making your test dependent on the
# network.
it "can upload data files", :js => false do
attach_file 'upload_file', Rails.root.join('spec', 'asset', 'example.pdf')
# UploadsController should be replaced with the actual controller
# you're expecting to POST to when the upload is done
stub_transloadit!(UploadsController, example_json)
click_button 'Submit'
end
def example_json
"{ ... JSON content from a real POST ... }"
end
Compatibility
At a minimum, this gem should work on MRI 2.3.0, 2.2.0, 2.1.0, Rubinius, and JRuby.
It may also work on older ruby versions, but support for those Rubies is not guaranteed.
If it doesn't work on one of the officially supported Rubies, please file a bug report.
Compatibility patches for other Rubies are welcome.
Support for EOL'd Ruby 1.9.x and Ruby 2.0 has been dropped, please use version 1.2.0
if you need support for older Ruby versions.
Testing against these versions is performed automatically by GitHub Actions.
Support IE6 or jQuery < 1.9
If you have to support IE6 and use a jQuery version below 1.9, you have to change
the jquery_sdk_version in the config to 'v1.0.0':
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/transloadit/rails-sdk là nguồn chính thức.
transloadit/rails-sdk có phải mã nguồn mở không?
Có — transloadit/rails-sdk phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/transloadit/rails-sdk.
transloadit/rails-sdk có trang demo không?
Dự án có trang chủ ở https://transloadit.com/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
transloadit/rails-sdk dùng license gì?
transloadit/rails-sdk phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
transloadit/rails-sdk là gì?
transloadit/rails-sdk (transloadit/rails-sdk) là dự án Ruby trên GitHub. Theo mô tả gốc: Transloadit's official Rails SDK, maintained by the community
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về rails-sdk?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về rails-sdk.