Select Git revision
CanvasAnnotationDisplay.test.js
ProjectController.php 6.26 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 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
{
/**
* @Route("/project/create", name="create_project", methods={"POST"})
* @throws ZipArchiveNotOpeningException
*/
public function create(
TranslatorInterface $translator,
Capsule $capsule,
string $video_url
): Response {
chdir('../legacy/');
$capsule_name = $capsule->getName();
$capsule_directory = $capsule->getLinkPath();
if (file_exists($capsule_directory)) {
$this->addFlash(
'warning',
$translator->trans(
'project.already_exists',
[
'capsule_name' => $capsule_name
]
)
);
return $this->redirectToRoute('capsule_list');
}
$this->extractZipArchiveInNewCapsuleDirectory($capsule_directory);
$this->addProjectVideoUrlInXMLProjectFile($capsule_directory, $video_url);
$this->createOrUpdatePasswordFile($capsule_directory, $capsule->getPassword());
$this->addFlash(
'success',
$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(
TranslatorInterface $translator,
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',
$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',
$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 addProjectVideoUrlInXMLProjectFile(string $capsule_directory, string $video_url): void
{
$project_xml_file = $capsule_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, TranslatorInterface $translator): Response
{
$form = $this->createForm(EditVideoUrlFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->addFlash(
'success',
$translator->trans(
'editors.remove.pending_editor.success'
)
);
return $this->redirectToRoute('capsule_list', [
'capsule_id' => $capsule_id
]);
}
return $this->render('project/edit_video_url.html.twig', [
'editVideoUrlForm' => $form->createView(),
// 'capsule_id' => $capsule_id
]);
}
}