Skip to content

Commit fbbb3ac

Browse files
committed
Add file access
1 parent f9bbf94 commit fbbb3ac

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

config/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
// set thumbnails route
66
Router::connect('/thumbnails/*', ['plugin' => 'Attachment', 'controller' => 'Resize', 'action' => 'proceed']);
7+
Router::connect('/file/*', ['plugin' => 'Attachment', 'controller' => 'File', 'action' => 'get']);
8+
79

810
// set plugin stuff : )
911
Router::plugin(

src/Controller/FileController.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Attachment\Controller;
3+
4+
use Attachment\Controller\AppController;
5+
use Cake\Core\Configure;
6+
use Cake\Network\Exception\NotFoundException;
7+
use Attachment\Fly\FilesystemRegistry;
8+
9+
/**
10+
* Attachments Controller
11+
*
12+
* @property \Attachment\Model\Table\AttachmentsTable $Attachments
13+
*/
14+
class FileController extends AppController
15+
{
16+
private function _filesystem($profile)
17+
{
18+
return FilesystemRegistry::retrieve($profile);
19+
}
20+
21+
public function get($profile, ...$file )
22+
{
23+
24+
// test profile
25+
if(!Configure::check('Attachment.profiles.'.$profile) || $profile == 'thumbnails' ){ throw new NotFoundException(); }
26+
27+
// if empty $image
28+
if(empty($file)){ throw new NotFoundException(); }
29+
$file = implode("/", $file);
30+
31+
//test if found
32+
if(!$this->_filesystem($profile)->has($file)) throw new NotFoundException();
33+
34+
// check if baseUrl file
35+
if(!Configure::read('Attachment.profiles.'.$profile.'.baseUrl')) throw new NotFoundException();
36+
37+
return $this->redirect(Configure::read('Attachment.profiles.'.$profile.'.baseUrl').$file);
38+
39+
}
40+
}

0 commit comments

Comments
 (0)