| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- /**
- *
- * @copyright ©2020 点小铺
- * @author hanj
- * @link: https://dyuit.com
- * Created by VSCode
- */
- namespace app\common\model;
- use app\common\library\storage\Driver as StorageDriver;
- /**
- * 文件库模型
- * Class UploadFile
- *
- * @package app\common\model
- */
- class UploadFile extends BaseModel
- {
- protected $name = 'upload_file';
- protected $updateTime = false;
- protected $deleteTime = false;
- protected $append = ['file_path', 'cover_file_path'];
- /**
- * 关联文件库分组表
- *
- * @return \think\model\relation\BelongsTo
- */
- public function uploadGroup()
- {
- return $this->belongsTo('UploadGroup', 'group_id');
- }
- /**
- * 获取图片完整路径
- *
- * @param $value
- * @param $data
- * @return string
- */
- public function getFilePathAttr($value, $data)
- {
- if ($data['storage'] === 'local') {
- return self::$base_url . 'uploads/' . $data['file_name'];
- }
- if ($data['storage'] === 'third_party') {
- return $data['file_name'];
- }
- $domain = str_replace('http://dy-dxf.oss-cn-hangzhou.aliyuncs.com', 'https://dy-dxf.oss-cn-hangzhou.aliyuncs.com', $data['file_url']);
- return $domain . '/' . $data['file_name'];
- }
- /**
- * [getCoverFilePathAttr]
- * 获取封面图片
- *
- * @Author: Yeshihao
- * @DateTime: 2024/6/14 15:23
- * @param $value
- * @param $data
- * @return array|bool|float|int|mixed|object|\stdClass|string|null
- */
- public function getCoverFilePathAttr($value, $data)
- {
- if (empty($data['file_type'])) {
- return $this->file_path ?? '';
- }
- if ($data['file_type'] == 'video') {
- return static_file('store/img/video.png');
- }
- if ($data['file_type'] == 'file') {
- return static_file('store/img/file.png');
- }
- if ($data['file_type'] == 'pdf') {
- return static_file('store/img/pdf.png');
- }
- return $this->file_path;
- }
- /**
- * 通过mime获取文件类型
- *
- * @param string $mime
- * @return string
- * @Author Mark
- * @DateTime 2024-08-07
- */
- public static function mime2fileType(string $mime): string
- {
- $map = [
- //图片
- 'image/jpeg' => 'image',
- 'image/jpg' => 'image',
- 'image/png' => 'image',
- 'image/gif' => 'image',
- 'image/bmp' => 'image',
- 'image/webp' => 'image',
- //视频
- 'video/mp4' => 'video',
- //pdf
- 'application/pdf' => 'pdf',
- ];
- return $map[$mime] ?? '';
- }
- /**
- * 文件详情
- *
- * @param $file_id
- * @return null|static
- * @throws \think\exception\DbException
- */
- public static function detail($file_id)
- {
- return self::get($file_id);
- }
- /**
- * 根据文件名查询文件id
- *
- * @param $fileName
- * @return mixed
- */
- public static function getFildIdByName($fileName)
- {
- return (new static)->where(['file_name' => $fileName])->value('file_id');
- }
- /**
- * 查询文件id
- *
- * @param $fileId
- * @return mixed
- */
- public static function getFileName($fileId)
- {
- return (new static)->where(['file_id' => $fileId])->value('file_name');
- }
- /**
- * 添加新记录
- *
- * @param $data
- * @return false|int
- */
- public function add($data)
- {
- $data['wxapp_id'] = $data['wxapp_id'] ?? (self::$wxapp_id ?: 0);
- return $this->save($data);
- }
- /**
- * [imageArr]
- *
- * @Author: Yeshihao
- * @DateTime: 2023/6/16 20:47
- */
- public static function id2url($id)
- {
- $file = self::cache(3600)->find($id);
- return $file->file_path ?? '';
- }
- /**
- * 文件上传
- *
- * @param integer $wxappId
- * @param integer $group_id
- * @param string $fileField
- * @return static
- * @Author Mark
- * @DateTime 2024-08-22
- */
- public function uploadFile($wxappId, $group_id = -1, $fileField = 'iFile', $comapnyId = 0)
- {
- $config = Setting::getItem('storage', $wxappId);
- // 实例化存储驱动
- $StorageDriver = new StorageDriver($config);
- // 设置上传文件的信息
- $StorageDriver->setUploadFile($fileField);
- //判断上传类型
- $fileInfo = $StorageDriver->getFileInfo();
- if (!UploadFile::mime2fileType($fileInfo['type'])) {
- $this->error = '图片上传失败:mime类型错误';
- return false;
- }
- // 上传图片
- if (!$StorageDriver->upload()) {
- $this->error = '图片上传失败:' . $StorageDriver->getError();
- return false;
- }
- // 图片上传路径
- $fileName = $StorageDriver->getFileName();
- // 图片信息
- $fileInfo = $StorageDriver->getFileInfo();
- $uploadFile = self::addUploadFile($group_id, $fileName, $fileInfo, $config, $comapnyId);
- // 图片上传成功
- return $uploadFile;
- }
- /**
- * 写入数据库
- *
- * @param int $group_id
- * @param string $fileName
- * @param array $fileInfo
- * @param array $config
- * @return static
- * @Author Mark
- * @DateTime 2024-08-22
- */
- private function addUploadFile($group_id, $fileName, $fileInfo, $config, $companyId)
- {
- // 存储引擎
- $storage = $config['default'];
- // 存储域名
- $fileUrl = isset($config['engine'][$storage]['domain'])
- ? $config['engine'][$storage]['domain'] : '';
- // 添加文件库记录
- $model = new self;
- $model->add([
- 'group_id' => $group_id > 0 ? (int)$group_id : 0,
- 'storage' => $storage,
- 'file_url' => $fileUrl,
- 'file_name' => $fileName,
- 'file_size' => $fileInfo['size'],
- 'file_type' => self::mime2fileType($fileInfo['type']),
- 'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION),
- 'origin_name' => $fileInfo['name'],
- 'mime' => $fileInfo['type'],
- 'company_id' => $companyId,
- ]);
- return $model;
- }
- }
|