public function indexAction()
 {
     $type = $this->params()->fromRoute('type');
     $id = $this->params()->fromRoute('id');
     $appServiceLoader = $this->recoverAppServiceLoader();
     $configurations = $appServiceLoader->recoverService('configurations');
     $wrapper = new AttachmentsGetterWrapper(new AttachmentsGetter($this->getServiceLocator()->get('doctrine.entitymanager.orm_default')));
     $wrapper->setInput(array('id' => $id, 'limit' => 1));
     $wrapper->setupQueryBuilder();
     $attachmentRecord = $wrapper->getRecords();
     if (empty($attachmentRecord)) {
         return $this->redirect()->toRoute('notfound', array('lang' => 'it'));
     }
     $bucketDir = $type . '/';
     $filename = $attachmentRecord[0]['name'];
     $mimetype = $attachmentRecord[0]['mimetype'];
     $s3 = new S3($configurations['amazon_s3_accesskey'], $configurations['amazon_s3_secretkey']);
     $sthreeFile = $s3->getObject($configurations['amazon_s3_bucket'], $bucketDir . $filename);
     if (empty($sthreeFile->body)) {
         return $this->redirect()->toRoute('notfound', array('lang' => 'it'));
     }
     $response = $this->getResponse();
     $response->setContent($sthreeFile->body);
     $response->getHeaders()->addHeaderLine('Content-Type', 'public')->addHeaderLine('Content-Description', 'File Transfer')->addHeaderLine('Content-Disposition', 'attachment; filename=' . $filename)->addHeaderLine('Content-Type', $mimetype);
     return $response;
 }
 /**
  * Add attachment records to the input recordset
  *
  * @param mixed $records
  * @param array $input
  * @return bool
  */
 public function addAttachmentsToPaginatorRecords($records, $input = array())
 {
     if (empty($records)) {
         return false;
     }
     $this->assertEntityManager();
     foreach ($records as $key => &$value) {
         $attachments = new AttachmentsGetterWrapper(new AttachmentsGetter($this->getEntityManager()));
         $attachments->setInput(array_merge($input, array('referenceId' => isset($value['id']) ? $value['id'] : null)));
         $attachments->setupQueryBuilder();
         $attachmentsRecords = $attachments->getRecords();
         if (!empty($attachmentsRecords)) {
             $value['attachments'] = $attachmentsRecords;
         }
     }
     return $records;
 }