From a941d1129a00944ce0c895d61733ccc469e1e76b Mon Sep 17 00:00:00 2001 From: koogua Date: Wed, 2 Nov 2022 15:01:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=8C=E6=96=87=E6=9C=AC=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=99=A8=E5=A2=9E=E5=8A=A0=E7=B2=98=E8=B4=B4=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=92=8C=E8=BF=9C=E7=A8=8B=E5=9B=BE=E7=89=87=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/EditorStorage.php | 159 +++++++++++++++++++++++++++++++++ app/Validators/Answer.php | 5 ++ app/Validators/Article.php | 5 ++ app/Validators/Course.php | 5 ++ app/Validators/Help.php | 5 ++ app/Validators/Page.php | 5 ++ app/Validators/PointGift.php | 5 ++ app/Validators/Question.php | 5 ++ 8 files changed, 194 insertions(+) create mode 100644 app/Services/EditorStorage.php diff --git a/app/Services/EditorStorage.php b/app/Services/EditorStorage.php new file mode 100644 index 00000000..10fb488d --- /dev/null +++ b/app/Services/EditorStorage.php @@ -0,0 +1,159 @@ +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('/ 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'; + } + +} \ No newline at end of file diff --git a/app/Validators/Answer.php b/app/Validators/Answer.php index 32a3fbe7..bb1099a4 100644 --- a/app/Validators/Answer.php +++ b/app/Validators/Answer.php @@ -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) { diff --git a/app/Validators/Article.php b/app/Validators/Article.php index 9656b652..9987d097 100644 --- a/app/Validators/Article.php +++ b/app/Validators/Article.php @@ -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) { diff --git a/app/Validators/Course.php b/app/Validators/Course.php index eda4d6ff..b562d98f 100644 --- a/app/Validators/Course.php +++ b/app/Validators/Course.php @@ -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) { diff --git a/app/Validators/Help.php b/app/Validators/Help.php index 95d567ab..c5acab63 100644 --- a/app/Validators/Help.php +++ b/app/Validators/Help.php @@ -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) { diff --git a/app/Validators/Page.php b/app/Validators/Page.php index fa222509..2080d7a6 100644 --- a/app/Validators/Page.php +++ b/app/Validators/Page.php @@ -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) { diff --git a/app/Validators/PointGift.php b/app/Validators/PointGift.php index 5c1460ca..4f7e30b1 100644 --- a/app/Validators/PointGift.php +++ b/app/Validators/PointGift.php @@ -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) { diff --git a/app/Validators/Question.php b/app/Validators/Question.php index 1481868c..84d1e98d 100644 --- a/app/Validators/Question.php +++ b/app/Validators/Question.php @@ -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) {