Skip to content
This repository was archived by the owner on Aug 6, 2018. It is now read-only.

Commit 2b3b870

Browse files
committed
filesystem abstraction
1 parent 1dfa773 commit 2b3b870

File tree

7 files changed

+268
-25
lines changed

7 files changed

+268
-25
lines changed

Module.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,39 @@ public static function registerGlobalRelations()
3636
*/
3737
public function registerAutoloaders()
3838
{
39+
3940
}
4041

4142
/**
4243
* Registers the module-only services
4344
*
44-
* @param $di
45+
* @param \Phalcon\DI $di
4546
*/
4647
public function registerServices($di)
4748
{
4849
$dispatcher = $di->getDispatcher();
4950
$dispatcher->setDefaultNamespace('Eva\EvaFileSystem\Controllers');
51+
52+
$self = $this;
53+
$di->set('customFilesystem', function ($configKey) use ($self, $di) {
54+
return $self->diCustomFilesystemFilesystem($di, $configKey);
55+
});
56+
}
57+
58+
/**
59+
* 自定义文件系统 DI
60+
*
61+
* @param \Phalcon\DI $di
62+
* @param string $configKey
63+
*/
64+
protected function diCustomFilesystemFilesystem($di, $configKey)
65+
{
66+
/** @var \Phalcon\Config $config */
67+
$config = $di->getConfig();
68+
$adapterClass = $config->filesystem->$configKey->adapter;
69+
$adapterOptions = $config->filesystem->$configKey->options;
70+
$adapter = new $adapterClass($adapterOptions);
71+
72+
return $adapter;
5073
}
5174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Eva\EvaFileSystem\Adapter;
3+
4+
use Gaufrette\Adapter;
5+
use Phalcon\Mvc\User\Component;
6+
7+
/**
8+
* 文件系统适配器抽象类
9+
* @author mr.5<mr5.simple@gmail.com>
10+
* @package Eva\EvaFileSystem\Adapter
11+
*/
12+
abstract class AdapterAbstract extends Component implements Adapter
13+
{
14+
// abstract public function thumb();
15+
16+
abstract public function __construct($options);
17+
18+
/**
19+
* 生成图片的绝对链接
20+
*
21+
* @param string $filename
22+
* @param string $configKey
23+
* @return string
24+
*/
25+
abstract public function url($filename, $configKey);
26+
27+
/**
28+
* 通过缩略图样式名称生成缩略图链接
29+
*
30+
* @param string $filename
31+
* @param string $styleClass
32+
* @param string $configKey
33+
* @return string
34+
*/
35+
abstract public function thumbWitchClass($filename, $styleClass, $configKey);
36+
}

