now faster way to store videos

This commit is contained in:
Harry Bomrah 2015-12-27 02:22:26 +08:00
parent d0124362d1
commit 6c57f18f73
7 changed files with 56 additions and 50 deletions

View File

@ -36,12 +36,12 @@ class Admin::VlogsController < OrbitAdminController
vlog = VLog.where(:uid => params[:id].split("-").last).first rescue nil
if !vlog.nil?
if params[:v_log][:type] == "upload"
dirname = params[:video_dirname]
filename = params[:video_filename]
if params[:video_filename].present?
directory = "public/vlog_temp_files/#{dirname}/#{filename}"
vlog.video = Rails.root.join(directory).open
FileUtils.remove_dir("public/vlog_temp_files/#{dirname}")
if params[:video_temp_id].present?
old_video = vlog.v_log_video
old_video.destroy
video = VLogVideo.find(params[:video_temp_id]) rescue nil
video.v_log_id = vlog.id if !video.nil?
video.save
end
vlog.update_attributes(vlog_params)
vlog.save
@ -60,25 +60,23 @@ class Admin::VlogsController < OrbitAdminController
def create
if params[:v_log][:type] == "upload"
dirname = params[:video_dirname]
filename = params[:video_filename]
directory = "public/vlog_temp_files/#{dirname}/#{filename}"
vlog = VLog.create(vlog_params)
vlog.video = Rails.root.join(directory).open
vlog.save
FileUtils.remove_dir("public/vlog_temp_files/#{dirname}")
video = VLogVideo.find(params[:video_temp_id]) rescue nil
video.v_log_id = vlog.id if !video.nil?
video.save
elsif params[:v_log][:type] == "youtube"
p = vlog_params
p[:youtube_link] = format_url(p[:youtube_link]) if p[:youtube_link].present?
vlog = VLog.create(vlog_params)
vlog = VLog.create(p)
end
redirect_to admin_vlogs_path
end
def clear_temp_dir
dirname = params[:dirname]
directory = "public/vlog_temp_files/#{dirname}"
FileUtils.remove_dir(directory)
video = VLogVideo.find(params[:video_id]) rescue nil
if !video.nil?
video.destroy
end
render :json => {"success" => true}.to_json
end
@ -86,20 +84,21 @@ class Admin::VlogsController < OrbitAdminController
dirname = Digest::MD5.hexdigest(Time.now.to_s)
directory = "public/vlog_temp_files/#{dirname}"
FileUtils.mkdir_p(directory) unless File.exists?(directory)
render :json => {"dirname" => dirname}.to_json
path_to_file = "#{directory}/#{params[:filename]}"
File.open(path_to_file,"w")
video = VLogVideo.new
video.video = Rails.root.join(path_to_file).open
video.save
FileUtils.remove_dir(directory)
render :json => {"video_id" => video.id.to_s}.to_json
end
def upload_temp_file
dirname = params[:dirname]
file = params[:video_file]
name = file.original_filename
directory = "public/vlog_temp_files/#{dirname}"
FileUtils.mkdir_p(directory) unless File.exists?(directory)
path_to_file = "#{directory}/#{name}"
File.open(path_to_file,"ab"){ |f| f.write(file.read) }
video = VLogVideo.find(params[:video_id]) rescue nil
if !video.nil?
File.open(video.video.path,"ab"){ |f| f.write(file.read) }
end
render :json => {"success" => true}.to_json
end

View File

@ -32,8 +32,8 @@ class VlogsController < ApplicationController
def show
params = OrbitHelper.params
vlog = VLog.where(:uid => params[:uid]).first
if vlog.type == "upload" && (!vlog.video.url.nil? || vlog.video.url != "")
video = "<video src='#{vlog.video.url}' controls> Your browser does not support the <code>video</code> element.</video>"
if vlog.type == "upload" && !vlog.v_log_video.nil?
video = "<video src='#{vlog.v_log_video.video.url}' controls> Your browser does not support the <code>video</code> element.</video>"
elsif vlog.type == "youtube" && !vlog.youtube_link.nil?
video = "<iframe src='#{vlog.youtube_link}' allowfullscreen frameborder='0'></iframe>"
end

View File

