Commit ac49e396 authored by chentianyu's avatar chentianyu

oss

parent 21acd8b5
...@@ -8,13 +8,13 @@ ...@@ -8,13 +8,13 @@
namespace App\Services; namespace App\Services;
use App\Model\File; use Meibuyu\Rpc\UploadOss\FileModel;
use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\ConfigInterface;
use League\Flysystem\Filesystem;
use Hyperf\Filesystem\FilesystemFactory; use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\HttpMessage\Upload\UploadedFile; use Hyperf\HttpMessage\Upload\UploadedFile;
use League\Flysystem\FileExistsException; use League\Flysystem\FileExistsException;
use Meibuyu\Micro\Exceptions\HttpResponseException; use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Model\Auth;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
class UploadOssService class UploadOssService
...@@ -55,20 +55,22 @@ class UploadOssService ...@@ -55,20 +55,22 @@ class UploadOssService
} }
/** /**
* 上传图片
* @param \Hyperf\HttpMessage\Upload\UploadedFile $image * @param \Hyperf\HttpMessage\Upload\UploadedFile $image
* @param string $path * @param string $path
* @return array * @return array
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException * @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @author Zero * @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']]; $options = ['mime' => ['jpeg', 'png', 'gif', 'jpg', 'svg']];
$userId = Auth::id(); $filePath = 'meibuyu/' . $this->appDev . "/$path/images/" . today() . "/$userId/" . $image->getClientFilename();
$filePath = $this->appDev . "/upload/$path/images/" . today() . "/$userId/" . $image->getClientFilename();
$this->upload($image, $filePath, $options); $this->upload($image, $filePath, $options);
$model = new File(); $model = new FileModel();
$model = $model->newInstance([ $model = $model->newInstance([
'type' => 'image',
'module' => '',
'user_id' => $userId, 'user_id' => $userId,
'size' => num_2_file_size($image->getSize()), 'size' => num_2_file_size($image->getSize()),
'name' => $image->getClientFilename(), 'name' => $image->getClientFilename(),
...@@ -79,6 +81,33 @@ class UploadOssService ...@@ -79,6 +81,33 @@ class UploadOssService
return $model->toArray(); 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 \Hyperf\HttpMessage\Upload\UploadedFile $file
* @param $path * @param $path
...@@ -87,7 +116,7 @@ class UploadOssService ...@@ -87,7 +116,7 @@ class UploadOssService
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException * @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @author Zero * @author Zero
*/ */
public function upload(UploadedFile $file, $path, array $options = []) public function upload(UploadedFile $file, string $path, array $options = [])
{ {
if ($file->isValid()) { if ($file->isValid()) {
$options = array_merge(self::$options, $options); $options = array_merge(self::$options, $options);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment