Select Git revision
CanvasAnnotationDisplay.js
ProjectController.php 7.45 KiB
<?php
namespace App\Controller;
use App\Entity\Capsule;
use App\Entity\PendingEditorInvitation;
use App\Exception\ZipArchiveNotOpeningException;
use App\Form\EditVideoUrlFormType;
use App\Form\RemoveEditorFormType;
use App\Repository\CapsuleRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\Config\Util\Exception\XmlParsingException;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use ZipArchive;
class ProjectController extends AbstractController
{
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @Route("/project/create", name="create_project", methods={"POST"})
* @throws ZipArchiveNotOpeningException
*/
public function create(
Capsule $capsule,
string $video_url
): Response {
chdir('../legacy/');
$capsule_name = $capsule->getName();
$capsule_directory = $capsule->getLinkPath();
if (file_exists($capsule_directory)) {
$this->addFlash(
'warning',
$this->translator->trans(
'project.already_exists',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
$this->extractZipArchiveInNewCapsuleDirectory($capsule_directory);
$this->setVideoUrlNodeAttributeInXMLProjectFile($capsule_directory, $video_url);
$this->createOrUpdatePasswordFile($capsule_directory, $capsule->getPassword());
$this->addFlash(
'success',
$this->translator->trans(
'capsule.created_success',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
/**
* @Route("/project/duplicate", name="duplicate_project", methods={"POST"})
* @throws ZipArchiveNotOpeningException
*/
public function duplicate(
Capsule $capsule,
string $parent_directory,
Filesystem $file_system
): Response {
chdir('../legacy/');
$capsule_name = $capsule->getName();
$capsule_directory = $capsule->getLinkPath();
if (file_exists($capsule_directory)) {
$this->addFlash(
'warning',
$this->translator->trans(
'project.already_exists',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
$this->extractZipArchiveInNewCapsuleDirectory($capsule_directory);
$this->replaceTheWholeFileDirectoryWithParentOne($file_system, $parent_directory, $capsule_directory);
$this->createOrUpdatePasswordFile($capsule_directory, $capsule->getPassword());
$this->addFlash(
'success',
$this->translator->trans(
'capsule.duplicate.success',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
/**
* @throws ZipArchiveNotOpeningException The archive file doesn't exist.
*/
private function extractZipArchiveInNewCapsuleDirectory(string $capsule_directory): void
{
$zip = new ZipArchive();
$zip_file_archive_is_open = $zip->open("create.zip");
if (! $zip_file_archive_is_open) {
throw new ZipArchiveNotOpeningException("Create Zip Archive could not be open");
}
$zip->extractTo($capsule_directory);
$zip->close();
}
private function setVideoUrlNodeAttributeInXMLProjectFile(string $project_directory, string $video_url): void
{
$project_xml_file = $project_directory . "/file/project.xml";
$xml_file_content = simplexml_load_file($project_xml_file);
if (false === $xml_file_content) {
throw new FileNotFoundException("The XML project file could not be found");
}
/** @phpstan-ignore-next-line */
$xml_file_content->video['url'] = $video_url;
$video_url_XML_tag_is_filled = $xml_file_content->saveXML($project_xml_file);
if (false === $video_url_XML_tag_is_filled) {
throw new XmlParsingException('Video URL could not be written in XML project file');
}
}
private function createOrUpdatePasswordFile(string $capsule_directory, string $password): void
{
$project_password_file = $capsule_directory . "/file/projectPassword.txt";
file_put_contents($project_password_file, $password);
}
private function replaceTheWholeFileDirectoryWithParentOne(
FileSystem $file_system,
string $parent_directory,
string $child_directory
): void {
$file_directory_path = '/file/';
$file_system->mirror(
$parent_directory . $file_directory_path,
$child_directory . $file_directory_path,
null,
['override' => true]
);
}
/**
* @Route("/capsule/{capsule_id}/edit_video_url", name="edit_video_url")
*/
public function editVideoUrl(
int $capsule_id,
Request $request,
CapsuleRepository $capsule_repository
): Response {
$capsule = $capsule_repository->findOneBy(['id' => $capsule_id]);
if (! $capsule instanceof Capsule) {
throw new \Exception('The retrieved capsule is not an instance of Capsule.');
}
$file_system = new Filesystem();
$parent_directory_name = $capsule->getLinkPath();
$parent_directory_exists = $file_system->exists('../legacy/' . $parent_directory_name);
if (! $parent_directory_exists) {
$this->addFlash(
'warning',
$this->translator->trans(
'project.not_exist',
[
'capsule_name' => $capsule->getName()
]
)
);
return $this->redirectToRoute('capsule_list');
}
$form = $this->createForm(EditVideoUrlFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$new_video_url = $form->get('new_video_url')->getData();
$project_directory = $capsule->getLinkPath();
chdir('../legacy/');
$this->setVideoUrlNodeAttributeInXMLProjectFile($project_directory, $new_video_url);
$this->addFlash(
'success',
$this->translator->trans(
'capsule.edit.video_url.success'
)
);
return $this->redirectToRoute('capsule_list', [
'capsule_id' => $capsule_id
]);
}
return $this->render('project/edit_video_url.html.twig', [
'editVideoUrlForm' => $form->createView()
]);
}
}