|
16 | 16 | use Illuminate\Support\Facades\Storage;
|
17 | 17 | use Overtrue\LaravelUEditor\Events\Uploaded;
|
18 | 18 | use Overtrue\LaravelUEditor\Events\Uploading;
|
| 19 | +use Overtrue\LaravelUEditor\Events\Catched; |
19 | 20 | use Symfony\Component\HttpFoundation\File\UploadedFile;
|
20 | 21 |
|
21 | 22 | /**
|
@@ -86,6 +87,94 @@ public function upload(Request $request)
|
86 | 87 | return response()->json($response);
|
87 | 88 | }
|
88 | 89 |
|
| 90 | + /** |
| 91 | + * Fetch a file. |
| 92 | + * |
| 93 | + * @param \Illuminate\Http\Request $request |
| 94 | + * |
| 95 | + * @return \Illuminate\Http\JsonResponse |
| 96 | + */ |
| 97 | + public function fetch(Request $request) |
| 98 | + { |
| 99 | + $config = $this->getUploadConfig($request->get('action')); |
| 100 | + $urls = $request->get($config['field_name']); |
| 101 | + if (count($urls) === 0) { |
| 102 | + return $this->error('UPLOAD_ERR_NO_FILE'); |
| 103 | + } |
| 104 | + $urls = array_unique($urls); |
| 105 | + |
| 106 | + $list = array(); |
| 107 | + foreach ($urls as $key => $url) { |
| 108 | + $img = $this->download($url, $config); |
| 109 | + $item = []; |
| 110 | + if ($img['state'] === 'SUCCESS') { |
| 111 | + $file = $img['file']; |
| 112 | + $filename = $img['filename']; |
| 113 | + $this->storeContent($file, $filename); |
| 114 | + if ($this->eventSupport()) { |
| 115 | + unset($img['file']); |
| 116 | + event(new Catched($img)); |
| 117 | + } |
| 118 | + } |
| 119 | + unset($img['file']); |
| 120 | + array_push($list, $img); |
| 121 | + } |
| 122 | + |
| 123 | + $response = [ |
| 124 | + 'state'=> count($list) ? 'SUCCESS':'ERROR', |
| 125 | + 'list'=> $list |
| 126 | + ]; |
| 127 | + |
| 128 | + return response()->json($response); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Download a file. |
| 133 | + * |
| 134 | + * @param \Illuminate\Http\Request $request |
| 135 | + * |
| 136 | + * @return Array $info |
| 137 | + */ |
| 138 | + private function download($url, $config) |
| 139 | + { |
| 140 | + if (strpos($url, 'http') !== 0) { |
| 141 | + return $this->error('ERROR_HTTP_LINK'); |
| 142 | + } |
| 143 | + $pathRes = parse_url($url); |
| 144 | + $img = new \SplFileInfo($pathRes['path']); |
| 145 | + $original = $img->getFilename(); |
| 146 | + $ext = $img->getExtension(); |
| 147 | + $title = config('ueditor.hash_filename') ? md5($original) . $ext : $original; |
| 148 | + $filename = $this->formatPath($config['path_format'], $title); |
| 149 | + $info = [ |
| 150 | + 'state' => 'SUCCESS', |
| 151 | + 'url' => $this->getUrl($filename), |
| 152 | + 'title' => $title, |
| 153 | + 'original' => $original, |
| 154 | + 'source' => $url, |
| 155 | + 'size' => 0, |
| 156 | + 'file' => '', |
| 157 | + 'filename' => $filename, |
| 158 | + ]; |
| 159 | + |
| 160 | + $context = stream_context_create( |
| 161 | + array('http' => array( |
| 162 | + 'follow_location' => false, // don't follow redirects |
| 163 | + )) |
| 164 | + ); |
| 165 | + $file = fopen($url, 'r', false, $context); |
| 166 | + if ($file === false) { |
| 167 | + $info['state'] = 'ERROR'; |
| 168 | + return $info; |
| 169 | + } |
| 170 | + $content = stream_get_contents($file); |
| 171 | + fclose($file); |
| 172 | + |
| 173 | + $info['file'] = $content; |
| 174 | + $info['siez'] = strlen($content); |
| 175 | + return $info; |
| 176 | + } |
| 177 | + |
89 | 178 | /**
|
90 | 179 | * @return bool
|
91 | 180 | */
|
@@ -149,6 +238,19 @@ protected function store(UploadedFile $file, $filename)
|
149 | 238 | return $this->disk->put($filename, fopen($file->getRealPath(), 'r+'));
|
150 | 239 | }
|
151 | 240 |
|
| 241 | + /** |
| 242 | + * Store file from content. |
| 243 | + * |
| 244 | + * @param string |
| 245 | + * @param string $filename |
| 246 | + * |
| 247 | + * @return mixed |
| 248 | + */ |
| 249 | + protected function storeContent($content, $filename) |
| 250 | + { |
| 251 | + return $this->disk->put($filename, $content); |
| 252 | + } |
| 253 | + |
152 | 254 | /**
|
153 | 255 | * Validate the input file.
|
154 | 256 | *
|
|
0 commit comments