upload video sử dụng Ziggeo
Bài đăng này đã không được cập nhật trong 7 năm
Cài đặt Gemfile
gem "Ziggeo"
gem "dotenv-rails"
gem "devise"
Ziggeo là một thư viện cho phép bạn dễ dàng làm việc với API của Ziggeo. Lưu ý rằng tên của nó bắt đầu với một chữ hoa "Z"! Dotenv sẽ được sử dụng để lưu trữ các biến môi trường để phát triển. Chạy các lệnh dưới đây:
bundle install
rails generate devise:install
rails generate devise User
rails generate model Video
rails generate controller Videos
rails db:migrate
config/routes.rb
resources :videos
** Đăng ký tài khoản** Các bạn vào https://ziggeo.com/signup để đăng ký tài khoản Sau khi đăng ký Ziggeo sẽ cung cấp cho bạn:
ZIGGEO_APP_TOKEN: "ea65185f0dde00397d55569028d67227"
ZIGGEO_PRIVATE_KEY: "9f939381106e572894ee4b41c41641de"
ZIGGEO_ENCRYPTION_KEY: "1cce2ff28051521ae0260496c7029e79"
- Thêm thuộc tính cho bảng users
rails g migration add_uid_to_users uid:string
+database db/migrate/20170228093212_devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
db/migrate/20170228093259_add_uid_to_users.rb
class AddUidToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :uid, :string
end
end
db/migrate/20170301013112_create_videos.rb
class CreateVideos < ActiveRecord::Migration[5.0]
def change
create_table :videos do |t|
t.belongs_to :user, foreign_key: true
t.string :uid
t.decimal :duration
t.datetime :ziggeo_created_at
t.boolean :approved
t.timestamps
end
end
end
- controller controller/videos_controller.rb
class VideosController < ApplicationController
def index
ziggeo = Ziggeo.new ENV["ZIGGEO_APP_TOKEN"], ENV["ZIGGEO_PRIVATE_KEY"], ENV["ZIGGEO_ENCRYPTION_KEY"]
@videos = ziggeo.videos.index tags: current_user.uid
end
def destroy
video = current_user.videos.find_by(uid: params[:id])
if video
ziggeo = Ziggeo.new(ENV['ZIGGEO_KEY'], ENV['ZIGGEO_SECRET'], ENV['ZIGGEO_ENCRYPTION'])
ziggeo.videos.delete(video.uid)
flash[:success] = 'Video removed! It may take some time to reflect changes on the website.'
else
flash[:warning] = 'Cannot find such video...'
end
redirect_to root_path
end
end
- View views/videos/index.html.erb
<h1>Videos</h1>
<%= link_to 'Add video', new_video_path %>
<%= render partial: 'video', collection: @videos, as: :video %>
views/videos/_video.html.erb
<div class="card">
<div class="card-block">
<ziggeo ziggeo-video='<%= video['token'] %>' ziggeo-width="320" ziggeo-height="240" ziggeo-popup></ziggeo>
</div>
</div>
views/videos/new.html.erb
<ziggeo ziggeo-limit="60" ziggeo-width="320" ziggeo-height="240"
ziggeo-perms="allowupload" ziggeo-tags="<%= current_user.uid %>"></ziggeo>
- routes config/routes.rb
root "video#index"
resources :videos
- assets/javascripts/videos.coffee
jQuery(document).on 'turbolinks:load', ->
ZiggeoApi.Events.on "upload_progress", ( uploaded, total, data ) ->
$('progress').removeClass('hidden-xs-up').attr 'value', (uploaded / total) * 100
Link tham khảo: https://www.sitepoint.com/video-uploads-with-rails-and-ziggeo/
All rights reserved