Skip to content

Commit 2626d00

Browse files
committed
Add page to upload a media for an article.
This way we support images again.
1 parent ee67140 commit 2626d00

File tree

6 files changed

+247
-0
lines changed

6 files changed

+247
-0
lines changed

src/Controller/MediaController.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
namespace App\Controller;
3+
4+
use App\Entity\Article;
5+
use App\Entity\Comment;
6+
use App\Form\ArticleType;
7+
use App\Form\MediaUploadType;
8+
use App\Handler\CommentHandler;
9+
use App\Handler\ContactHandler;
10+
use App\Handler\SubscriptionHandler;
11+
use App\Manager\ArticleManager;
12+
use App\Model\MediaUpload;
13+
use Exception;
14+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
15+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\Routing\Annotation\Route;
19+
20+
/**
21+
* @Route("/media")
22+
* @Security("is_granted('ROLE_SUPER_ADMIN')")
23+
*/
24+
class MediaController extends AbstractController
25+
{
26+
/**
27+
* @Route("/article/{url}/new", name="media_article_new")
28+
*
29+
* @param Request $request The request.
30+
* @param Article $article The article.
31+
* @param ArticleManager $articleManager The article manager.
32+
*
33+
* @return Response
34+
* @throws Exception
35+
*/
36+
public function articleNewAction(Request $request, Article $article, ArticleManager $articleManager): Response
37+
{
38+
$mediaUpload = new MediaUpload();
39+
$mediaUpload->setArticle($article);
40+
$form = $this->createForm(MediaUploadType::class, $mediaUpload);
41+
42+
$form->handleRequest($request);
43+
if ($form->isSubmitted()) {
44+
if ($form->isValid()) {
45+
$mediaUpload->getFile()->move(
46+
$articleManager->getArticleBasePath() . $article->getDirectory(),
47+
$mediaUpload->getName()
48+
);
49+
50+
$this->addFlash('success', 'The media has been uploaded successfully.');
51+
52+
return $this->redirectToRoute('article_edit', [
53+
'url' => $article->getUrl(),
54+
]);
55+
}
56+
}
57+
58+
return $this->render('app/media/article_new.html.twig', [
59+
'form' => $form->createView(),
60+
'article' => $article,
61+
]);
62+
}
63+
}

src/Form/MediaUploadType.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Form;
4+
5+
use App\Entity\Article;
6+
use App\Entity\Theme;
7+
use App\Model\MediaUpload;
8+
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
9+
use Symfony\Component\Form\AbstractType;
10+
use Symfony\Component\Form\Extension\Core\Type\FileType;
11+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
12+
use Symfony\Component\Form\FormBuilderInterface;
13+
use Symfony\Component\OptionsResolver\OptionsResolver;
14+
use Symfony\Component\Validator\Constraints\File;
15+
16+
class MediaUploadType extends AbstractType
17+
{
18+
public function buildForm(FormBuilderInterface $builder, array $options)
19+
{
20+
$builder->add('name', null, [
21+
'label' => 'The name of the media with the extension (a watermark will be added on images with a lowercase extension)',
22+
'attr' => [
23+
'placeholder' => 'cover.JPG for the cover image',
24+
],
25+
]);
26+
$builder->add('file', FileType::class, [
27+
'label' => 'Media file (pdf or image)',
28+
]);
29+
}
30+
31+
public function configureOptions(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults([
34+
'data_class' => MediaUpload::class,
35+
]);
36+
}
37+
}

src/Manager/ArticleManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public function __construct(EntityManagerInterface $em, Watermark $watermark)
3737
$this->watermark = $watermark;
3838
}
3939

40+
/**
41+
* Gets the article base path.
42+
*
43+
* @return string
44+
*/
45+
public function getArticleBasePath(): string
46+
{
47+
return $this->articleBasePath;
48+
}
49+
4050
/**
4151
* Synchronizes an article from the disk.
4252
*

src/Model/MediaUpload.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace App\Model;
4+
5+
use App\Entity\Article;
6+
use Symfony\Component\HttpFoundation\File\File;
7+
use Symfony\Component\Validator\Constraints as Assert;
8+
9+
class MediaUpload
10+
{
11+
private Article $article;
12+
13+
/**
14+
* @var string The name of the media with the extension.
15+
*
16+
* @Assert\NotBlank()
17+
* @Assert\Length(max=255)
18+
* @Assert\Regex(
19+
* pattern="/^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/",
20+
* message="The name of the media must only contain letters, numbers and dashes."
21+
* )
22+
* @Assert\Regex(
23+
* pattern="/\.(pdf|jpg|jpeg|png|PDF|JPG|JPEG|PNG)$/",
24+
* message="The name of the media must end with .pdf, .jpg, .jpeg or .png."
25+
* )
26+
*/
27+
private string $name;
28+
29+
/**
30+
* @var File The file.
31+
*
32+
* @Assert\NotBlank()
33+
* @Assert\File(
34+
* maxSize="8128k",
35+
* mimeTypes={
36+
* "application/pdf",
37+
* "application/x-pdf",
38+
* "image/jpeg",
39+
* "image/png"
40+
* },
41+
* mimeTypesMessage="Please upload a valid file (pdf or image)"
42+
* )
43+
*/
44+
private File $file;
45+
46+
/**
47+
* @return Article
48+
*/
49+
public function getArticle(): Article
50+
{
51+
return $this->article;
52+
}
53+
54+
/**
55+
* @param Article $article
56+
*
57+
* @return MediaUpload
58+
*/
59+
public function setArticle(Article $article): self
60+
{
61+
$this->article = $article;
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
public function getName(): string
70+
{
71+
return $this->name;
72+
}
73+
74+
/**
75+
* @param string $name
76+
*
77+
* @return MediaUpload
78+
*/
79+
public function setName(string $name): self
80+
{
81+
$this->name = $name;
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* @return File
88+
*/
89+
public function getFile(): File
90+
{
91+
return $this->file;
92+
}
93+
94+
/**
95+
* @param File $file
96+
*
97+
* @return MediaUpload
98+
*/
99+
public function setFile(File $file): self
100+
{
101+
$this->file = $file;
102+
103+
return $this;
104+
}
105+
}

templates/app/article/edit.html.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
{{ form_row(form.publishedAt) }}
2626
<br /><br />
2727

28+
<a class="btn float-right" href="{{ path('media_article_new', { url: article.url }) }}" target="_blank">
29+
Upload media
30+
</a>
31+
<div class="clearfix"></div>
32+
2833
{{ form_row(form.rawContent) }}
2934

3035
<br /><br />
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% extends 'app/base.html.twig' %}
2+
3+
{% block title %}New media for article {{ article.title}} - Geoffrey Huck{% endblock %}
4+
{% block canonical %}<link rel="canonical" href="{{ site_url }}{{ path('media_article_new', { url: article.url }) }}" />{% endblock %}
5+
6+
{% block content %}
7+
<div class="container">
8+
<a href="{{ path('article_show', { url: article.url, _locale: article.language.code }) }}" class="btn float-right">
9+
Back to the article
10+
</a>
11+
12+
<h1>Add new media for article {{ article.title }}</h1>
13+
14+
<form method="post" enctype="multipart/form-data" novalidate>
15+
{{ form_errors(form) }}
16+
17+
{{ form_row(form.name) }}
18+
{{ form_row(form.file) }}
19+
20+
{{ form_rest(form) }}
21+
22+
<button class="btn" type="submit">
23+
Upload the new media!
24+
</button>
25+
</form>
26+
</div>
27+
{% endblock %}

0 commit comments

Comments
 (0)