2023-03-05 10:11:17 -05:00
|
|
|
require 'fileutils'
|
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
class StageManager
|
|
|
|
class VideoController
|
2023-03-03 14:19:20 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
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
|
2023-03-03 14:19:20 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
get '/create' do
|
|
|
|
# check if there's a channel specified
|
|
|
|
selected_channel = false
|
|
|
|
if params.has_key?(:channel)
|
|
|
|
selected_channel = params[:channel].to_i()
|
|
|
|
end
|
|
|
|
|
|
|
|
channels = Channel.all()
|
|
|
|
erb :'video/create', :locals => {
|
|
|
|
:title => 'Create new video',
|
|
|
|
:channels => channels,
|
|
|
|
:selected_channel => selected_channel
|
|
|
|
}
|
|
|
|
end
|
|
|
|
post '/create' 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,
|
|
|
|
'Main',
|
|
|
|
"##{video_serial} - #{params[:video_name]}"
|
|
|
|
)
|
|
|
|
|
|
|
|
video = Video.create(
|
|
|
|
serial: params[:video_serial],
|
|
|
|
name: params[:video_name],
|
|
|
|
channel_id: params[:video_channel],
|
|
|
|
directory_path: video_path,
|
|
|
|
description: params[:video_description],
|
|
|
|
script: "# Introduction\n\n# Body\n\n# Conclusions"
|
|
|
|
)
|
|
|
|
|
|
|
|
# create supporting directory structure
|
|
|
|
Dir.mkdir(video_path)
|
|
|
|
video.ensureDirectoryStructure()
|
|
|
|
|
|
|
|
redirect "/video/#{video.id}"
|
2023-03-06 23:44:51 -05:00
|
|
|
end
|
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
get '/:video_id' do
|
|
|
|
video = Video.where(id: params[:video_id]).first()
|
|
|
|
erb :'video/view', :locals => {
|
|
|
|
:title => video.name,
|
|
|
|
:video => video
|
|
|
|
}
|
|
|
|
end
|
2023-03-03 14:19:20 -05:00
|
|
|
|
2023-03-09 21:37:33 -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-03 14:19:20 -05:00
|
|
|
|
2023-03-09 21:37:33 -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,
|
|
|
|
'Main',
|
|
|
|
"##{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
|
2023-03-04 11:37:53 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
# 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]
|
|
|
|
)
|
2023-03-04 08:47:07 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
# rename the video project directory
|
|
|
|
File.rename(old_path, video_path)
|
2023-03-06 17:59:58 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
# redirect the user
|
|
|
|
redirect "/video/#{video.id}"
|
2023-03-07 11:29:10 -05:00
|
|
|
end
|
|
|
|
|
2023-03-09 21:37:33 -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]
|
2023-03-06 17:59:58 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
# if we update the video's serial, we need to also update the directory path
|
|
|
|
if attrToEdit == 'serial'
|
|
|
|
old_path = video.directory_path
|
|
|
|
new_path = video.directory_path.sub("##{video.serial}", "##{params[:value]}")
|
|
|
|
File.rename(old_path, new_path)
|
|
|
|
video[:directory_path] = new_path
|
|
|
|
end
|
2023-03-06 17:59:58 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
video[attrToEdit.to_sym] = params[:value]
|
|
|
|
video.save()
|
2023-03-05 10:11:17 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
return "success"
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/:video_id/archive' 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
|
|
|
|
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()
|
|
|
|
end
|
2023-03-04 11:37:53 -05:00
|
|
|
|
2023-03-09 21:37:33 -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()
|
2023-03-04 11:37:53 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
# edit video attributes
|
|
|
|
video.update(
|
|
|
|
script: params[:video_script]
|
|
|
|
)
|
2023-03-04 11:37:53 -05:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
# redirect the user
|
|
|
|
redirect "/video/#{video.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2023-03-03 14:19:20 -05:00
|
|
|
end
|