Skip to content

Commit

Permalink
'大力优化文件管理'
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Feb 23, 2017
1 parent 97d359b commit e0b87a4
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 96 deletions.
43 changes: 10 additions & 33 deletions common/actions/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class UploadAction extends Action
*/
public $path;

/**
* @var string URL path to directory where files will be uploaded
*/
public $url;

/**
* @var string Validator name
*/
Expand Down Expand Up @@ -79,20 +74,6 @@ class UploadAction extends Action
*/
public function init()
{
if ($this->url === null) {
throw new InvalidConfigException('The "url" attribute must be set.');
} else {
$this->url = rtrim($this->url, '/') . '/';
}
if ($this->path === null) {
throw new InvalidConfigException('The "path" attribute must be set.');
} else {
$this->path = rtrim(Yii::getAlias($this->path), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

if (!FileHelper::createDirectory($this->path)) {
throw new InvalidCallException("Directory specified in 'path' attribute doesn't exist or cannot be created.");
}
}
if (Yii::$app->request->get($this->uploadQueryParam)) {
$this->uploadParam = Yii::$app->request->get($this->uploadQueryParam);
}
Expand Down Expand Up @@ -130,7 +111,7 @@ public function run()
}

}
private function uploadMore($files) {
private function uploadMore(array $files) {
$res = [];
foreach ($files as $file) {

Expand All @@ -139,12 +120,7 @@ private function uploadMore($files) {
}
return $res;
}

/**
* @param UploadedFile $file
* @return array|mixed
*/
private function uploadOne($file)
private function uploadOne(UploadedFile $file)
{
try {
$model = new DynamicModel(compact('file'));
Expand All @@ -159,12 +135,13 @@ private function uploadOne($file)
if ($this->unique === true && $file->extension) {
$file->name = uniqid() . '.' . $file->extension;
}
if ($model->file->saveAs($this->path . $file->name)) {
$filePath = $this->path . $file->name;
if (Yii::$app->storage->upload($filePath, $file->tempName)) {
/**
* @var \common\models\Attachment $attachment
*/
$attachment = new $this->modelClass;
$attachment->url = $this->url . $file->name;
$attachment->path = $filePath;
$attachment->name = $file->name;
$attachment->extension = $file->extension;
$attachment->type = $file->type;
Expand All @@ -177,7 +154,7 @@ private function uploadOne($file)
}
$result = [
'id' => $attachment->id,
'url' => $attachment->url,
'url' => $attachment->getUrl(),
'extension' => $attachment->extension,
'type' => $attachment->type,
'size' => $attachment->size,
Expand All @@ -186,15 +163,15 @@ private function uploadOne($file)
if ($this->uploadOnlyImage !== true) {
$result['filename'] = $attachment->name;
}
if ($this->itemCallback != null) {
$result = call_user_func($this->itemCallback, $result);
}
}
}catch (Exception $e) {
} catch (Exception $e) {
$result = [
'error' => $e->getMessage()
];
}
if ($this->itemCallback instanceof \Closure) {
$result = call_user_func($this->itemCallback, $result);
}
return $result;
}
}
25 changes: 1 addition & 24 deletions common/actions/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ public function actions()
return [
'redactor-files-get' => [
'class' => 'vova07\imperavi\actions\GetAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'type' => GetAction::TYPE_FILES,
],
'redactor-image-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment',
'callback' => function($result) {
return !isset($result['files'][0]['error']) ? [
Expand All @@ -42,14 +38,10 @@ public function actions()
],
'redactor-images-get' => [
'class' => 'vova07\imperavi\actions\GetAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'type' => GetAction::TYPE_IMAGES,
],
'redactor-file-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'uploadOnlyImage' => false,
'modelClass' => 'common\models\Attachment',
'callback' => function($result) {
Expand All @@ -63,36 +55,27 @@ public function actions()
],
'image-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment'
],
'avatar-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment',
'path' => 'avatar',
'validatorOptions' => ['minWidth' => 100, 'minHeight' => 100, 'underWidth' => '图片宽高不要小于100x100', 'underHeight' => '图片宽高不要小于100x100']
],
'file-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment',
'uploadOnlyImage' => false
],
'images-upload' => [
'class' => 'common\actions\UploadAction',
'multiple' => true,
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment'
],
'backend-files-upload' => [
'class' => 'common\actions\UploadAction',
'multiple' => true,
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment',
'uploadOnlyImage' => false,
'itemCallback' => function ($result) {
Expand All @@ -102,8 +85,6 @@ public function actions()
],
'md-image-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment',
'callback' => function($result) {
return !isset($result['files'][0]['error']) ? [
Expand All @@ -117,8 +98,6 @@ public function actions()
],
'im-image-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment',
'callback' => function($result) {
return !isset($result['files'][0]['error']) ? [
Expand All @@ -136,8 +115,6 @@ public function actions()
],
'im-file-upload' => [
'class' => 'common\actions\UploadAction',
'url' => Yii::$app->storage->baseUrl,
'path' => Yii::$app->storage->basePath,
'modelClass' => 'common\models\Attachment',
'uploadOnlyImage' => false,
'callback' => function($result) {
Expand Down
26 changes: 13 additions & 13 deletions common/components/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@
namespace common\components;


use creocoder\flysystem\Filesystem;
use yii\base\Component;
use creocoder\flysystem\LocalFilesystem;

class Storage extends Component
{
public $baseUrl;

public $basePath;
/**
* @var array|string|Filesystem
*/
public $fs;

public function init()
{
parent::init();
$this->baseUrl = \Yii::getAlias($this->baseUrl);
$this->basePath = \Yii::getAlias($this->basePath);
}

public function path2url($path)
{
return str_replace($this->basePath, $this->baseUrl, $path);
}
public function url2path($url)
{
return str_replace($this->baseUrl, $this->basePath, $url);
$this->fs = \Yii::createObject($this->fs);
}

public function getPath($filename)
{
return $this->basePath . '/' . $filename;
return $this->fs->getAdapter()->applyPathPrefix($filename);
}

public function getUrl($filename)
{
return $this->baseUrl . '/' . $filename;
}

public function upload($target, $source)
{
return $this->fs->write($target, file_get_contents($source));
}

}
3 changes: 2 additions & 1 deletion common/components/flysystem/QiniuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

namespace common\components\flysystem;

use League\Flysystem\Adapter\AbstractAdapter;
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Config;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;

class QiniuAdapter implements AdapterInterface
class QiniuAdapter extends AbstractAdapter
{
use NotSupportingVisibilityTrait;
/**
Expand Down
9 changes: 4 additions & 5 deletions common/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@
],*/
],
],
'fs' => [
'class' => 'creocoder\flysystem\LocalFilesystem',
'path' => '@storagePath/upload',
],
'storage' => [
'class' => 'common\\components\\Storage',
'basePath' => '@storagePath/upload',
'fs' => [
'class' => 'creocoder\flysystem\LocalFilesystem',
'path' => '@storagePath/upload',
],
'baseUrl' => '@storageUrl/upload'
],
'log' => [
Expand Down
30 changes: 16 additions & 14 deletions common/models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
* @property integer $user_id
* @property string $name
* @property string $title
* @property string $url
* @property string $path
* @property string $extension
* @property string $description
* @property string $hash
* @property integer $size
* @property string $type
* @property integer $created_at
* @property integer $updated_at
* @property string $url
*/
class Attachment extends \yii\db\ActiveRecord
{
Expand All @@ -40,7 +41,7 @@ public static function tableName()
public function rules()
{
return [
[['url', 'hash'], 'required'],
[['path', 'hash'], 'required'],
[['user_id', 'size'], 'integer'],
[['name', 'title', 'description', 'type', 'extension'], 'string', 'max' => 255],
[['hash'], 'string', 'max' => 64]
Expand Down Expand Up @@ -77,19 +78,19 @@ public function behaviors()
];
}

public function getFilePath()
public function getUrl()
{
return Yii::$app->storage->basePath . DIRECTORY_SEPARATOR . $this->name;
return Yii::$app->storage->getUrl($this->path);
}

public function getAbsolutePath()
{
return Yii::$app->storage->basePath . DIRECTORY_SEPARATOR . $this->name;
return Yii::$app->storage->fs->getAdapter()->applyPathPrefix($this->path);
}

public function getFile()
{
return Yii::$app->fs->get($this->name);
return Yii::$app->storage->fs->get($this->path);
}

public function getThumb($width, $height, $options = [])
Expand All @@ -100,20 +101,21 @@ public function getThumb($width, $height, $options = [])
$options = $this->getDefaultThumbOptions($options);

$thumbFile = $this->getThumbFilename($width, $height, $options);
$thumbPath = \Yii::$app->storage->getPath($thumbFile);
$thumbPath = pathinfo($this->path, PATHINFO_DIRNAME) . $thumbFile;

if (!\Yii::$app->get('fs')->has($thumbPath)) {
if (!\Yii::$app->storage->fs->has($thumbPath)) {
$this->makeThumbStorage($thumbPath, $width, $height, $options);
}
return \Yii::$app->storage->path2url($thumbPath);
return \Yii::$app->storage->getUrl($thumbPath);
}

// TODO 这里只能用localFilesystem
protected function makeThumbStorage($thumbPath, $width, $height, $options)
{
Image::thumbnail($this->getFilePath(), $width, $height)->save($thumbPath);
Image::thumbnail($this->getAbsolutePath(), $width, $height)->save($thumbPath);

}

protected function getThumbFilename($width, $height, $options)
{
return 'thumb_' . $this->primaryKey . '_' . $width . 'x' . $height . '_' . $options['offset'][0] . '_' . $options['offset'][1] . '_' . $options['mode'] . '.' . $options['extension'];
Expand Down Expand Up @@ -153,7 +155,7 @@ public function deleteThumbs()
$collection = $this->getThumbs();
if (!empty($collection)) {
foreach ($collection as $item) {
Yii::$app->fs->delete($item);
Yii::$app->storage->fs->delete($item);
}
}
}
Expand All @@ -162,7 +164,7 @@ public function getThumbs()
{
$pattern = 'thumb_' . $this->primaryKey . '_';

$allFiles = \Yii::$app->fs->listContents();
$allFiles = \Yii::$app->storage->fs->listContents();

$collection = [];
foreach ($allFiles as $file) {
Expand All @@ -176,7 +178,7 @@ public function afterDelete()
{
parent::afterDelete();
// 文件删了
$filePath = $this->getFilePath();
$filePath = $this->getAbsolutePath();
@unlink($filePath);
}

Expand Down
Loading

0 comments on commit e0b87a4

Please sign in to comment.