vlog/app/controllers/admin/vlogs_controller.rb

124 lines
3.2 KiB
Ruby

class Admin::VlogsController < OrbitAdminController
def index
tags = @module_app.tags
categories = @module_app.categories.enabled
@filter_fields = filter_fields(categories, tags)
@filter_fields.delete(:status)
@table_fields = ["vlog.thumbnail", :category, :title, "vlog.uploaded_by"]
@vlogs = VLog.all
.order_by(sort)
.with_categories(filters("category"))
.with_tags(filters("tag"))
@vlogs = search_data(@vlogs,[:title]).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def new
@vlog = VLog.new
end
def destroy
vlog = VLog.find(params[:id])
vlog.destroy
redirect_to admin_vlogs_path(:page => params[:page])
end
def edit
@vlog = VLog.find(params[:id]) rescue nil
end
def update
vlog = VLog.where(:uid => params[:id].split("-").last).first rescue nil
if !vlog.nil?
if params[:v_log][:type] == "upload"
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
elsif params[:v_log][:type] == "youtube"
p = vlog_params
p[:youtube_link] = format_url(p[:youtube_link]) if p[:youtube_link].present?
vlog = VLog.update_attributes(vlog_params)
vlog.save
end
end
redirect_to admin_vlogs_path(:page => params[:page])
end
def show
end
def create
if params[:v_log][:type] == "upload"
vlog = VLog.create(vlog_params)
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(p)
end
redirect_to admin_vlogs_path
end
def clear_temp_dir
video = VLogVideo.find(params[:video_id]) rescue nil
if !video.nil?
video.destroy
end
render :json => {"success" => true}.to_json
end
def get_temp_dir_name
dirname = Digest::MD5.hexdigest(Time.now.to_s)
directory = "public/vlog_temp_files/#{dirname}"
FileUtils.mkdir_p(directory) unless File.exists?(directory)
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
file = params[:video_file]
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
private
def vlog_params
p = params.require(:v_log).permit!
p[:create_user_id] = current_user.id if !p[:create_user_id].present?
p
end
def format_url(url)
uri = URI.parse(url)
ps = uri.query.split("&")
url_params = {}
ps.each do |p|
x = p.split("=")
url_params[x.first] = x.last
end
url = "http://www.youtube.com/embed/#{url_params["v"]}"
end
end