Skip to content

Commit

Permalink
'大力优化文件管理'
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Feb 24, 2017
1 parent 97d359b commit b021cab
Show file tree
Hide file tree
Showing 23 changed files with 422 additions and 127 deletions.
11 changes: 0 additions & 11 deletions backend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'controllerMap'=>[
'file-manager-elfinder' => [
'class' => 'mihaildev\elfinder\Controller',
'disabledCommands' => ['netmount'],
'roots' => [
[
'baseUrl' => '@storageUrl',
'basePath' => '@storagePath',
'path' => '/',
]
]
],
'upload' => \common\actions\UploadController::className()
],
'components' => [
Expand Down
19 changes: 16 additions & 3 deletions backend/models/MediaItem.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
<?php

namespace backend\models;

/**
* HassCMS (http:https://www.hassium.org/)
*
* @link http:https://github.com/hasscms for the canonical source repository
* @copyright Copyright (c) 2016-2099 Hassium Software LLC.
* @license http:https://opensource.org/licenses/gpl-license.php GNU Public License
*/
namespace backend\models;
use common\models\Attachment;

/**
*
* @package hass\package_name
* @author zhepama <[email protected]>
* @since 0.1.0
*
*/
class MediaItem
{
const TYPE_FILE = 'file';
Expand Down Expand Up @@ -72,8 +86,7 @@ public function __construct($path, $size, $lastModified, $type, $publicUrl)
*/
public static function createFromAttachment($attachment)
{
$file = $attachment->getFile();
$item = new static($attachment->getAbsolutePath(),$file->getSize(),$file->getTimestamp(),$file->getType(),$attachment->url);
$item = new static($attachment->path,$attachment->size, $attachment->updated_at, 'file', $attachment->url);
return $item;
}

Expand Down
11 changes: 11 additions & 0 deletions backend/views/layouts/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
<?php $this->beginBody() ?>
<style>
.content-wrapper, .right-side, .main-footer {margin-left:0!important;}
.btn-refresh {
position: fixed;
bottom: 100px;
right: 2px;
padding: 3px 8px;
font-size: 24px;
border:1px solid #ccc;
border-radius:4px;
cursor: pointer;
}
</style>
<div class="content-wrapper">
<section class="content-header">
Expand Down Expand Up @@ -52,6 +62,7 @@
<?= $content ?>
</section>
</div>
<?= Html::a(Html::icon('refresh'), 'javascript:;', ['class' => 'btn btn-success btn-refresh', 'onclick' => 'location.reload()']) ?>
<?php $this->endBody() ?>
<?php if (isset($this->blocks['js'])): ?>
<?= $this->blocks['js'] ?>
Expand Down
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,11 +163,11 @@ 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) {
if ($this->itemCallback instanceof \Closure) {
$result = call_user_func($this->itemCallback, $result);
}
} catch (Exception $e) {
$result = [
'error' => $e->getMessage()
];
Expand Down
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));
}

}
Loading

0 comments on commit b021cab

Please sign in to comment.