@ -4,21 +4,21 @@ class VLog
include OrbitModel::Impression
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
field :create_user_id, type: BSON::ObjectId
field :title, as: :slug_title, type: String, localize: true
field :subtitle, localize: true
field :type
field :display_in_profile, type: Boolean, default: false
field :youtube_link
field :subtitle, localize: true
field :type
field :display_in_profile, type: Boolean, default: false
field :youtube_link
mount_uploader :image, ImageUploader
mount_uploader :video, AssetUploader
has_many :v_log_files, :dependent => :destroy, :autosave => true
has_one :v_log_video, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :v_log_files, :allow_destroy => true

View File

@ -0,0 +1,8 @@
class VLogVideo
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :video, AssetUploader
belongs_to :v_log
end

View File

@ -55,10 +55,9 @@
<div class="control-group video <%= @vlog.type == "upload" ? "" : "hide" %>" for="upload">
<%= f.label :video, "Video", :class => "control-label muted" %>
<div class="controls">
<input type="hidden" name="video_dirname" id="video_file_temp_dir" />
<input type="hidden" name="video_filename" id="video_file_temp_name" />
<a href="#videoUploader" role="button" class="btn btn-info" data-toggle="modal">Upload Video</a>
<label id="video_file_name"><%= File.basename(@vlog.video.file.path) if !@vlog.new_record? && @vlog.type != "youtube" %></label>
<input type="hidden" name="video_temp_id" id="video_temp_id" />
<a href="#videoUploader" role="button" class="btn btn-info" data-toggle="modal">Upload Video</a>
<label id="video_file_name"><%= File.basename(@vlog.v_log_video.video.file.path) if !@vlog.new_record? && @vlog.type != "youtube" %></label>
</div>
</div>
<div class="control-group video <%= @vlog.type == "upload" ? "" : "hide" %>" for="upload">

View File

@ -11,8 +11,8 @@
<% @vlogs.each do |vlog| %>
<tr>
<td>
<% if (!vlog.video.url.nil? || vlog.video.url != "") && vlog.type == "upload" %>
<div class="video-thumbnail" data-video-url="http://<%= request.host_with_port %>/<%= vlog.video.url %>" data-video-type="upload" data-video-title="<%= vlog.title %>" >
<% if !vlog.v_log_video.nil? && vlog.type == "upload" %>
<div class="video-thumbnail" data-video-url="http://<%= request.host_with_port %>/<%= vlog.v_log_video.video.url %>" data-video-type="upload" data-video-title="<%= vlog.title %>" >
<img src="<%= vlog.thumbnail %>" />
<div class="video-thumbnail-overlay"></div>
<i class="icons-play"></i>
@ -26,7 +26,7 @@
<% end %>
</td>
<td>
<%= vlog.category.title %>
<%= vlog.category.title rescue "" %>
</td>
<td>
<%= vlog.title %>

View File

@ -59,7 +59,7 @@
</div>
<script type="text/javascript">
var progressbar = $("#videoUploader .progress div.bar"),
server_dirname = "",
video_temp_id = "",
uploadBtn = $("#videoUploader #video-upload-btn"),
closeBtn = $("#videoUploader button[data-dismiss=modal]"),
abortUploadBtn = $("#videoUploader #video-upload-abort-btn"),
@ -80,11 +80,11 @@
$.ajax({
url : "/admin/vlogs/get_temp_dir_name",
dataType : "json",
type : "get"
type : "get",
data : {"filename" : uploadData.files[0].name}
}).done(function(data){
server_dirname = data.dirname;
$("#video_file_temp_dir").val(server_dirname);
$("#video_file_temp_name").val(uploadData.files[0].name);
video_temp_id = data.video_id;
$("#video_temp_id").val(video_temp_id);
videoUploadInstance = uploadData.submit()
.error(function (jqXHR, textStatus, errorThrown) {
videoUploadInput.slideDown();
@ -96,7 +96,7 @@
url : "/admin/vlogs/clear_temp_dir",
dataType : "json",
type : "post",
data : {dirname : server_dirname}
data : {video_id : video_temp_id}
})
if (errorThrown === 'abort') {
notificationArea.addClass("error").text("File upload aborted.");
@ -108,7 +108,7 @@
})
}
}).bind('fileuploadsubmit',function (e,data) {
data.formData = {"dirname" : server_dirname};
data.formData = {video_id : video_temp_id};
closeBtn.addClass("hide");
abortUploadBtn.removeClass("hide");
uploadBtn.addClass("hide");