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

Commit 7f61327

Browse files
authored
Merge pull request overtrue#56 from yueziii/master
added remote image catch support
2 parents ac7e860 + 1ae25a4 commit 7f61327

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

src/Events/Catched.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-ueditor.
5+
*
6+
* (c) yueziii <i@yueziii.com>
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 <i@yueziii.com>
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->result = $result;
41+
}
42+
}

src/StorageManager.php

Lines changed: 102 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
/**
@@ -86,6 +87,94 @@ public function upload(Request $request)
8687
return response()->json($response);
8788
}
8889

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+
89178
/**
90179
* @return bool
91180
*/
@@ -149,6 +238,19 @@ protected function store(UploadedFile $file, $filename)
149238
return $this->disk->put($filename, fopen($file->getRealPath(), 'r+'));
150239
}
151240

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+
152254
/**
153255
* Validate the input file.
154256
*

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)