# frozen_string_literal: true require 'fileutils' class StageManager # Controller to handle top-level video routes class VideoController 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 # check if there's a channel specified selected_channel = params[:channel].to_i() if params.key?(:channel) 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.ensure_directory_structure() 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 get '/:video_id/script' do video = Video.where(id: params[:video_id]).first() erb :'video/script', locals: { title: "Script: #{video.name}", video: video } end 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 # 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 post '/:video_id/edit/:attr' do # find video and temporarily save the old video path video = Video.where(id: params[:video_id]).first() attr_to_edit = params[:attr] # if we update the video's serial, we need to also update the directory path if attr_to_edit == '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 video[attr_to_edit.to_sym] = params[:value] video.save_changes() 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}" 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}" end 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 end end