forked from recca0120/upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.php
178 lines (157 loc) · 3.86 KB
/
Api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Recca0120\Upload;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Recca0120\Upload\Contracts\Api as ApiContract;
use Symfony\Component\HttpFoundation\File\UploadedFile;
abstract class Api implements ApiContract
{
/**
* $request.
*
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* $files.
*
* @var \Recca0120\Upload\Filesystem
*/
protected $files;
/**
* $chunkFile.
*
* @var \Recca0120\Upload\ChunkFile
*/
protected $chunkFile;
/**
* $config.
*
* @var array
*/
protected $config;
/**
* __construct.
*
* @param array $config
* @param \Illuminate\Http\Request $request
* @param \Recca0120\Upload\Filesystem $files
* @param \Recca0120\Upload\ChunkFile $chunkFile
*/
public function __construct($config = [], Request $request = null, Filesystem $files = null, ChunkFile $chunkFile = null)
{
$this->request = $request ?: Request::capture();
$this->files = $files ?: new Filesystem();
$this->chunkFile = $chunkFile ?: new ChunkFile($this->files);
$this->config = array_merge([
'chunks' => sys_get_temp_dir().'/chunks',
'storage' => 'storage/temp',
'domain' => $this->request->root(),
'path' => 'storage/temp',
], $config);
}
/**
* domain.
*
* @return string
*/
public function domain()
{
return rtrim($this->config['domain'], '/').'/';
}
/**
* path.
*
* @return string
*/
public function path()
{
return rtrim($this->config['path'], '/').'/';
}
/**
* makeDirectory.
*
* @return $this
*/
public function makeDirectory($path)
{
if ($this->files->isDirectory($path) === false) {
$this->files->makeDirectory($path, 0777, true, true);
}
return $this;
}
/**
* cleanDirectory.
*
* @param string $path
* @return $this
*/
public function cleanDirectory($path)
{
$time = time();
$maxFileAge = 3600;
$files = (array) $this->files->files($path);
foreach ($files as $file) {
if ($this->files->isFile($file) === true &&
$this->files->lastModified($file) < ($time - $maxFileAge)
) {
$this->files->delete($file);
}
}
return $this;
}
/**
* receive.
*
* @param string $inputName
* @return \Symfony\Component\HttpFoundation\File\UploadedFile
*
* @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
*/
abstract public function receive($inputName);
/**
* deleteUploadedFile.
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile
* @return $this
*/
public function deleteUploadedFile(UploadedFile $uploadedFile)
{
$file = $uploadedFile->getPathname();
if ($this->files->isFile($file) === true) {
$this->files->delete($file);
}
$this->cleanDirectory($this->config['chunks']);
return $this;
}
/**
* completedResponse.
*
* @param \Illuminate\Http\JsonResponse $response
* @return \Illuminate\Http\JsonResponse
*/
public function completedResponse(JsonResponse $response)
{
return $response;
}
/**
* chunkPath.
*
* @return string
*/
protected function chunkPath()
{
$this->makeDirectory($this->config['chunks']);
return rtrim($this->config['chunks'], '/').'/';
}
/**
* storagePath.
*
* @return string
*/
protected function storagePath()
{
$this->makeDirectory($this->config['storage']);
return rtrim($this->config['storage'], '/').'/';
}
}