public function read($path)
 {
     $dir = new Folder($path);
     $read = $dir->read();
     $result = [];
     foreach ($read[0] as $folder) {
         $info = new Folder($path . DS . $folder);
         $reference = Router::fullBaseUrl() . '/' . $this->toRelative($path . '/' . $folder);
         $data = ['name' => $folder, 'type' => 'folder', 'path' => $path . DS . $folder, 'reference' => $reference, 'extension' => 'folder', 'size' => $info->dirsize()];
         $result[] = $data;
     }
     foreach ($read[1] as $file) {
         $info = new File($path . DS . $file, false);
         $reference = Router::fullBaseUrl() . '/' . $this->toRelative($path . '/' . $file);
         $data = ['name' => $info->info()['basename'], 'type' => 'file', 'path' => $path, 'reference' => $reference, 'extension' => $info->info()['extension'], 'size' => $info->info()['filesize']];
         $result[] = $data;
     }
     return $result;
 }
 /**
  * Creates an Attachment entity based on the given file
  *
  * @param EntityInterface $entity Entity the file will be attached to
  * @param string $filePath Absolute path to the file
  * @param array $tags Indexed array of tags to be assigned
  * @return Attachment
  * @throws \Exception If the given file doesn't exist or isn't readable
  */
 public function createAttachmentEntity(EntityInterface $entity, $filePath, array $tags = [])
 {
     if (!file_exists($filePath)) {
         throw new \Exception("File {$filePath} does not exist.");
     }
     if (!is_readable($filePath)) {
         throw new \Exception("File {$filePath} cannot be read.");
     }
     $file = new File($filePath);
     $info = $file->info();
     // in filepath, we store the path relative to the Attachment.path configuration
     // to make it easy to switch storage
     $info = $this->__getFileName($info, $entity);
     $targetPath = $entity->source() . '/' . $entity->id . '/' . $info['basename'];
     $attachment = $this->newEntity(['model' => $entity->source(), 'foreign_key' => $entity->id, 'filename' => $info['basename'], 'filesize' => $info['filesize'], 'filetype' => $info['mime'], 'filepath' => $targetPath, 'tmpPath' => $filePath, 'tags' => $tags]);
     return $attachment;
 }
Exemplo n.º 3
0
 /**
  * Edit an attachment.
  *
  * @return \Cake\Network\Response|void
  */
 public function edit()
 {
     $this->loadModel('BlogAttachments');
     $this->loadModel('BlogArticles');
     $attachment = $this->BlogAttachments->find()->where(['id' => $this->request->id])->first();
     //Check if the attachment is found.
     if (empty($attachment)) {
         $this->Flash->error(__d('admin', 'This attachment doesn\'t exist or has been deleted.'));
         return $this->redirect(['action' => 'index']);
     }
     if ($this->request->is(['put', 'post'])) {
         $this->BlogAttachments->patchEntity($attachment, $this->request->data());
         //Check if the article has already an attachment
         $article = $this->BlogArticles->find()->contain(['BlogAttachments'])->where(['BlogArticles.id' => $this->request->data['article_id']])->first();
         if (!is_null($article->blog_attachment) && $article->blog_attachment->id != $this->request->id) {
             $this->Flash->error(__d('admin', 'This article has already an attachment, you can edit it <a href="{0}" class="btn btn-sm btn-danger">here</a>.', Router::url(['_name' => 'attachments-edit', 'id' => $article->blog_attachment->id])));
             return $this->redirect(['action' => 'index']);
         }
         $attachment->user_id = $this->Auth->user('id');
         $attachment->accessible('url_file', true);
         if ($editedAttachment = $this->BlogAttachments->save($attachment)) {
             $file = new File(WWW_ROOT . $editedAttachment->url);
             $editedAttachment->name = $file->name;
             $editedAttachment->extension = '.' . $file->info()['extension'];
             $editedAttachment->size = $file->info()['filesize'];
             $this->BlogAttachments->save($editedAttachment);
             $this->Flash->success(__d('admin', 'Your attachment has been edited successfully !'));
             return $this->redirect(['action' => 'index']);
         }
     }
     $articles = $this->BlogAttachments->BlogArticles->find('list');
     $this->set(compact('attachment', 'articles'));
 }