1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Repository\Project\Issue\Attachment; |
13
|
|
|
|
14
|
|
|
use Tinyissue\Model\Project; |
15
|
|
|
use Tinyissue\Repository\Repository; |
16
|
|
|
use Illuminate\Http\Response; |
17
|
|
|
use Illuminate\Http\Request; |
18
|
|
|
|
19
|
|
|
class Fetcher extends Repository |
20
|
|
|
{ |
21
|
|
|
protected $storage; |
22
|
|
|
|
23
|
|
|
public function __construct(Project\Issue\Attachment $model) |
24
|
|
|
{ |
25
|
|
|
$this->model = $model; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
protected function getStorage() |
32
|
|
|
{ |
33
|
|
|
if (is_null($this->storage)) { |
34
|
|
|
$this->storage = \Storage::disk('local'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->storage; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getFilePath() |
44
|
|
|
{ |
45
|
|
|
return |
46
|
|
|
config('tinyissue.uploads_dir') . '/' . |
47
|
|
|
$this->model->issue->project_id . '/' . |
48
|
|
|
$this->model->upload_token . '/' . |
49
|
|
|
$this->model->filename; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getSize() |
56
|
|
|
{ |
57
|
|
|
return $this->getStorage()->size($this->getFilePath()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getLastModified() |
64
|
|
|
{ |
65
|
|
|
return $this->getStorage()->lastModified($this->getFilePath()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getMimetype() |
72
|
|
|
{ |
73
|
|
|
return $this->getStorage()->getDriver()->getMimetype($this->getFilePath()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Request $request |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
*/ |
81
|
|
|
public function getDisplayResponse(Request $request) |
82
|
|
|
{ |
83
|
|
|
$time = $this->getLastModified(); |
84
|
|
|
|
85
|
|
|
$response = new Response(); |
86
|
|
|
$response->setEtag(md5($time . $this->getFilePath())); |
87
|
|
|
$response->setExpires(new \DateTime('@' . ($time + 60))); |
88
|
|
|
$response->setLastModified(new \DateTime('@' . $time)); |
89
|
|
|
$response->setPublic(); |
90
|
|
|
$response->setStatusCode(200); |
91
|
|
|
|
92
|
|
|
$response->header('Content-Type', $this->getMimetype()); |
93
|
|
|
$response->header('Content-Length', $this->getSize()); |
94
|
|
|
$response->header('Content-Disposition', 'inline; filename="' . $this->model->filename . '"'); |
95
|
|
|
$response->header('Cache-Control', 'must-revalidate'); |
96
|
|
|
|
97
|
|
|
if (!$response->isNotModified($request)) { |
98
|
|
|
// Return file if first request / modified |
99
|
|
|
$response->setContent($this->getStorage()->get($this->getFilePath())); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $response; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|