1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 20:06:09 +08:00

富文本编辑器增加粘贴图片和远程图片本地化

This commit is contained in:
koogua 2022-11-02 15:01:25 +08:00
parent 301557abbc
commit a941d1129a
8 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,159 @@
<?php
/**
* @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.koogua.com
*/
namespace App\Services;
use App\Library\Utils\FileInfo;
use App\Models\Upload as UploadModel;
use App\Repos\Upload as UploadRepo;
use Phalcon\Text;
class EditorStorage extends Storage
{
public function handle($content)
{
$content = $this->handleBase64Image($content);
$content = $this->handleRemoteImage($content);
return trim($content);
}
protected function handleBase64Image($content)
{
$content = preg_replace("/data-ke-src=\"(.*?)\"/", '', $content);
preg_match_all('/src="(data:image\/(\S+);base64,(\S+))"/U', $content, $matches);
if (count($matches[3]) > 0) {
foreach ($matches[3] as $key => $value) {
$imageUrl = $this->uploadBase64Image($matches[3][$key], $matches[2][$key]);
$content = str_replace($matches[1][$key], $imageUrl, $content);
}
}
return $content;
}
protected function handleRemoteImage($content)
{
$baseUrl = $this->getBaseUrl();
preg_match_all('/<img src="(\S+)"/', $content, $matches);
if (count($matches[1]) > 0) {
foreach ($matches[1] as $key => $value) {
if (!Text::startsWith($value, $baseUrl)) {
$imageUrl = $this->uploadRemoteImage($value);
$content = str_replace($value, $imageUrl, $content);
}
}
}
return $content;
}
protected function uploadBase64Image($encodeContent, $extension)
{
$keyName = $this->generateFileName($extension, '/img/content/');
$content = base64_decode($encodeContent);
$md5 = md5($content);
$uploadRepo = new UploadRepo();
$upload = $uploadRepo->findByMd5($md5);
if (!$upload) {
$uploadPath = $this->putString($keyName, $content);
if ($uploadPath) {
$upload = new UploadModel();
$upload->type = UploadModel::TYPE_CONTENT_IMG;
$upload->mime = FileInfo::getMimeTypeByExt($extension);
$upload->name = pathinfo($uploadPath, PATHINFO_BASENAME);
$upload->size = strlen($content);
$upload->path = $uploadPath;
$upload->md5 = $md5;
$upload->create();
}
$imageUrl = $uploadPath ? $this->getImageUrl($uploadPath) : '';
} else {
$imageUrl = $this->getImageUrl($upload->path);
}
return $imageUrl;
}
protected function uploadRemoteImage($remoteUrl)
{
$extension = $this->getImageExtension($remoteUrl);
$content = file_get_contents($remoteUrl);
if ($content === false) return $remoteUrl;
$keyName = $this->generateFileName($extension, '/img/content/');
$md5 = md5($content);
$uploadRepo = new UploadRepo();
$upload = $uploadRepo->findByMd5($md5);
if (!$upload) {
$uploadPath = $this->putString($keyName, $content);
if ($uploadPath) {
$upload = new UploadModel();
$upload->type = UploadModel::TYPE_CONTENT_IMG;
$upload->mime = FileInfo::getMimeTypeByExt($extension);
$upload->name = pathinfo($uploadPath, PATHINFO_BASENAME);
$upload->size = strlen($content);
$upload->path = $uploadPath;
$upload->md5 = $md5;
$upload->create();
}
$imageUrl = $uploadPath ? $this->getImageUrl($uploadPath) : $remoteUrl;
} else {
$imageUrl = $this->getImageUrl($upload->path);
}
return $imageUrl;
}
/**
* 例如https://abc.com/123.jpg!large这类不规范地址需要特殊处理
*
* @param string $imageUrl
* @return string
*/
protected function getImageExtension($imageUrl)
{
$path = parse_url($imageUrl, PHP_URL_PATH);
preg_match('/(\S+)\.(png|gif|jpg|jpeg)/i', $path, $matches);
return $matches[2] ? strtolower($matches[2]) : 'jpg';
}
}

