From 6c57f18f73a8f8656e81580fd28ef755e8740f04 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Sun, 27 Dec 2015 02:22:26 +0800 Subject: [PATCH] now faster way to store videos --- app/controllers/admin/vlogs_controller.rb | 51 +++++++++---------- app/controllers/vlogs_controller.rb | 4 +- app/models/v_log.rb | 16 +++--- app/models/v_log_video.rb | 8 +++ app/views/admin/vlogs/_form.html.erb | 7 ++- app/views/admin/vlogs/_index.html.erb | 6 +-- .../admin/vlogs/_video_file_uploader.html.erb | 14 ++--- 7 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 app/models/v_log_video.rb diff --git a/app/controllers/admin/vlogs_controller.rb b/app/controllers/admin/vlogs_controller.rb index 22e7cda..c57ed90 100644 --- a/app/controllers/admin/vlogs_controller.rb +++ b/app/controllers/admin/vlogs_controller.rb @@ -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 diff --git a/app/controllers/vlogs_controller.rb b/app/controllers/vlogs_controller.rb index 033893e..1e75858 100644 --- a/app/controllers/vlogs_controller.rb +++ b/app/controllers/vlogs_controller.rb @@ -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 = "" + if vlog.type == "upload" && !vlog.v_log_video.nil? + video = "" elsif vlog.type == "youtube" && !vlog.youtube_link.nil? video = "" end diff --git a/app/models/v_log.rb b/app/models/v_log.rb index 4a7b45c..e5ad398 100644 --- a/app/models/v_log.rb +++ b/app/models/v_log.rb @@ -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 diff --git a/app/models/v_log_video.rb b/app/models/v_log_video.rb new file mode 100644 index 0000000..02f3323 --- /dev/null +++ b/app/models/v_log_video.rb @@ -0,0 +1,8 @@ +class VLogVideo + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :video, AssetUploader + + belongs_to :v_log +end \ No newline at end of file diff --git a/app/views/admin/vlogs/_form.html.erb b/app/views/admin/vlogs/_form.html.erb index 7056563..e0e5cef 100644 --- a/app/views/admin/vlogs/_form.html.erb +++ b/app/views/admin/vlogs/_form.html.erb @@ -55,10 +55,9 @@
" for="upload"> <%= f.label :video, "Video", :class => "control-label muted" %>
- - - Upload Video - + + Upload Video +
" for="upload"> diff --git a/app/views/admin/vlogs/_index.html.erb b/app/views/admin/vlogs/_index.html.erb index f6b0d59..eafc782 100644 --- a/app/views/admin/vlogs/_index.html.erb +++ b/app/views/admin/vlogs/_index.html.erb @@ -11,8 +11,8 @@ <% @vlogs.each do |vlog| %> - <% if (!vlog.video.url.nil? || vlog.video.url != "") && vlog.type == "upload" %> -
+ <% if !vlog.v_log_video.nil? && vlog.type == "upload" %> +
@@ -26,7 +26,7 @@ <% end %> - <%= vlog.category.title %> + <%= vlog.category.title rescue "" %> <%= vlog.title %> diff --git a/app/views/admin/vlogs/_video_file_uploader.html.erb b/app/views/admin/vlogs/_video_file_uploader.html.erb index df3f05f..0756101 100644 --- a/app/views/admin/vlogs/_video_file_uploader.html.erb +++ b/app/views/admin/vlogs/_video_file_uploader.html.erb @@ -59,7 +59,7 @@