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

Commit 1ae25a4

Browse files
author
yueziii
committed
update download logic
1 parent ae6c06b commit 1ae25a4

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/StorageManager.php

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public function fetch(Request $request)
116116
event(new Catched($img));
117117
}
118118
}
119-
$img['filename'] = $filename;
120119
unset($img['file']);
121120
array_push($list, $img);
122121
}
@@ -134,42 +133,46 @@ public function fetch(Request $request)
134133
*
135134
* @param \Illuminate\Http\Request $request
136135
*
137-
* @return Array
136+
* @return Array $info
138137
*/
139138
private function download($url, $config)
140139
{
141-
$imgUrl = htmlspecialchars($url);
142-
$imgUrl = str_replace('&', '&', $imgUrl);
143-
$pathRes = parse_url($imgUrl);
144-
$queryString = isset($pathRes['query']) ? $pathRes['query'] : '';
145-
$imgUrl = str_replace('?' . $queryString, '', $imgUrl);
146-
if (strpos($imgUrl, 'http') !== 0) {
140+
if (strpos($url, 'http') !== 0) {
147141
return $this->error('ERROR_HTTP_LINK');
148142
}
149-
150-
$context = stream_context_create(
151-
array('http' => array(
152-
'follow_location' => false, // don't follow redirects
153-
))
154-
);
155-
$file = fopen($imgUrl . '?' . $queryString, 'r', false, $context);
156-
$img = stream_get_contents($file);
157-
fclose($file);
158-
preg_match('/[\/]([^\/]*)[\.]?[^\.\/]*$/', $imgUrl, $m);
159-
$original = $m ? $m[1] : '';
160-
$ext = strtolower(strrchr($original, '.'));
143+
$pathRes = parse_url($url);
144+
$img = new \SplFileInfo($pathRes['path']);
145+
$original = $img->getFilename();
146+
$ext = $img->getExtension();
161147
$title = config('ueditor.hash_filename') ? md5($original) . $ext : $original;
162148
$filename = $this->formatPath($config['path_format'], $title);
163-
return [
149+
$info = [
164150
'state' => 'SUCCESS',
165151
'url' => $this->getUrl($filename),
166152
'title' => $title,
167153
'original' => $original,
168154
'source' => $url,
169-
'size' => strlen($img),
170-
'file' => $img,
155+
'size' => 0,
156+
'file' => '',
171157
'filename' => $filename,
172158
];
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;
173176
}
174177

175178
/**

0 commit comments

Comments
 (0)