Skip to content
This repository was archived by the owner on Aug 22, 2021. It is now read-only.

Commit 1286c67

Browse files
author
yueziii
committed
added remote image catch support
1 parent ac7e860 commit 1286c67

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

src/Events/Catched.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-ueditor.
5+
*
6+
* (c) yueziii <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Overtrue\LaravelUEditor\Events;
13+
14+
use Illuminate\Broadcasting\InteractsWithSockets;
15+
use Illuminate\Foundation\Events\Dispatchable;
16+
use Illuminate\Queue\SerializesModels;
17+
use Symfony\Component\HttpFoundation\File\UploadedFile;
18+
19+
/**
20+
* Class Catched.
21+
*
22+
* @author yueziii <[email protected]>
23+
*/
24+
class Catched
25+
{
26+
use Dispatchable, InteractsWithSockets, SerializesModels;
27+
28+
/**
29+
* @var array
30+
*/
31+
public $result;
32+
33+
/**
34+
* Catched constructor.
35+
*
36+
* @param array $result
37+
*/
38+
public function __construct(array $result)
39+
{
40+
// $this->file = $file;
41+
$this->result = $result;
42+
}
43+
}

src/StorageManager.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Support\Facades\Storage;
1717
use Overtrue\LaravelUEditor\Events\Uploaded;
1818
use Overtrue\LaravelUEditor\Events\Uploading;
19+
use Overtrue\LaravelUEditor\Events\Catched;
1920
use Symfony\Component\HttpFoundation\File\UploadedFile;
2021

2122
/**
@@ -85,6 +86,90 @@ public function upload(Request $request)
8586

8687
return response()->json($response);
8788
}
89+
/**
90+
* Fetch a file.
91+
*
92+
* @param \Illuminate\Http\Request $request
93+
*
94+
* @return \Illuminate\Http\JsonResponse
95+
*/
96+
public function fetch(Request $request)
97+
{
98+
$config = $this->getUploadConfig($request->get('action'));
99+
$urls = $request->get($config['field_name']);
100+
if (count($urls) === 0) {
101+
return $this->error('UPLOAD_ERR_NO_FILE');
102+
}
103+
$urls = array_unique($urls);
104+
105+
$list = array();
106+
foreach ($urls as $key => $url) {
107+
$img = $this->download($url, $config);
108+
$item = [];
109+
if ($img['state'] === 'SUCCESS') {
110+
$file = $img['file'];
111+
$filename = $img['filename'];
112+
$this->storeContent($file, $filename);
113+
if ($this->eventSupport()) {
114+
unset($img['file']);
115+
event(new Catched($img));
116+
}
117+
}
118+
$img['filename'] = $filename;
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+
* Download a file.
132+
*
133+
* @param \Illuminate\Http\Request $request
134+
*
135+
* @return file
136+
*/
137+
private function download($url, $config)
138+
{
139+
$imgUrl = htmlspecialchars($url);
140+
$imgUrl = str_replace('&amp;', '&', $imgUrl);
141+
$pathRes = parse_url($imgUrl);
142+
$queryString = isset($pathRes['query']) ? $pathRes['query'] : '';
143+
$imgUrl = str_replace('?' . $queryString, '', $imgUrl);
144+
if (strpos($imgUrl, 'http') !== 0) {
145+
return $this->error('ERROR_HTTP_LINK');
146+
}
147+
148+
$context = stream_context_create(
149+
array('http' => array(
150+
'follow_location' => false, // don't follow redirects
151+
))
152+
);
153+
$file = fopen($imgUrl . '?' . $queryString, 'r', false, $context);
154+
$img = stream_get_contents($file);
155+
fclose($file);
156+
preg_match('/[\/]([^\/]*)[\.]?[^\.\/]*$/', $imgUrl, $m);
157+
$original = $m ? $m[1] : '';
158+
$ext = strtolower(strrchr($original, '.'));
159+
$title = config('ueditor.hash_filename') ? md5($original) . $ext : $original;
160+
$filename = $this->formatPath($config['path_format'], $title);
161+
return [
162+
'state' => 'SUCCESS',
163+
'url' => $this->getUrl($filename),
164+
'title' => $title,
165+
'original' => $original,
166+
'source' => $url,
167+
'size' => strlen($img),
168+
'file' => $img,
169+
'filename' => $filename,
170+
];
171+
172+
}
88173

89174
/**
90175
* @return bool
@@ -149,6 +234,19 @@ protected function store(UploadedFile $file, $filename)
149234
return $this->disk->put($filename, fopen($file->getRealPath(), 'r+'));
150235
}
151236

237+
/**
238+
* Store file from content.
239+
*
240+
* @param string
241+
* @param string $filename
242+
*
243+
* @return mixed
244+
*/
245+
protected function storeContent($content, $filename)
246+
{
247+
return $this->disk->put($filename, $content);
248+
}
249+
152250
/**
153251
* Validate the input file.
154252
*

src/UEditorController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function serve(Request $request)
4141
$request->get('start'),
4242
$request->get('size'),
4343
$upload['fileManagerAllowFiles']);
44+
case $upload['catcherActionName']:
45+
return $storage->fetch($request);
4446
default:
4547
return $storage->upload($request);
4648
}

0 commit comments

Comments
 (0)