123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\Material;
- use App\Support\DouYinApi;
- use App\Support\EmailQueue;
- use Illuminate\Support\Facades\Storage;
- class MaterialService
- {
- /**
- *
- * */
- public static function uploadMaterial($appId, $type, $file, $videoTitle, $videoDesc, &$failList)
- {
- $insertData = $failList = [];
- if($type == 1) {
- $errno = MaterialService::upload($type, $file, $ossUrl);
- if($errno) return $errno;
- $insertData = [
- 'app_id' => $appId, 'resource_type' => $type, 'oss_url' => $ossUrl, 'title' => $videoTitle,
- 'desc' => $videoDesc,
- ];
- } else {
- if(!is_array($file)) $file = [$file];
- foreach ($file as $datum) {
- $errno = MaterialService::upload($type, $datum, $ossUrl);
- if($errno) array_push($failList, $datum->getClientOriginalName());
- $insertData[] = [
- 'app_id' => $appId, 'resource_type' => $type, 'oss_url' => $ossUrl, 'title' => $datum->getClientOriginalName()
- ];
- }
- }
- # 存储数据
- $result = Material::insert($insertData);
- if(!$result) return 2310;
- return 0;
- }
- /**
- * 获取素材列表
- * */
- public static function materialList($appId, $type, $keyword, $page, $pageSize)
- {
- list($list, $count) = Material::mediaList($appId, $type, $keyword, $page, $pageSize);
- return [$list, $count];
- }
- /**
- * 上传素材至OSS
- * */
- public static function upload($type, $file, &$fileUrl)
- {
- try{
- if (empty($file) || !$file->isValid()) {
- return 2311;
- }
- $originalExtension = strtolower($file->getClientOriginalExtension()); //文件扩展名
- if(!$originalExtension) $originalExtension = $file->guessExtension();
- $realPath = $file->getRealPath(); //临时文件的绝对路径
- $mimeType = $file->getClientMimeType(); // image/jpeg
- $fileSize = $file->getClientSize();
- switch ($type) {
- case 1: // 视频
- if (!in_array($originalExtension, ['mp4', 'm3u8'])) {
- return 2303;
- }
- # 视频文件大小限制
- if($fileSize > 100 * 1024 * 1024) {
- return 2308;
- }
- break;
- case 2: // 图片
- if (!in_array($originalExtension, ['jpg', 'png'])) {
- Log::logError('上传的图片类型为:', ['type' => $originalExtension, 'info' => $file->guessExtension() ], 'uploadMaterial');
- return 2301;
- }
- if($fileSize > 10 * 1024 * 1024) {
- return 2306;
- }
- // list($width, $height, $imageType, $attr) = getimagesize($realPath);
- // if($width * $height > 1555200) {
- // return 2312;
- // }
- break;
- }
- // 上传文件
- $filename = date('YmdHis') . '-' . uniqid() . '.' . $originalExtension;
- // 使用我们新建的uploads本地存储空间(目录)
- $result = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
- if(!$result)
- return 2304;
- $localFilePath = '../storage/uploads/'.$filename;
- # 上传素材到阿里oss
- $oss = new OssService('dou-smallapp');
- $ossFile = $oss->upload($originalExtension, $localFilePath, $mimeType.'/'.date("Y-m-d",time()));
- $fileUrl = $ossFile['oss-request-url'];
- @unlink($localFilePath);
- } catch (\Exception $e) {
- EmailQueue::rPush('素材上传过程发生异常', $e->getTraceAsString(), ['song.shen@kuxuan-inc.com'], '素材上传过程发生异常');
- Log::logError('素材上传过程发生异常', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ], 'UploadMaterial-Exception');
- return 2305;
- }
- return 0;
- }
- public static function uploadMaterialToDouYin($id, $appId, $resourceType, $ossUrl, $title, $desc) {
- # 通过链接解析文件后缀
- $pathInfo = pathinfo($ossUrl);
- $originalExtension = $pathInfo['extension'];
- // 上传素材到抖音
- $mediaData = DouYinApi::uploadMedia($appId, $resourceType, $ossUrl, $title, $desc, $originalExtension);
- $updateData['open_pic_id'] = $mediaData['image_result']['open_pic_id'] ?? '';
- $updateData['open_video_id'] = $mediaData['video_result']['open_video_id'] ?? '';
- if(empty($updateData['open_pic_id']) && empty($updateData['open_video_id'])) {
- return 2305;
- }
- // 更新系统数据
- if(1 == $resourceType) {
- $updateData['status'] = 1;
- } else {
- $updateData['status'] = 2;
- }
- $res = Material::updateById($id, $updateData);
- if(!$res) return 500;
- return 0;
- }
- }
|