2023-03-05 10:11:17 -05:00
|
|
|
require 'fileutils'
|
|
|
|
|
2023-03-03 14:19:20 -05:00
|
|
|
namespace '/video' do
|
|
|
|
|
|
|
|
get '' do
|
|
|
|
redirect '/video/list'
|
|
|
|
end
|
|
|
|
get '/list' do
|
|
|
|
videos = Video.reverse(:updated_at).all()
|
|
|
|
erb :'video/list', :locals => {
|
|
|
|
:title => 'List of videos',
|
|
|
|
:videos => videos
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/create' do
|
2023-03-06 23:44:51 -05:00
|
|
|
# check if there's a channel specified
|
|
|
|
selected_channel = false
|
|
|
|
if params.has_key?(:channel)
|
|
|
|
selected_channel = params[:channel].to_i()
|
|
|
|
end
|
|
|
|
|
2023-03-03 14:19:20 -05:00
|
|
|
channels = Channel.all()
|
|
|
|
erb :'video/create', :locals => {
|
|
|
|
:title => 'Create new video',
|
2023-03-06 23:44:51 -05:00
|
|
|
:channels => channels,
|
|
|
|
:selected_channel => selected_channel
|
2023-03-03 14:19:20 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
post '/create' do
|
2023-03-03 23:22:17 -05:00
|
|
|
channel = Channel.where(id: params[:video_channel]).first()
|
|
|
|
video_serial = params[:video_serial].to_s.rjust(4, '0')
|
|
|
|
video_path = File.join(
|
|
|
|
channel.directory_path,
|
2023-03-06 15:59:09 -05:00
|
|
|
'Main',
|
2023-03-03 23:22:17 -05:00
|
|
|
"##{video_serial} - #{params[:video_name]}"
|
|
|
|
)
|
|
|
|
|
2023-03-03 14:19:20 -05:00
|
|
|
video = Video.create(
|
|
|
|
serial: params[:video_serial],
|
|
|
|
name: params[:video_name],
|
|
|
|
channel_id: params[:video_channel],
|
2023-03-03 23:22:17 -05:00
|
|
|
directory_path: video_path,
|
2023-03-04 11:14:54 -05:00
|
|
|
description: params[:video_description],
|
|
|
|
script: "# Introduction\n\n# Body\n\n# Conclusions"
|
2023-03-03 14:19:20 -05:00
|
|
|
)
|
2023-03-03 23:22:17 -05:00
|
|
|
|
2023-03-06 15:59:09 -05:00
|
|
|
# create supporting directory structure
|
2023-03-03 23:22:17 -05:00
|
|
|
Dir.mkdir(video_path)
|
2023-03-06 15:59:09 -05:00
|
|
|
video.ensureDirectoryStructure()
|
2023-03-03 23:22:17 -05:00
|
|
|
|
2023-03-03 14:19:20 -05:00
|
|
|
redirect "/video/#{video.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/:video_id' do
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
erb :'video/view', :locals => {
|
|
|
|
:title => video.name,
|
|
|
|
:video => video
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-03-04 11:37:53 -05:00
|
|
|
get '/:video_id/script' do
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
erb :'video/script', :locals => {
|
|
|
|
:title => "Script: #{video.name}",
|
|
|
|
:video => video
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-03-04 08:47:07 -05:00
|
|
|
get '/:video_id/edit' do
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
channels = Channel.all()
|
|
|
|
erb :'video/edit', :locals => {
|
|
|
|
:title => "Editing: #{video.name}",
|
|
|
|
:video => video,
|
|
|
|
:channels => channels
|
|
|
|
}
|
|
|
|
end
|
|
|
|
post '/:video_id/edit' do
|
|
|
|
channel = Channel.where(id: params[:video_channel]).first()
|
|
|
|
video_serial = params[:video_serial].to_s.rjust(4, '0')
|
|
|
|
video_path = File.join(
|
|
|
|
channel.directory_path,
|
2023-03-06 15:59:09 -05:00
|
|
|
'Main',
|
2023-03-04 08:47:07 -05:00
|
|
|
"##{video_serial} - #{params[:video_name]}"
|
|
|
|
)
|
|
|
|
|
|
|
|
# find video and temporarily save the old video path
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
old_path = video.directory_path
|
|
|
|
|
|
|
|
# edit video attributes
|
|
|
|
video.update(
|
|
|
|
name: params[:video_name],
|
|
|
|
serial: params[:video_serial],
|
|
|
|
channel_id: params[:video_channel],
|
|
|
|
directory_path: video_path,
|
|
|
|
description: params[:video_description]
|
|
|
|
)
|
|
|
|
|
|
|
|
# rename the video project directory
|
|
|
|
File.rename(old_path, video_path)
|
|
|
|
|
|
|
|
# redirect the user
|
|
|
|
redirect "/video/#{video.id}"
|
|
|
|
end
|
|
|
|
|
2023-03-06 17:59:58 -05:00
|
|
|
post '/:video_id/edit/:attr' do
|
|
|
|
# find video and temporarily save the old video path
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
attrToEdit = params[:attr]
|
|
|
|
|
|
|
|
video[attrToEdit.to_sym] = params[attrToEdit]
|
|
|
|
video.save()
|
|
|
|
|
|
|
|
return "success"
|
|
|
|
end
|
|
|
|
|
2023-03-07 00:07:29 -05:00
|
|
|
get '/:video_id/archive' do
|
|
|
|
# find the video
|
2023-03-05 10:11:17 -05:00
|
|
|
video = Video.where(id: params[:video_id]).first()
|
2023-03-07 00:07:29 -05:00
|
|
|
video_base = File.basename(video.directory_path)
|
2023-03-05 10:11:17 -05:00
|
|
|
|
2023-03-07 00:07:29 -05:00
|
|
|
# move project to channel's archive
|
|
|
|
archive_dir = File.join(
|
|
|
|
video.channel.directory_path,
|
|
|
|
'Archive',
|
|
|
|
video_base
|
|
|
|
)
|
|
|
|
FileUtils.mv(video.directory_path, archive_dir)
|
|
|
|
|
|
|
|
# mark the video as archived and update directory
|
|
|
|
video.update(
|
|
|
|
directory_path: archive_dir,
|
|
|
|
archived: true
|
|
|
|
)
|
|
|
|
|
|
|
|
redirect '/video/' + video.id.to_s()
|
|
|
|
end
|
|
|
|
get '/:video_id/unarchive' do
|
|
|
|
# find the video
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
video_base = File.basename(video.directory_path)
|
|
|
|
|
|
|
|
# move project to channel's archive
|
|
|
|
active_dir = File.join(
|
|
|
|
video.channel.directory_path,
|
|
|
|
'Main',
|
|
|
|
video_base
|
|
|
|
)
|
|
|
|
FileUtils.mv(video.directory_path, active_dir)
|
|
|
|
|
|
|
|
# mark the video as archived and update directory
|
|
|
|
video.update(
|
|
|
|
directory_path: active_dir,
|
|
|
|
archived: false
|
|
|
|
)
|
|
|
|
|
|
|
|
redirect '/video/' + video.id.to_s()
|
2023-03-05 10:11:17 -05:00
|
|
|
end
|
|
|
|
|
2023-03-04 11:37:53 -05:00
|
|
|
get '/:video_id/edit/script' do
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
erb :'video/edit-script', :locals => {
|
|
|
|
:title => "Editing script: #{video.name}",
|
|
|
|
:video => video
|
|
|
|
}
|
|
|
|
end
|
|
|
|
post '/:video_id/edit/script' do
|
|
|
|
# find video and temporarily save the old video path
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
|
|
|
|
# edit video attributes
|
|
|
|
video.update(
|
|
|
|
script: params[:video_script]
|
|
|
|
)
|
|
|
|
|
|
|
|
# redirect the user
|
|
|
|
redirect "/video/#{video.id}"
|
|
|
|
end
|
|
|
|
|
2023-03-03 14:19:20 -05:00
|
|
|
end
|