Commit ac49e396 authored by chentianyu's avatar chentianyu
Browse files

oss

parent 21acd8b5
Loading
Loading
Loading
Loading
+36 −7
Original line number Diff line number Diff line
@@ -8,13 +8,13 @@

namespace App\Services;

use App\Model\File;
use Meibuyu\Rpc\UploadOss\FileModel;
use Hyperf\Contract\ConfigInterface;
use League\Flysystem\Filesystem;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\HttpMessage\Upload\UploadedFile;
use League\Flysystem\FileExistsException;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Model\Auth;
use Psr\Container\ContainerInterface;

class UploadOssService
@@ -55,20 +55,22 @@ class UploadOssService
    }

    /**
     * 上传图片
     * @param \Hyperf\HttpMessage\Upload\UploadedFile $image
     * @param string $path
     * @return array
     * @throws \Meibuyu\Micro\Exceptions\HttpResponseException
     * @author Zero
     */
    public function uploadImage(UploadedFile $image, string $path = 'default')
    public function uploadImage(UploadedFile $image, string $path, string $userId)
    {
        $options = ['mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg']];
        $userId = Auth::id();
        $filePath = $this->appDev . "/upload/$path/images/" . today() . "/$userId/" . $image->getClientFilename();
        $filePath = 'meibuyu/' . $this->appDev . "/$path/images/" . today() . "/$userId/" . $image->getClientFilename();
        $this->upload($image, $filePath, $options);
        $model = new File();
        $model = new FileModel();
        $model = $model->newInstance([
            'type'  => 'image',
            'module'  => '',
            'user_id' => $userId,
            'size' => num_2_file_size($image->getSize()),
            'name' => $image->getClientFilename(),
@@ -79,6 +81,33 @@ class UploadOssService
        return $model->toArray();
    }


    /**
     * 上传文件
     * @param \Hyperf\HttpMessage\Upload\UploadedFile $image
     * @param string $path
     * @return array
     * @throws \Meibuyu\Micro\Exceptions\HttpResponseException
     * @author Zero
     */
    public function uploadFile(UploadedFile $file, string $path, string $userId, array $options = [])
    {
        $filePath = 'meibuyu/' . $this->appDev . "/$path/images/" . today() . "/$userId/" . $file->getClientFilename();
        $this->upload($file, $filePath, $options);
        $model = new FileModel();
        $model = $model->newInstance([
            'type'  => 'file',
            'module'  => '',
            'user_id' => $userId,
            'size' => num_2_file_size($file->getSize()),
            'name' => $file->getClientFilename(),
            'path' => $this->urlPrefix . $filePath,
            'ext' => $file->getExtension(),
        ]);
        $model->save();
        return $model->toArray();
    }

    /**
     * @param \Hyperf\HttpMessage\Upload\UploadedFile $file
     * @param $path
@@ -87,7 +116,7 @@ class UploadOssService
     * @throws \Meibuyu\Micro\Exceptions\HttpResponseException
     * @author Zero
     */
    public function upload(UploadedFile $file, $path, array $options = [])
    public function upload(UploadedFile $file, string $path, array $options = [])
    {
        if ($file->isValid()) {
            $options = array_merge(self::$options, $options);