| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace App\Module\File\Logics;
- use App\Module\File\Config\StorageConfig;
- use App\Module\File\Events\FileDeletedEvent;
- use App\Module\File\Events\FileUploadedEvent;
- use App\Module\File\Events\ImageDeletedEvent;
- use App\Module\File\Events\ImageUploadedEvent;
- use App\Module\File\Models\FileFile;
- use App\Module\File\Models\FileImg;
- use App\Module\File\Services\ImgService;
- use App\Module\File\Services\TemporaryService;
- use App\Module\File\Services\UploadService;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Storage as FacadesStorage;
- /**
- * 文件逻辑类
- *
- * 实现文件操作的具体逻辑
- */
- class FileLogic
- {
- /**
- * 上传文件
- *
- * @param UploadedFile $file 上传的文件
- * @param int $userId 用户ID
- * @param string $reType 关联类型
- * @param int $reId 关联ID
- * @return FileFile 文件模型
- */
- public function uploadFile(UploadedFile $file, int $userId, string $reType = '', int $reId = 0)
- {
- $dirLogic = new DirLogic();
- $dir = $dirLogic->getDir($userId);
- $path = $dir . '/' . uniqid() . '.' . $file->getClientOriginalExtension();
- $disk = StorageConfig::getStorage();
- $res = FacadesStorage::disk($disk)->putFileAs(dirname($path), $file, basename($path));
- if ($res === false) {
- throw new \Exception('服务器错误,请联系客服.');
- }
- $fileModel = new FileFile();
- $fileModel->path = $path;
- $fileModel->re_type = $reType;
- $fileModel->re_id = $reId;
- $fileModel->o_name = $file->getClientOriginalName();
- $fileModel->fsize = $file->getSize();
- $fileModel->type1 = $file->getClientOriginalExtension();
- $fileModel->save();
- // 触发文件上传事件
- event(new FileUploadedEvent($fileModel));
- return $fileModel;
- }
- /**
- * 上传图片
- *
- * @param UploadedFile $file 上传的图片
- * @param int $userId 用户ID
- * @param bool $private 是否私有
- * @param string $reType 关联类型
- * @param int $reId 关联ID
- * @return FileImg 图片模型
- */
- public function uploadImage(UploadedFile $file, int $userId, bool $private = false, string $reType = '', int $reId = 0)
- {
- $uploader = new UploadService($userId);
- $fileImg = $uploader->uploadImg($file, $private);
- if (!empty($reType) && $reId > 0) {
- $fileImg->re_type = $reType;
- $fileImg->re_id = $reId;
- $fileImg->save();
- }
- // 触发图片上传事件
- event(new ImageUploadedEvent($fileImg));
- return $fileImg;
- }
- /**
- * 获取文件URL
- *
- * @param int $fileId 文件ID
- * @return string 文件URL
- */
- public function getFileUrl(int $fileId)
- {
- $file = FileFile::find($fileId);
- if (!$file) {
- return '';
- }
- $disk = StorageConfig::getStorage();
- return FacadesStorage::disk($disk)->url($file->path);
- }
- /**
- * 获取图片URL
- *
- * @param int $imageId 图片ID
- * @param bool $private 是否私有
- * @return string 图片URL
- */
- public function getImageUrl(int $imageId, bool $private = false)
- {
- $imgService = new ImgService();
- return $imgService->getPicUrl4Id($imageId, $private);
- }
- /**
- * 删除文件
- *
- * @param int $fileId 文件ID
- * @return bool 是否成功
- */
- public function deleteFile(int $fileId)
- {
- $file = FileFile::find($fileId);
- if (!$file) {
- return false;
- }
- $disk = StorageConfig::getStorage();
- $path = $file->path;
- $result = $file->delete();
- if ($result) {
- // 触发文件删除事件
- event(new FileDeletedEvent($fileId, $path, $disk));
- }
- return $result;
- }
- /**
- * 删除图片
- *
- * @param int $imageId 图片ID
- * @return bool 是否成功
- */
- public function deleteImage(int $imageId)
- {
- $image = FileImg::find($imageId);
- if (!$image) {
- return false;
- }
- $path = $image->path;
- $disk = $image->storage_disk;
- $result = $image->delete();
- if ($result) {
- // 触发图片删除事件
- event(new ImageDeletedEvent($imageId, $path, $disk));
- }
- return $result;
- }
- /**
- * 保存临时文件
- *
- * @param string $ext 文件扩展名
- * @param string $content 文件内容
- * @return string 临时文件路径
- */
- public function saveTempFile(string $ext, string $content)
- {
- $temporaryService = new TemporaryService();
- return $temporaryService->save($ext, $content);
- }
- /**
- * 获取临时文件URL
- *
- * @param string $path 临时文件路径
- * @return string 临时文件URL
- */
- public function getTempFileUrl(string $path)
- {
- $temporaryService = new TemporaryService();
- return $temporaryService->getDownUrl($path);
- }
- }
|