123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- <?php
- namespace crmeb\services\upload\storage;
- use crmeb\basic\BaseUpload;
- use crmeb\exceptions\UploadException;
- use crmeb\services\DownloadImageService;
- use think\exception\ValidateException;
- use think\facade\Config;
- use think\facade\Filesystem;
- use think\File;
- use think\Image;
- class Local extends BaseUpload
- {
-
- protected $defaultPath;
-
- public $thumbWaterPath = 'thumb_water';
- public function initialize(array $config)
- {
- parent::initialize($config);
- $this->defaultPath = Config::get('filesystem.disks.' . Config::get('filesystem.default') . '.url');
- $this->waterConfig['watermark_text_font'] = app()->getRootPath() . 'public' . '/statics/font/simsunb.ttf';
- }
- protected function app()
- {
-
- }
- public function getTempKeys()
- {
-
- return $this->setError('请检查您的上传配置,视频默认oss上传');
- }
-
- public function uploadDir($path, $root = null)
- {
- if ($root === null) $root = app()->getRootPath() . 'public/';
- return str_replace('\\', '/', $root . 'uploads/' . $path);
- }
-
- protected function validDir($dir)
- {
- return is_dir($dir) == true || mkdir($dir, 0777, true) == true;
- }
-
- public function checkFilePathIsRemote(string $filePath)
- {
- return strpos($filePath, 'https:') !== false || strpos($filePath, 'http:') !== false || substr($filePath, 0, 2) === '//';
- }
-
- public function createSaveFilePath(string $filePath, string $toPath, array $config = [], $root = '/')
- {
- [$path, $ext] = $this->getFileName($filePath);
- $fileName = md5(json_encode($config) . $filePath);
- return $this->uploadDir($toPath, $root) . '/' . $fileName . '.' . $ext;
- }
-
- public function move(string $file = 'file')
- {
- $fileHandle = app()->request->file($file);
- if (!$fileHandle) {
- return $this->setError('Upload file does not exist');
- }
- if ($this->validate) {
- try {
- $error = [
- $file . '.filesize' => 'Upload filesize error',
- $file . '.fileExt' => 'Upload fileExt error',
- $file . '.fileMime' => 'Upload fileMine error'
- ];
- validate([$file => $this->validate], $error)->check([$file => $fileHandle]);
- } catch (ValidateException $e) {
- return $this->setError($e->getMessage());
- }
- }
- $fileName = Filesystem::putFileAs($this->path, $fileHandle, strtolower($fileHandle->hashName()));
- if (!$fileName)
- return $this->setError('Upload failure');
- $filePath = Filesystem::path($fileName);
- $this->fileInfo->uploadInfo = new File($filePath);
- $this->fileInfo->realName = $fileHandle->getOriginalName();
- $this->fileInfo->fileName = $this->fileInfo->uploadInfo->getFilename();
- $this->fileInfo->filePath = $this->defaultPath . '/' . str_replace('\\', '/', $fileName);
- if ($this->checkImage(root_path() . 'public' . $this->fileInfo->filePath) && $this->authThumb) {
- try {
- $this->thumb($this->fileInfo->filePath);
- } catch (\Throwable $e) {
- return $this->setError($e->getMessage());
- }
- }
- return $this->fileInfo;
- }
-
- public function stream(string $fileContent, string $key = null)
- {
- if (!$key) {
- $key = $this->saveFileName();
- }
- $dir = $this->uploadDir($this->path);
- if (!$this->validDir($dir)) {
- return $this->setError('Failed to generate upload directory, please check the permission!');
- }
- $fileName = $dir . '/' . $key;
- file_put_contents($fileName, $fileContent);
- $this->fileInfo->uploadInfo = new File($fileName);
- $this->fileInfo->realName = $key;
- $this->fileInfo->fileName = $key;
- $this->fileInfo->filePath = $this->defaultPath . '/' . $this->path . '/' . $key;
- if ($this->checkImage(root_path() . 'public' . $this->fileInfo->filePath) && $this->authThumb) {
- try {
- $this->thumb($this->fileInfo->filePath);
- } catch (\Throwable $e) {
- return $this->setError($e->getMessage());
- }
- }
- return $this->fileInfo;
- }
-
- public function down(string $fileContent, string $key = null)
- {
- if (!$key) {
- $key = $this->saveFileName();
- }
- $dir = $this->uploadDir($this->path);
- if (!$this->validDir($dir)) {
- return $this->setError('Failed to generate upload directory, please check the permission!');
- }
- $fileName = $dir . '/' . $key;
- file_put_contents($fileName, $fileContent);
- $this->downFileInfo->downloadInfo = new File($fileName);
- $this->downFileInfo->downloadRealName = $key;
- $this->downFileInfo->downloadFileName = $key;
- $this->downFileInfo->downloadFilePath = $this->defaultPath . '/' . $this->path . '/' . $key;
- return $this->downFileInfo;
- }
-
- public function thumb(string $filePath = '', string $type = 'all')
- {
- $filePath = $this->getFilePath($filePath, true);
- $data = ['big' => $filePath, 'mid' => $filePath, 'small' => $filePath];
- $this->fileInfo->filePathBig = $this->fileInfo->filePathMid = $this->fileInfo->filePathSmall = $this->fileInfo->filePathWater = $filePath;
-
- if ($filePath && !$this->checkFilePathIsRemote($filePath) && strpos($filePath, 'uploads/' . $this->thumbWaterPath) === false) {
- $dir = $this->uploadDir($this->thumbWaterPath);
- if (!$this->validDir($dir)) {
- throw new ValidateException('缩略图生成目录生成失败,目录:' . $dir);
- }
- $filePath = $this->water($filePath);
- $this->fileInfo->filePathWater = $filePath;
- $config = $this->thumbConfig;
- try {
- foreach ($this->thumb as $v) {
- if ($type == 'all' || $type == $v) {
- $height = 'thumb_' . $v . '_height';
- $width = 'thumb_' . $v . '_width';
- if (isset($config[$height]) && isset($config[$width]) && $config[$height] && $config[$width]) {
- $savePath = $this->createSaveFilePath($filePath, $this->thumbWaterPath, [$height => $config[$height], $width => $config[$width]]);
-
- if (!file_exists(root_path() . 'public' . $savePath)) {
- if ($this->authThumb) {
- $Image = Image::open(app()->getRootPath() . 'public' . $filePath);
- $Image->thumb($config[$width], $config[$height])->save(root_path() . 'public' . $savePath);
- } else {
- $savePath = $filePath;
- }
- }
- $key = 'filePath' . ucfirst($v);
- $data[$v] = $this->fileInfo->$key = $savePath;
- }
- }
- }
- } catch (\Throwable $e) {
- throw new ValidateException($e->getMessage());
- }
- }
- return $data;
- }
-
- public function water(string $filePath = '')
- {
- $waterPath = $filePath = $this->getFilePath($filePath);
-
- if ($filePath && !$this->checkFilePathIsRemote($filePath)) {
- $waterConfig = $this->waterConfig;
- if ($waterConfig['image_watermark_status'] && $filePath) {
- $waterPath = $this->createSaveFilePath($filePath, $this->thumbWaterPath, $waterConfig);
- switch ($waterConfig['watermark_type']) {
- case 1:
- if ($waterConfig['watermark_image']) $waterPath = $this->image($filePath, $waterConfig, $waterPath);
- break;
- case 2:
- $waterPath = $this->text($filePath, $waterConfig, $waterPath);
- break;
- }
- }
- }
- return $waterPath;
- }
-
- public function image(string $filePath, array $waterConfig = [], string $waterPath = '')
- {
- if (!$waterConfig) {
- $waterConfig = $this->waterConfig;
- }
- $watermark_image = $waterConfig['watermark_image'];
-
- if ($watermark_image && $this->checkFilePathIsRemote($watermark_image)) {
-
- if (!$this->isLocal($watermark_image)) {
- [$p, $e] = $this->getFileName($watermark_image);
- $name = 'water_image_' . md5($watermark_image) . '.' . $e;
- $watermark_image = $this->defaultPath . '/' . $this->thumbWaterPath . '/' . $name;
- if (!file_exists(root_path() . 'public' . $watermark_image)) {
- try {
-
- $down = app()->make(DownloadImageService::class);
- $data = $down->path($this->thumbWaterPath)->downloadImage($waterConfig['watermark_image'], $name);
- $watermark_image = $data['path'] ?? '';
- } catch (\Throwable $e) {
- throw new ValidateException('远程水印图片下载失败,原因:' . $e->getMessage());
- }
- }
- } else {
- $watermark_image = $this->getFilePath($watermark_image, true);
- }
- }
- if (!$watermark_image) {
- throw new ValidateException('请先配置水印图片');
- }
- if (!$waterPath) {
- [$path, $ext] = $this->getFileName($filePath);
- $waterPath = $path . '_water_image.' . $ext;
- }
- try {
- if (!file_exists(root_path() . 'public' . $waterPath)) {
- if ($this->authThumb) {
- $Image = Image::open(app()->getRootPath() . 'public' . $filePath);
- $Image->water(app()->getRootPath() . 'public' . $watermark_image, $waterConfig['watermark_position'] ?: 1, $waterConfig['watermark_opacity'])->save(root_path() . 'public' . $waterPath);
- } else {
- $waterPath = $filePath;
- }
- }
- } catch (\Throwable $e) {
- throw new ValidateException($e->getMessage());
- }
- return $waterPath;
- }
-
- public function text(string $filePath, array $waterConfig = [], string $waterPath = '')
- {
- if (!$waterConfig) {
- $waterConfig = $this->waterConfig;
- }
- if (!$waterConfig['watermark_text']) {
- throw new ValidateException('请先配置水印文字');
- }
- if (!$waterPath) {
- [$path, $ext] = $this->getFileName($filePath);
- $waterPath = $path . '_water_text.' . $ext;
- }
- try {
- if (!file_exists(root_path() . 'public' . $waterPath)) {
- if ($this->authThumb) {
- $Image = Image::open(app()->getRootPath() . 'public' . $filePath);
- if (strlen($waterConfig['watermark_text_color']) < 7) {
- $waterConfig['watermark_text_color'] = substr($waterConfig['watermark_text_color'], 1);
- $waterConfig['watermark_text_color'] = '#' . $waterConfig['watermark_text_color'] . $waterConfig['watermark_text_color'];
- }
- if (strlen($waterConfig['watermark_text_color']) > 7) {
- $waterConfig['watermark_text_color'] = substr($waterConfig['watermark_text_color'], 0, 7);
- }
- $Image->text($waterConfig['watermark_text'], $waterConfig['watermark_text_font'], $waterConfig['watermark_text_size'], $waterConfig['watermark_text_color'], $waterConfig['watermark_position'], [$waterConfig['watermark_x'], $waterConfig['watermark_y'], $waterConfig['watermark_text_angle']])->save(root_path() . 'public' . $waterPath);
- } else {
- $waterPath = $filePath;
- }
- }
- } catch (\Throwable $e) {
- throw new ValidateException($e->getMessage() . $e->getLine());
- }
- return $waterPath;
- }
-
- public function delete(string $filePath)
- {
- if (file_exists($filePath)) {
- try {
- unlink($filePath);
- $waterConfig = $this->waterConfig;
-
- $waterPath = $this->createSaveFilePath($filePath, $this->thumbWaterPath, $waterConfig, null);
- if (file_exists($waterPath)) unlink($waterPath);
- $config = $this->thumbConfig;
-
- foreach ($this->thumb as $v) {
- $height = 'thumb_' . $v . '_height';
- $width = 'thumb_' . $v . '_width';
- if (isset($config[$height]) && isset($config[$width]) && $config[$height] && $config[$width]) {
- $thumbPath = $this->createSaveFilePath($waterPath, $this->thumbWaterPath, [$height => $config[$height], $width => $config[$width]], null);
- if (file_exists($thumbPath)) unlink($thumbPath);
- }
- }
- return true;
- } catch (UploadException $e) {
- return $this->setError($e->getMessage());
- }
- }
- return false;
- }
- public function listbuckets(string $region, bool $line = false, bool $shared = false)
- {
- return [];
- }
- public function createBucket(string $name, string $region)
- {
- return null;
- }
- public function deleteBucket(string $name)
- {
- return null;
- }
- public function setBucketCors(string $name, string $region)
- {
- return true;
- }
- public function getRegion()
- {
- return [];
- }
- public function bindDomian(string $name, string $domain, string $region = null)
- {
- return true;
- }
- public function getDomian(string $name, string $region = null)
- {
- return [];
- }
- }
|