Permalink
Browse files

upload file to blog, and adjust .gitignore

  • Loading branch information...
1 parent 46c9be7 commit c50b158041b859e273e26f8b85caf7bd2c1d5645 sunfeng committed Dec 31, 2012
View
3 .gitignore
@@ -11,10 +11,11 @@ nbproject
# ignore symfony2
web/bundles/
+web/uploads/
app/bootstrap.php.cache
app/cache/*
app/logs/*
app/config/parameters.yml
+vendor/
build/
-vendor
composer.phar
View
2 src/Blogger/BlogBundle/Controller/BlogController.php
@@ -51,6 +51,7 @@ public function createAction()
// Persist the blog entity
$em = $this->getDoctrine()
->getEntityManager();
+ //$blog->upload();
$em->persist($blog);
$em->flush();
@@ -85,6 +86,7 @@ public function updateAction($id)
if ($form->isValid()) {
// Update the blog entity
+ //$blog->upload();
$em->flush();
return $this->redirect($this->generateUrl('blogger_blog_blog_show', array('id'=>$blog->getId())));
View
105 src/Blogger/BlogBundle/Entity/Blog.php
@@ -7,6 +7,8 @@
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
+use Symfony\Component\Validator\Constraints as Assert;
+
/**
* @ORM\Entity(repositoryClass="Blogger\BlogBundle\Entity\Repository\BlogRepository")
* @ORM\Table(name="blog")
@@ -47,6 +49,13 @@ class Blog
protected $image;
/**
+ * must be a public property
+ *
+ * @Assert\File(maxSize="6000000")
+ */
+ public $file;
+
+ /**
* @ORM\Column(type="text", nullable=true)
*/
protected $tags;
@@ -384,6 +393,102 @@ public function slugify($text)
return $text;
}
+ /**
+ * @ORM\PrePersist()
+ * @ORM\PreUpdate()
+ */
+ public function preUpload()
+ {
+ if (null !== $this->file) {
+ // do whatever you want to generate a unique name
+ $filename = sha1(uniqid(mt_rand(), true));
+ $this->image = $filename.'.'.$this->file->guessExtension();
+ }
+ }
+
+ /**
+ * @ORM\PostPersist()
+ * @ORM\PostUpdate()
+ */
+ public function upload()
+ {
+ if (null === $this->file) {
+ return;
+ }
+
+ // if there is an error when moving the file, an exception will
+ // be automatically thrown by move(). This will properly prevent
+ // the entity from being persisted to the database on error
+ $this->file->move($this->getUploadRootDir(), $this->image);
+
+ unset($this->file);
+ }
+
+ /**
+ * @ORM\PostRemove()
+ */
+ public function removeUpload()
+ {
+ if ($this->file == $this->getAbsolutePath()) {
+ unlink($this->file);
+ }
+ }
+
+ /**
+ * Method 1: upload a file
+ */
+ /* public function upload()
+ {
+ // the file property can be empty if the field is not required
+ if (null === $this->file) {
+ return;
+ }
+
+ // use the original file name here but you should
+ // sanitize it at least to avoid any security issues
+
+ // move takes the target directory and then the
+ // target filename to move to
+ $this->file->move(
+ $this->getUploadRootDir(),
+ $this->file->getClientOriginalName()
+ );
+
+ // set the path property to the filename where you've saved the file
+ $this->image = $this->file->getClientOriginalName();
+
+ // clean up the file property as you won't need it anymore
+ $this->file = null;
+ } */
+
+ public function getAbsolutePath()
+ {
+ return null === $this->image
+ ? null
+ : $this->getUploadRootDir().'/'.$this->image;
+ }
+
+ public function getWebPath()
+ {
+ return null === $this->image
+ ? null
+ : $this->getUploadDir().'/'.$this->image;
+ }
+
+ protected function getUploadRootDir()
+ {
+ // the absolute directory path where uploaded
+ // documents should be saved
+ return __DIR__.'/../../../../web/'.$this->getUploadDir();
+ }
+
+ protected function getUploadDir()
+ {
+ // get rid of the __DIR__ so it doesn't screw up
+ // when displaying uploaded doc/image in the view.
+ return 'uploads/documents';
+ }
+
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('title', new NotBlank());
View
3 src/Blogger/BlogBundle/Form/BlogType.php
@@ -15,8 +15,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
//->add('slug')
->add('author')
->add('content')
- ->add('image', 'file', array('required'=>false))
+ //->add('image', 'file', array('required'=>false))
->add('tags', 'text', array('required'=>false))
+ ->add('file')
//->add('created')
//->add('updated')
;
View
2 src/Blogger/BlogBundle/Resources/views/Blog/show.html.twig
@@ -8,7 +8,7 @@
<div class="date"><time datetime="{{ blog.created|date('c') }}">{{ blog.created|date('l, F j, Y') }}</time></div>
<h2>{{ blog.title }}</h2>
</header>
- {% if blog.image != '' %}<img src="{{ asset(['images/', blog.image]|join) }}" alt="{{ blog.title }} image not found" class="large" />{% endif %}
+ {% if blog.image != '' %}<img src="{{ asset(['uploads/documents/', blog.image]|join) }}" alt="{{ blog.title }} image not found" class="large" />{% endif %}
<div>
<p>{{ blog.content }}</p>
</div>
View
2 src/Blogger/BlogBundle/Resources/views/Page/index.html.twig
@@ -10,7 +10,7 @@
<h2><a href="{{ path('blogger_blog_blog_show', { 'id': blog.id }) }}">{{ blog.title }}</a></h2>
</header>
- {% if blog.image != '' %}<img src="{{ asset(['images/', blog.image]|join) }}" />{% endif %}
+ {% if blog.image != '' %}<img src="{{ asset(['uploads/documents/', blog.image]|join) }}" />{% endif %}
<div class="snippet">
<p>{{ blog.content(500) }}</p>
<p class="continue"><a href="{{ path('blogger_blog_blog_show', { 'id': blog.id }) }}">Continue reading...</a> | <a href="{{ path('blogger_blog_blog_update', { 'id': blog.id }) }}">Update</a> | <a href="{{ path('blogger_blog_blog_delete', { 'id': blog.id }) }}">Delete</a></p>
View
2 src/Blogger/BlogBundle/Resources/views/Page/tag_search.html.twig
@@ -14,7 +14,7 @@
<h2><a href="{{ path('blogger_blog_blog_show', { 'id': blog.id }) }}">{{ blog.title }}</a></h2>
</header>
- {% if blog.image != '' %}<img src="{{ asset(['images/', blog.image]|join) }}" />{% endif %}
+ {% if blog.image != '' %}<img src="{{ asset(['uploads/documents/', blog.image]|join) }}" />{% endif %}
<div class="snippet">
<p>{{ blog.content(500) }}</p>
<p class="continue"><a href="{{ path('blogger_blog_blog_show', { 'id': blog.id }) }}">Continue reading...</a></p>

0 comments on commit c50b158

Please sign in to comment.