View File

@ -15,6 +15,7 @@ use App\Models\Reason as ReasonModel;
use App\Models\User as UserModel;
use App\Repos\Answer as AnswerRepo;
use App\Repos\Question as QuestionRepo;
use App\Services\EditorStorage as EditorStorageService;
class Answer extends Validator
{
@ -58,6 +59,10 @@ class Answer extends Validator
{
$value = $this->filter->sanitize($content, ['trim']);
$storage = new EditorStorageService();
$value = $storage->handle($value);
$length = kg_strlen($value);
if ($length < 10) {

View File

@ -14,6 +14,7 @@ use App\Library\Validators\Common as CommonValidator;
use App\Models\Article as ArticleModel;
use App\Models\Reason as ReasonModel;
use App\Repos\Article as ArticleRepo;
use App\Services\EditorStorage as EditorStorageService;
class Article extends Validator
{
@ -94,6 +95,10 @@ class Article extends Validator
{
$value = $this->filter->sanitize($content, ['trim']);
$storage = new EditorStorageService();
$value = $storage->handle($value);
$length = kg_strlen($value);
if ($length < 10) {

View File

@ -13,6 +13,7 @@ use App\Exceptions\BadRequest as BadRequestException;
use App\Library\Validators\Common as CommonValidator;
use App\Models\Course as CourseModel;
use App\Repos\Course as CourseRepo;
use App\Services\EditorStorage as EditorStorageService;
class Course extends Validator
{
@ -119,6 +120,10 @@ class Course extends Validator
{
$value = $this->filter->sanitize($details, ['trim']);
$storage = new EditorStorageService();
$value = $storage->handle($value);
$length = kg_strlen($value);
if ($length > 30000) {

View File

@ -12,6 +12,7 @@ use App\Caches\MaxHelpId as MaxHelpIdCache;
use App\Exceptions\BadRequest as BadRequestException;
use App\Models\Help as HelpModel;
use App\Repos\Help as HelpRepo;
use App\Services\EditorStorage as EditorStorageService;
class Help extends Validator
{
@ -90,6 +91,10 @@ class Help extends Validator
{
$value = $this->filter->sanitize($content, ['trim']);
$storage = new EditorStorageService();
$value = $storage->handle($value);
$length = kg_strlen($value);
if ($length < 10) {

View File

@ -13,6 +13,7 @@ use App\Exceptions\BadRequest as BadRequestException;
use App\Library\Validators\Common as CommonValidator;
use App\Models\Page as PageModel;
use App\Repos\Page as PageRepo;
use App\Services\EditorStorage as EditorStorageService;
class Page extends Validator
{
@ -124,6 +125,10 @@ class Page extends Validator
{
$value = $this->filter->sanitize($content, ['trim']);
$storage = new EditorStorageService();
$value = $storage->handle($value);
$length = kg_strlen($value);
if ($length < 10) {

View File

@ -13,6 +13,7 @@ use App\Exceptions\BadRequest as BadRequestException;
use App\Library\Validators\Common as CommonValidator;
use App\Models\PointGift as PointGiftModel;
use App\Repos\PointGift as PointGiftRepo;
use App\Services\EditorStorage as EditorStorageService;
class PointGift extends Validator
{
@ -86,6 +87,10 @@ class PointGift extends Validator
{
$value = $this->filter->sanitize($details, ['trim']);
$storage = new EditorStorageService();
$value = $storage->handle($value);
$length = kg_strlen($value);
if ($length > 30000) {

View File

@ -13,6 +13,7 @@ use App\Exceptions\BadRequest as BadRequestException;
use App\Models\Question as QuestionModel;
use App\Models\Reason as ReasonModel;
use App\Repos\Question as QuestionRepo;
use App\Services\EditorStorage as EditorStorageService;
class Question extends Validator
{
@ -106,6 +107,10 @@ class Question extends Validator
{
$value = $this->filter->sanitize($content, ['trim']);
$storage = new EditorStorageService();
$value = $storage->handle($value);
$length = kg_strlen($value);
if ($length > 30000) {