2013-02-13 53 views
4

我有一個表格,其中我有一個file字段上傳圖片。我需要在Amazon S3上傳這張圖片。一步一步建立我開始上傳本地磁盤上的圖像,它現在正在工作。Symfony 2 - 在Amazon S3上上傳圖片的最佳做法

上傳發生在我的實體Page內部,因爲建議在保存實體之前測試上傳是否成功。我結束了這一塊代碼

/** 
    * @ORM\Column(name="objectThumbnail", type="string", length=255, nullable=true) 
    */ 
    protected $objectThumbnail; 

    /** 
    * This is not a column in the database but it's mapping a field from the form 
    * 
    * @Assert\File(maxSize="600000000") 
    */ 
    public $file; 

    ... 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 
     if (null !== $this->file) { 
      // generate a unique filename 
      $this->objectThumbnail = $this->getGuid().'.'.$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->objectThumbnail); 

     unset($this->file); 
    } 

    /** 
    * @ORM\PostRemove() 
    */ 
    public function removeUpload() 
    { 
     if ($file = $this->getAbsolutePath()) { 
      unlink($file); 
     } 
    } 

這是完美的,它的工作就像一個魅力。 Just Symfony(2.1.7)由於file屬性的公共範圍尖叫,沒有什麼重要的。

現在我需要整合S3層。爲此,我想我將使用GaufretteStreamWrapper

現在我正在努力尋找最好的辦法。如何訪問實體中的Filesystem服務?這樣做真的很乾淨嗎?在實體中處理S3上的圖像上傳時,我感到有些尷尬。

你會如何推薦這麼做?

乾杯,

馬克西姆

+0

Hi @ maxwell2022,你採取哪種方法解決這個問題?你使用Gaufrette包或Cyber​​nox/AmazonWebServicesBundle? Tks爲你的好問題... – 2014-06-24 12:40:08

+0

是的,我使用了Gaufrette。閱讀接受的答案的評論,有更多的細節。 – maxwell2022 2014-06-24 23:28:35

回答

5

在實體使用的服務是不是一個很好的實踐。 我會推薦創建執行驗證,持久性的表單處理程序,並上傳在這個例子中

我寫一個適合您的需求

//... 
//inside a creation controller action 
//... 
//create a new page entity 
$page = new Page(); 

//get your page form class 
$form = $this->createForm(new PageForm(), $page); 

//call your form handler 
$formHandler = new PageFormHandler(
     $form, 
     $this->get('request'), 
     $this->get('your.gaufrette.filesystem'), 
     $this->get('your.page.manager') 
); 

//form is valid ? 
if ($formHandler->process()) {   
    //flash message, redirection etc 
} 

處理程序類

namespace YourCompagnyBundle\Form\Handler; 

use YourCompagnyBundle\Entity\Page; 
use Symfony\Component\Form\Form; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 
use Gaufrette\Filesystem; 


class PageFormHandler 
{ 
    protected $form; 
    protected $request; 
    protected $pageManager; 
    protected $filesystem; 

    public function __construct(Form $form, Request $request, Filesystem $filesystem, $pageManager) 
    { 
     $this->form = $form; 
     $this->request = $request; 
     $this->filesystem = $filesystem; 
     $this->pageManager = $pageManager; 
    } 

    public function process() 
    { 
     if($this->request->getMethod() == 'POST') 
     { 
      $this->form->bind($this->request); 

      if($this->form->isValid()) 
      { 
       $this->onSuccess($this->form->getData());    

       return true; 
      } 
     } 

     return false; 
    } 

    function onSuccess(Page $page) 
    { 
     //has uploaded file ? 
     if($page->getFile() instanceof UploadedFile){ 
      //do some logic with gaufrette filesystem 
      //if page is already managed in database (update), delete previous file 


     } 
     //storage 
     $this->pageManager->save($page); 
    } 

} 

希望這樣的例子幫助

+0

該解決方案的問題在於,當您刪除「Page」實體時,它不會刪除S3上的圖像。而在'Page'實體中使用@ORM \ PostRemove()將處理這種情況。我想過這個,但我想找到一個解決方案來處理所有情況(插入,更新和刪除)儘可能最好的方式。 thx – maxwell2022 2013-02-15 23:29:50

+0

我已將此示例用於創建。在我的情況下,pageManager類是一個服務層,它持久/刪除你的實體,管理事件等。刪除將是另一種形式,但是在這兩個事件中,事件仍然由實體管理器分派。 – 2013-02-15 23:33:42

+0

請注意,更新是一個棘手的情況,因爲您必須檢測新的文件上傳,刪除以前的文件並保存新文件 – 2013-02-15 23:40:42

4

也許你應該檢查VichUploaderBundle基本上注入File對象到你的entites和與Gaufrette的整合非常容易。

+1

謝謝。它看起來不錯,但我更喜歡在我的捆綁中處理它。我發現它更容易理解和調試 – maxwell2022 2013-02-20 08:03:25