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

View File

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

View File

@ -16,9 +16,9 @@ class VLog
field :youtube_link field :youtube_link
mount_uploader :image, ImageUploader mount_uploader :image, ImageUploader
mount_uploader :video, AssetUploader
has_many :v_log_files, :dependent => :destroy, :autosave => true 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 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"> <div class="control-group video <%= @vlog.type == "upload" ? "" : "hide" %>" for="upload">
<%= f.label :video, "Video", :class => "control-label muted" %> <%= f.label :video, "Video", :class => "control-label muted" %>
<div class="controls"> <div class="controls">
<input type="hidden" name="video_dirname" id="video_file_temp_dir" /> <input type="hidden" name="video_temp_id" id="video_temp_id" />
<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> <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> <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> </div>
<div class="control-group video <%= @vlog.type == "upload" ? "" : "hide" %>" for="upload"> <div class="control-group video <%= @vlog.type == "upload" ? "" : "hide" %>" for="upload">

View File

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

View File

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