2023-03-18 11:35:52 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-05 10:11:17 -05:00
|
|
|
require 'fileutils'
|
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
class StageManager
|
2023-03-18 11:35:52 -04:00
|
|
|
# Controller to handle top-level video routes
|
2023-03-09 21:37:33 -05:00
|
|
|
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()
|
2023-03-18 11:35:52 -04:00
|
|
|
erb :'video/list', locals: {
|
|
|
|
title: 'List of videos',
|
|
|
|
videos: videos
|
2023-03-09 21:37:33 -05:00
|
|
|
}
|
|
|
|
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
|
2023-03-18 11:35:52 -04:00
|
|
|
selected_channel = params[:channel].to_i() if params.key?(:channel)
|
2023-03-09 21:37:33 -05:00
|
|
|
|
|
|
|
channels = Channel.all()
|
2023-03-18 11:35:52 -04:00
|
|
|
erb :'video/create', locals: {
|
|
|
|
title: 'Create new video',
|
|
|
|
channels: channels,
|
|
|
|
selected_channel: selected_channel
|
2023-03-09 21:37:33 -05:00
|
|
|
}
|
|
|
|
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)
|
2023-03-18 11:35:52 -04:00
|
|
|
video.ensure_directory_structure()
|
2023-03-09 21:37:33 -05:00
|
|
|
|
|
|
|
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()
|
2023-03-18 11:35:52 -04:00
|
|
|
erb :'video/view', locals: {
|
|
|
|
title: video.name,
|
|
|
|
video: video
|
2023-03-09 21:37:33 -05:00
|
|
|
}
|
|
|
|
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()
|
2023-03-18 11:35:52 -04:00
|
|
|
erb :'video/script', locals: {
|
|
|
|
title: "Script: #{video.name}",
|
|
|
|
video: video
|
2023-03-09 21:37:33 -05:00
|
|
|
}
|
|
|
|
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()
|
2023-03-18 11:35:52 -04:00
|
|
|
erb :'video/edit', locals: {
|
|
|
|
title: "Editing: #{video.name}",
|
|
|
|
video: video,
|
|
|
|
channels: channels
|
2023-03-09 21:37:33 -05:00
|
|
|
}
|
|
|
|
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()
|
2023-03-18 11:35:52 -04:00
|
|
|
attr_to_edit = 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
|
2023-03-18 11:35:52 -04:00
|
|
|
if attr_to_edit == 'serial'
|
2023-03-09 21:37:33 -05:00
|
|
|
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-18 11:35:52 -04:00
|
|
|
video[attr_to_edit.to_sym] = params[:value]
|
|
|
|
video.save_changes()
|
2023-03-05 10:11:17 -05:00
|
|
|
|
2023-03-18 11:35:52 -04:00
|
|
|
return 'success'
|
2023-03-09 21:37:33 -05:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2023-03-18 11:35:52 -04:00
|
|
|
redirect "/video/#{video.id}"
|
2023-03-09 21:37:33 -05:00
|
|
|
end
|
2023-03-18 11:35:52 -04:00
|
|
|
|
2023-03-09 21:37:33 -05:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2023-03-18 11:35:52 -04:00
|
|
|
redirect "/video/#{video.id}"
|
2023-03-09 21:37:33 -05:00
|
|
|
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()
|
2023-03-18 11:35:52 -04:00
|
|
|
erb :'video/edit-script', locals: {
|
|
|
|
title: "Editing script: #{video.name}",
|
|
|
|
video: video
|
2023-03-09 21:37:33 -05:00
|
|
|
}
|
|
|
|
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
|