src/EvaFileSystem/Adapter/Qiniu.php

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
namespace Eva\EvaFileSystem\Adapter;
4+
5+
use Gaufrette\Adapter;
6+
use Gaufrette\Adapter\MetadataSupporter;
7+
8+
class Qiniu extends AdapterAbstract implements
9+
MetadataSupporter
10+
{
11+
protected $bucket;
12+
protected $client;
13+
/**
14+
* @var \Phalcon\Config
15+
*/
16+
protected $config;
17+
18+
public function __construct($options)
19+
{
20+
// $accessKey, $secretKey, $bucket
21+
\Qiniu_setKeys($options['accessKey'], $options['secretKey']);
22+
$this->bucket = $options['bucket'];
23+
$this->client = new \Qiniu_MacHttpClient(null);
24+
$this->config = $this->getDI()->getConfig();
25+
}
26+
27+
/**
28+
* Reads the content of the file
29+
*
30+
* @param string $key
31+
*
32+
* @return string|boolean if cannot read content
33+
*/
34+
public function read($key)
35+
{
36+
// TODO: Implement read() method.
37+
}
38+
39+
/**
40+
* Writes the given content into the file
41+
*
42+
* @param string $key
43+
* @param string $content
44+
*
45+
* @return integer|boolean The number of bytes that were written into the file
46+
*/
47+
public function write($key, $content)
48+
{
49+
$putPolicy = new \Qiniu_RS_PutPolicy($this->bucket);
50+
$upToken = $putPolicy->Token(null);
51+
52+
list($ret, $err) = \Qiniu_Put($upToken, $key, $content, null);
53+
if ($err !== null) {
54+
return false;
55+
} else {
56+
return true;
57+
}
58+
}
59+
60+
/**
61+
* Indicates whether the file exists
62+
*
63+
* @param string $key
64+
*
65+
* @return boolean
66+
*/
67+
public function exists($key)
68+
{
69+
// TODO: Implement exists() method.
70+
}
71+
72+
/**
73+
* Returns an array of all keys (files and directories)
74+
*
75+
* @return array
76+
*/
77+
public function keys()
78+
{
79+
// TODO: Implement keys() method.
80+
}
81+
82+
/**
83+
* Returns the last modified time
84+
*
85+
* @param string $key
86+
*
87+
* @return integer|boolean An UNIX like timestamp or false
88+
*/
89+
public function mtime($key)
90+
{
91+
// TODO: Implement mtime() method.
92+
}
93+
94+
/**
95+
* Deletes the file
96+
*
97+
* @param string $key
98+
*
99+
* @return boolean
100+
*/
101+
public function delete($key)
102+
{
103+
return \Qiniu_RS_Delete($this->client, $this->bucket, $key) === null;
104+
105+
}
106+
107+
/**
108+
* Renames a file
109+
*
110+
* @param string $sourceKey
111+
* @param string $targetKey
112+
*
113+
* @return boolean
114+
*/
115+
public function rename($sourceKey, $targetKey)
116+
{
117+
return \Qiniu_RS_Move($this->client, $this->bucket, $sourceKey, $this->bucket, $targetKey) === null;
118+
}
119+
120+
/**
121+
* Check if key is directory
122+
*
123+
* @param string $key
124+
*
125+
* @return boolean
126+
*/
127+
public function isDirectory($key)
128+
{
129+
// TODO: Implement isDirectory() method.
130+
}
131+
132+
/**
133+
* @param string $key
134+
* @param array $content
135+
*/
136+
public function setMetadata($key, $content)
137+
{
138+
// TODO: Implement setMetadata() method.
139+
}
140+
141+
/**
142+
* @param string $key
143+
* @return array
144+
*/
145+
public function getMetadata($key)
146+
{
147+
list($ret, $err) = \Qiniu_RS_Stat($this->client, $this->bucket, $key);
148+
if ($err !== null) {
149+
return false;
150+
} else {
151+
return $ret;
152+
}
153+
}
154+
155+
public function thumbWitchClass($filename, $styleClass, $configKey)
156+
{
157+
// $this->config
158+
}
159+
160+
public function url($filename, $configKey)
161+
{
162+
163+
}
164+
}

src/EvaFileSystem/Controllers/Admin/UploadController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function indexAction()
3434
$files = $this->request->getUploadedFiles();
3535
//Only allow upload the first file by force
3636
$file = $files[0];
37-
$file = $upload->upload($file);
37+
$file = $upload->upload($file, 'userAvatar');
3838
if ($file) {
3939
$fileinfo = $file->toArray();
4040
$fileinfo['localUrl'] = $file->getLocalUrl();

src/EvaFileSystem/Entities/Files.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Files extends \Eva\EvaEngine\Mvc\Model
3232
*/
3333
public $storageAdapter;
3434

35+
3536
/**
3637
*
3738
* @var string
@@ -121,8 +122,10 @@ class Files extends \Eva\EvaEngine\Mvc\Model
121122
* @var integer
122123
*/
123124
public $createdAt;
124-
125-
private $configKey = 'default';
125+
/**
126+
* @var string
127+
*/
128+
public $configKey = 'default';
126129

127130
private $configReady = false;
128131

src/EvaFileSystem/Models/Upload.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function validation()
4545
}
4646
}
4747

