1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Upload; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Recca0120\Upload\Contracts\Api as ApiContract; |
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
use Recca0120\Upload\Exceptions\ChunkedResponseException; |
10
|
|
|
|
11
|
|
|
class Receiver |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* $api. |
15
|
|
|
* |
16
|
|
|
* @var \Recca0120\Upload\Contracts\Api |
17
|
|
|
*/ |
18
|
|
|
protected $api; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* __construct. |
22
|
|
|
* |
23
|
|
|
* @param \Recca0120\Upload\Contracts\Api $api |
24
|
|
|
*/ |
25
|
7 |
|
public function __construct(ApiContract $api) |
26
|
|
|
{ |
27
|
7 |
|
$this->api = $api; |
28
|
7 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* receive. |
32
|
|
|
* |
33
|
|
|
* @param string $inputName |
34
|
|
|
* @param callable $callback |
35
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
36
|
|
|
*/ |
37
|
3 |
|
public function receive($inputName = 'file', callable $callback = null) |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
3 |
|
$callback = $callback ?: [$this, 'callback']; |
41
|
3 |
|
$response = call_user_func_array($callback, [ |
42
|
3 |
|
$uploadedFile = $this->api->receive($inputName), |
43
|
2 |
|
$this->api->path(), |
44
|
2 |
|
$this->api->domain(), |
45
|
2 |
|
$this->api, |
46
|
|
|
]); |
47
|
|
|
|
48
|
2 |
|
return $this->api->deleteUploadedFile($uploadedFile)->completedResponse($response); |
49
|
1 |
|
} catch (ChunkedResponseException $e) { |
50
|
1 |
|
return $e->getResponse(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* factory. |
56
|
|
|
* |
57
|
|
|
* @param array $config |
58
|
|
|
* @param string $class |
59
|
|
|
* @return \Recca0120\Upload\Contracts\Api |
60
|
|
|
*/ |
61
|
1 |
|
public static function factory($config = [], $class = FileAPI::class) |
62
|
|
|
{ |
63
|
1 |
|
$class = Arr::get([ |
64
|
1 |
|
'dropzone' => Dropzone::class, |
65
|
|
|
'fileapi' => FileAPI::class, |
66
|
|
|
'fineuploader' => FineUploader::class, |
67
|
|
|
'plupload' => Plupload::class, |
68
|
1 |
|
], strtolower($class), $class); |
69
|
|
|
|
70
|
1 |
|
return new static(new $class($config)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* callback. |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Http\JsonResponse |
77
|
|
|
*/ |
78
|
1 |
|
protected function callback(UploadedFile $uploadedFile, $path, $domain) |
79
|
|
|
{ |
80
|
1 |
|
$clientPathInfo = $this->pathInfo($uploadedFile->getClientOriginalName()); |
81
|
1 |
|
$basePathInfo = $this->pathInfo($uploadedFile->getBasename()); |
82
|
1 |
|
$filename = md5($basePathInfo['basename']).'.'.$clientPathInfo['extension']; |
83
|
1 |
|
$mimeType = $uploadedFile->getMimeType(); |
84
|
1 |
|
$size = $uploadedFile->getSize(); |
85
|
1 |
|
$uploadedFile->move($path, $filename); |
86
|
|
|
$response = [ |
87
|
1 |
|
'name' => $clientPathInfo['filename'].'.'.$clientPathInfo['extension'], |
88
|
1 |
|
'tmp_name' => $path.$filename, |
89
|
1 |
|
'type' => $mimeType, |
90
|
1 |
|
'size' => $size, |
91
|
1 |
|
'url' => $domain.$path.$filename, |
92
|
|
|
]; |
93
|
|
|
|
94
|
1 |
|
return new JsonResponse($response); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
private function pathInfo($path) |
98
|
|
|
{ |
99
|
1 |
|
$parts = []; |
100
|
1 |
|
$parts['dirname'] = rtrim(substr($path, 0, strrpos($path, '/')), '/').'/'; |
101
|
1 |
|
$parts['basename'] = ltrim(substr($path, strrpos($path, '/')), '/'); |
102
|
1 |
|
$parts['extension'] = strtolower(substr(strrchr($path, '.'), 1)); |
103
|
1 |
|
$parts['filename'] = ltrim(substr($parts['basename'], 0, strrpos($parts ['basename'], '.')), '/'); |
104
|
|
|
|
105
|
1 |
|
return $parts; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|