Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/list-html-encoded-entities
Browse files Browse the repository at this point in the history
  • Loading branch information
roelofr committed Sep 25, 2022
2 parents df078c7 + 4f9de04 commit 7a57b2a
Show file tree
Hide file tree
Showing 15 changed files with 424 additions and 67 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [UNRELEASED]

### Added
- Guzzle is now a dependency of this project.

### Changed
- Improved image upload handling, using Laravel-native libraries
- Improved link metadata retrieval, using Laravel-native libraries

### Fixed

- Fixed HTML escaping on list and raw HTML fields. (#80 by @Harrk)
Expand Down
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"ext-exif": "*",
"ext-json": "*",
"codex-team/editor.js": "*",
"guzzlehttp/guzzle": "^6.0|^7.0",
"illuminate/support": "^8.0 || ^9.0",
"laravel/nova": "^4.0",
"spatie/image": "^1.7 || ^2.0"
Expand All @@ -29,7 +30,16 @@
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"files": [
"tests/helpers.php"
]
},
"scripts": {
"test": "phpunit"
},
"scripts-descriptions": {
"test": "Test application using PHPUnit."
},
"extra": {
"laravel": {
Expand Down
92 changes: 52 additions & 40 deletions src/Http/Controllers/EditorJsImageUploadController.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
<?php

declare(strict_types=1);

namespace Advoor\NovaEditorJs\Http\Controllers;

use finfo;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
use Laravel\Nova\Http\Requests\NovaRequest;
use Spatie\Image\Exceptions\InvalidManipulation;
use Spatie\Image\Image;

class EditorJsImageUploadController extends Controller
{
private const VALID_IMAGE_MIMES = [
'image/jpeg',
'image/webp',
'image/gif',
'image/png',
'image/svg+xml',
];

/**
* Upload file
*
* @param NovaRequest $request
* @return array
* Upload file.
*/
public function file(NovaRequest $request)
public function file(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'image' => 'required|image',
]);

if ($validator->fails()) {
return [
'success' => 0
];
return response()->json([
'success' => 0,
]);
}

$path = $request->file('image')->store(
Expand All @@ -51,60 +64,59 @@ public function file(NovaRequest $request)
$thumbnails = $this->applyThumbnails($path);
}

return [
return response()->json([
'success' => 1,
'file' => [
'url' => Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->url($path),
'thumbnails' => $thumbnails
]
];
]);
}

/**
* @param NovaRequest $request
* @return array
* "Upload" a URL.
*/
public function url(NovaRequest $request)
public function url(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'url' => [
'required',
'active_url',
function ($attribute, $value, $fail) {
$imageDetails = getimagesize($value);

if (!in_array($imageDetails['mime'] ?? '', [
'image/jpeg',
'image/webp',
'image/gif',
'image/png',
'image/svg+xml',
])) {
$fail($attribute . ' is invalid.');
}
},
],
'url' => 'required|url',
]);

if ($validator->fails()) {
return [
'success' => 0
];
return response()->json([
'success' => 0,
]);
}

$url = $request->input('url');
$imageContents = file_get_contents($url);
$name = parse_url(substr($url, strrpos($url, '/') + 1))['path'];
$nameWithPath = config('nova-editor-js.toolSettings.image.path') . '/' . uniqid() . $name;

Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->put($nameWithPath, $imageContents);
// Fetch URL
try {
$response = Http::timeout(5)->get($url)->throw();
} catch (ConnectionException | RequestException) {
return response()->json([
'success' => 0,
]);
}

// Validate mime type
$mime = (new finfo())->buffer($response->body(), FILEINFO_MIME_TYPE);
if (! in_array($mime, self::VALID_IMAGE_MIMES, true)) {
return response()->json([
'success' => 0,
]);
}

return [
$urlBasename = basename(parse_url(url($url), PHP_URL_PATH));
$nameWithPath = config('nova-editor-js.toolSettings.image.path') . '/' . uniqid() . $urlBasename;
Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->put($nameWithPath, $response->body());

return response()->json([
'success' => 1,
'file' => [
'url' => Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->url($nameWithPath)
]
];
]);
}

/**
Expand Down
57 changes: 31 additions & 26 deletions src/Http/Controllers/EditorJsLinkController.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
<?php

declare(strict_types=1);

namespace Advoor\NovaEditorJs\Http\Controllers;

use DOMDocument;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Laravel\Nova\Http\Requests\NovaRequest;

class EditorJsLinkController extends Controller
{
/**
* Upload file
*
* @param NovaRequest $request
* @return array
* Determine microdata for the given file.
*/
public function fetch(NovaRequest $request)
public function fetch(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'url' => 'required|active_url',
'url' => 'required|url',
]);

if ($validator->fails()) {
return [
'success' => 0
];
return response()->json([
'success' => 0,
]);
}

$contents = file_get_contents($request->input('url'));
// Contents
try {
$url = $request->input('url');
$response = Http::timeout(5)->get($url)->throw();
} catch (ConnectionException | RequestException) {
return response()->json([
'success' => 0,
]);
}

$doc = new \DOMDocument();
@$doc->loadHTML($contents);
$doc = new DOMDocument();
@$doc->loadHTML((string) $response->getBody());
$nodes = $doc->getElementsByTagName('title');
$title = $nodes->item(0)->nodeValue;
$description = '';
Expand All @@ -48,20 +60,13 @@ public function fetch(NovaRequest $request)
}
}

$results = [
return response()->json([
'success' => 1,
'meta' => [
'title' => $title,
'meta' => array_filter([
'title' => $title ?? $url,
'description' => $description,
]
];

if (!empty($imageUrl)){
$results['meta']['image'] = [
'url' => $imageUrl,
];
}

return $results;
'imageUrl' => $imageUrl,
]),
]);
}
}
Loading

0 comments on commit 7a57b2a

Please sign in to comment.