48-
public function upload(File $file)
48+
public function upload(File $file, $configKey = 'default')
4949
{
5050
if ($file->getError()) {
5151
throw new Exception\IOException('ERR_FILE_UPLOAD_FAILED');
@@ -84,6 +84,7 @@ public function upload(File $file)
8484
'fileExtension' => $fileExtension,
8585
'fileHash' => $fileHash,
8686
'isImage' => $isImage,
87+
'configKey'=>$configKey,
8788
'fileName' => $fileName . '.' . $fileExtension,
8889
'createdAt' => time(),
8990
);
@@ -93,8 +94,12 @@ public function upload(File $file)
9394
$fileinfo['imageWidth'] = $image[0];
9495
$fileinfo['imageHeight'] = $image[1];
9596
}
96-
97-
$filesystem = $this->getDI()->getFileSystem();
97+
/** @var \Gaufrette\Adapter $filesystem */
98+
if($configKey == 'default') {
99+
$filesystem = $this->getDI()->getFileSystem();
100+
} else {
101+
$filesystem = $this->getDI()->get('customFilesystem', array($configKey));
102+
}
98103

99104
$path = md5(microtime());
100105
$path = str_split($path, 2);
@@ -108,22 +113,22 @@ public function upload(File $file)
108113

109114
$this->assign($fileinfo);
110115
if ($this->save()) {
111-
if (!$filesystem->has($path)) {
116+
// if (!$filesystem->has($path)) {
112117
if ($filesystem->write($path, file_get_contents($tmp))) {
113118
unlink($tmp);
114119
} else {
115120
throw new Exception\IOException('ERR_FILE_MOVE_TO_STORAGE_FAILED');
116121
}
117-
} else {
118-
throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME');
119-
}
122+
// } else {
123+
// throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME');
124+
// }
120125
} else {
121126
throw new Exception\RuntimeException('ERR_FILE_SAVE_TO_DB_FAILED');
122127
}
123128
return $this;
124129
}
125130

126-
public function uploadByEncodedData($data, $originalName, $mimeType = null)
131+
public function uploadByEncodedData($data, $originalName, $mimeType = null, $configKey = 'default')
127132
{
128133
if (!$headPos = strpos($data, ',')) {
129134
throw new Exception\InvalidArgumentException('ERR_FILE_ENCODED_UPLOAD_FORMAT_INCORRECT');
@@ -134,7 +139,7 @@ public function uploadByEncodedData($data, $originalName, $mimeType = null)
134139

135140
$tmpName = Text::random(\Phalcon\Text::RANDOM_ALNUM, 6);
136141
$tmpPath = $this->getUploadTmpPath();
137-
$tmp = $tmpPath . '/' . $tmpName;
142+
$tmp = $tmpPath . '/' . $tmpName;
138143
$adapter = new \Gaufrette\Adapter\Local($tmpPath);
139144
$filesystem = new \Gaufrette\Filesystem($adapter);
140145
$filesystem->write($tmpName, $data);
@@ -169,6 +174,7 @@ public function uploadByEncodedData($data, $originalName, $mimeType = null)
169174
'fileExtension' => $fileExtension,
170175
'fileHash' => $fileHash,
171176
'isImage' => $isImage,
177+
'configKey'=>$configKey,
172178
'fileName' => $fileName . '.' . $fileExtension,
173179
'createdAt' => time(),
174180
);
@@ -179,7 +185,12 @@ public function uploadByEncodedData($data, $originalName, $mimeType = null)
179185
$fileinfo['imageHeight'] = $image[1];
180186
}
181187

182-
$filesystem = $this->getDI()->getFileSystem();
188+
/** @var \Gaufrette\Adapter $filesystem */
189+
if($configKey == 'default') {
190+
$filesystem = $this->getDI()->getFileSystem();
191+
} else {
192+
$filesystem = $this->getDI()->get($configKey.'Filesystem');
193+
}
183194

184195
$path = md5(time());
185196
$path = str_split($path, 2);
@@ -193,15 +204,15 @@ public function uploadByEncodedData($data, $originalName, $mimeType = null)
193204

194205
$this->assign($fileinfo);
195206
if ($this->save()) {
196-
if (!$filesystem->has($path)) {
207+
// if (!$filesystem->has($path)) {
197208
if ($filesystem->write($path, file_get_contents($tmp))) {
198209
unlink($tmp);
199210
} else {
200211
throw new Exception\IOException('ERR_FILE_MOVE_TO_STORAGE_FAILED');
201212
}
202-
} else {
203-
throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME');
204-
}
213+
// } else {
214+
// throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME');
215+
// }
205216
} else {
206217
throw new Exception\RuntimeException('ERR_FILE_SAVE_TO_DB_FAILED');
207218
}

0 commit comments

Comments
 (0)