125 lines
3.3 KiB
Ruby
125 lines
3.3 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.where(:title.ne => "")
|
||
|
.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"
|
||
|
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}")
|
||
|
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
|
||
|
end
|
||
|
|
||
|
def show
|
||
|
end
|
||
|
|
||
|
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}")
|
||
|
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)
|
||
|
end
|
||
|
redirect_to admin_vlogs_path
|
||
|
end
|
||
|
|
||
|
def clear_temp_dir
|
||
|
dirname = params[:dirname]
|
||
|
directory = "public/vlog_temp_files/#{dirname}"
|
||
|
FileUtils.remove_dir(directory)
|
||
|
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)
|
||
|
render :json => {"dirname" => dirname}.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) }
|
||
|
|
||
|
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
|