Skip to content
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

added remote image catch support #56

Merged
merged 3 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Events/Catched.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the overtrue/laravel-ueditor.
*
* (c) yueziii <i@yueziii.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\LaravelUEditor\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Class Catched.
*
* @author yueziii <i@yueziii.com>
*/
class Catched
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var array
*/
public $result;

/**
* Catched constructor.
*
* @param array $result
*/
public function __construct(array $result)
{
$this->result = $result;
}
}
102 changes: 102 additions & 0 deletions src/StorageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Support\Facades\Storage;
use Overtrue\LaravelUEditor\Events\Uploaded;
use Overtrue\LaravelUEditor\Events\Uploading;
use Overtrue\LaravelUEditor\Events\Catched;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
Expand Down Expand Up @@ -86,6 +87,94 @@ public function upload(Request $request)
return response()->json($response);
}

/**
* Fetch a file.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function fetch(Request $request)
{
$config = $this->getUploadConfig($request->get('action'));
$urls = $request->get($config['field_name']);
if (count($urls) === 0) {
return $this->error('UPLOAD_ERR_NO_FILE');
}
$urls = array_unique($urls);

$list = array();
foreach ($urls as $key => $url) {
$img = $this->download($url, $config);
$item = [];
if ($img['state'] === 'SUCCESS') {
$file = $img['file'];
$filename = $img['filename'];
$this->storeContent($file, $filename);
if ($this->eventSupport()) {
unset($img['file']);
event(new Catched($img));
}
}
unset($img['file']);
array_push($list, $img);
}

$response = [
'state'=> count($list) ? 'SUCCESS':'ERROR',
'list'=> $list
];

return response()->json($response);
}

/**
* Download a file.
*
* @param \Illuminate\Http\Request $request
*
* @return Array $info
*/
private function download($url, $config)
{
if (strpos($url, 'http') !== 0) {
return $this->error('ERROR_HTTP_LINK');
}
$pathRes = parse_url($url);
$img = new \SplFileInfo($pathRes['path']);
$original = $img->getFilename();
$ext = $img->getExtension();
$title = config('ueditor.hash_filename') ? md5($original) . $ext : $original;
$filename = $this->formatPath($config['path_format'], $title);
$info = [
'state' => 'SUCCESS',
'url' => $this->getUrl($filename),
'title' => $title,
'original' => $original,
'source' => $url,
'size' => 0,
'file' => '',
'filename' => $filename,
];

$context = stream_context_create(
array('http' => array(
'follow_location' => false, // don't follow redirects
))
);
$file = fopen($url, 'r', false, $context);
if ($file === false) {
$info['state'] = 'ERROR';
return $info;
}
$content = stream_get_contents($file);
fclose($file);

$info['file'] = $content;
$info['siez'] = strlen($content);
return $info;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -149,6 +238,19 @@ protected function store(UploadedFile $file, $filename)
return $this->disk->put($filename, fopen($file->getRealPath(), 'r+'));
}

/**
* Store file from content.
*
* @param string
* @param string $filename
*
* @return mixed
*/
protected function storeContent($content, $filename)
{
return $this->disk->put($filename, $content);
}

/**
* Validate the input file.
*
Expand Down
2 changes: 2 additions & 0 deletions src/UEditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function serve(Request $request)
$request->get('start'),
$request->get('size'),
$upload['fileManagerAllowFiles']);
case $upload['catcherActionName']:
return $storage->fetch($request);
default:
return $storage->upload($request);
}
Expand Down