Skip to content

Commit e7272d2

Browse files
committed
Implement attachment downloading
1 parent c2db63a commit e7272d2

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace BNETDocs\Controllers\Attachment;
4+
5+
use \BNETDocs\Libraries\Attachment;
6+
use \BNETDocs\Libraries\Cache;
7+
use \BNETDocs\Libraries\Common;
8+
use \BNETDocs\Libraries\Controller;
9+
use \BNETDocs\Libraries\Database;
10+
use \BNETDocs\Libraries\DatabaseDriver;
11+
use \BNETDocs\Libraries\Exceptions\AttachmentNotFoundException;
12+
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
13+
use \BNETDocs\Libraries\Router;
14+
use \BNETDocs\Models\Attachment\Download as DownloadModel;
15+
use \BNETDocs\Views\Attachment\DownloadRaw as DownloadRawView;
16+
17+
class Download extends Controller {
18+
19+
public function run(Router &$router) {
20+
switch ($router->getRequestPathExtension()) {
21+
case "":
22+
$view = new DownloadRawView();
23+
break;
24+
default:
25+
throw new UnspecifiedViewException();
26+
}
27+
28+
$data = $router->getRequestQueryArray();
29+
$id = (isset($data["id"]) ? (int) $data["id"] : null);
30+
31+
$model = new DownloadModel();
32+
$model->attachment_id = $id;
33+
34+
try {
35+
$model->attachment = new Attachment($id);
36+
} catch (AttachmentNotFoundException $e) {
37+
$model->attachment = null;
38+
}
39+
40+
ob_start();
41+
$view->render($model);
42+
$router->setResponseCode(($model->attachment ? 200 : 404));
43+
$router->setResponseTTL(31536000); // 1 year
44+
$router->setResponseHeader("Content-Type", $view->getMimeType());
45+
if ($model->extra_headers) {
46+
foreach ($model->extra_headers as $name => $value) {
47+
$router->setResponseHeader($name, $value);
48+
}
49+
}
50+
$router->setResponseContent(ob_get_contents());
51+
ob_end_clean();
52+
}
53+
54+
}

src/libraries/Router.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BNETDocs\Libraries;
44

55
use \BNETDocs\Controllers\API\Comment as APICommentController;
6+
use \BNETDocs\Controllers\Attachment\Download as AttachmentDownloadController;
67
use \BNETDocs\Controllers\Credits as CreditsController;
78
use \BNETDocs\Controllers\Document\Index as DocumentIndexController;
89
use \BNETDocs\Controllers\Document\Popular as DocumentPopularController;
@@ -254,6 +255,17 @@ public function route(Pair &$redirect = null) {
254255
}
255256
$controller = new RedirectController($url, $code);
256257
break;
258+
case "attachment":
259+
switch ($subpath) {
260+
case "download":
261+
$controller = new AttachmentDownloadController();
262+
break;
263+
default:
264+
throw new ControllerNotFoundException(
265+
$path . "/" . $subpath
266+
);
267+
}
268+
break;
257269
case "api":
258270
switch ($subpath) {
259271
case "comment":

src/models/Attachment/Download.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace BNETDocs\Models\Attachment;
4+
5+
use \BNETDocs\Libraries\Model;
6+
7+
class Download extends Model {
8+
9+
public $attachment;
10+
public $attachment_id;
11+
public $extra_headers;
12+
13+
public function __construct() {
14+
parent::__construct();
15+
$this->attachment = null;
16+
$this->attachment_id = null;
17+
$this->extra_headers = null;
18+
}
19+
20+
}

src/views/Attachment/DownloadRaw.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace BNETDocs\Views\Attachment;
4+
5+
use \BNETDocs\Libraries\Common;
6+
use \BNETDocs\Libraries\Exceptions\IncorrectModelException;
7+
use \BNETDocs\Libraries\Model;
8+
use \BNETDocs\Libraries\View;
9+
use \BNETDocs\Models\Attachment\Download as DownloadModel;
10+
use \DateTime;
11+
12+
class DownloadRaw extends View {
13+
14+
public function getMimeType() {
15+
return "application/octet-stream";
16+
}
17+
18+
public function render(Model &$model) {
19+
if (!$model instanceof DownloadModel) {
20+
throw new IncorrectModelException();
21+
}
22+
$model->extra_headers = [
23+
"Content-Disposition" => "attachment;filename=\""
24+
. $model->attachment->getFilename() . "\"",
25+
"Last-Modified" => $model->attachment->getCreatedDateTime()->format(
26+
DateTime::RFC1123
27+
)
28+
];
29+
echo $model->attachment->getContent();
30+
}
31+
32+
}

0 commit comments

Comments
 (0)