-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bruce Xie
committed
Apr 30, 2019
1 parent
4315145
commit 50f0197
Showing
18 changed files
with
692 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ | |
/config/.settings | ||
/config/.config.json | ||
/uploads | ||
/uploader/UploadCoroutine.php | ||
/uploader/UploadCoroutine.php | ||
/db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Bruce Xie | ||
* Date: 2019-04-30 | ||
* Time: 02:33 | ||
*/ | ||
|
||
namespace settings; | ||
|
||
class Controller { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Bruce Xie | ||
* Date: 2019-04-29 | ||
* Time: 14:58 | ||
*/ | ||
|
||
namespace settings; | ||
|
||
use PDO; | ||
use PDOException; | ||
use uploader\Common; | ||
|
||
class DbModel { | ||
|
||
public $connection; | ||
|
||
public function __construct () | ||
{ | ||
$database = (new SettingController())->getDatabaseConfig(); | ||
if(!isset($database['dsn']) || !$database['dsn']){ | ||
return $this->connection; | ||
} | ||
|
||
try{ | ||
if(strpos($database['dsn'], 'sqlite') === 0){ | ||
$file = str_replace('sqlite:', '', $database['dsn']); | ||
strpos($file, '/')===false && $file = APP_PATH . '/db/'.$file; | ||
if(!is_file($file)){ | ||
$dbDir = dirname($file); | ||
!is_dir($dbDir) && @mkdir($dbDir, 0777, true); | ||
if(is_dir($dbDir)){ | ||
if(copy(APP_PATH . '/settings/PicUploader-tpl.db', $file) && chmod($file, 0777)){ | ||
$this->connection = new PDO('sqlite:'.$file); | ||
} | ||
} | ||
}else{ | ||
$this->connection = new PDO('sqlite:'.$file); | ||
} | ||
}else{ | ||
$this->connection = new PDO($database['dsn'], $database['username'], $database['password']); | ||
$res = $this->connection->query("SHOW TABLES LIKE 'history'"); | ||
$row = $res->fetch(); | ||
if(!$row || !isset($row[0]) || $row[0]!='history'){ | ||
$historyTableSqlFile = APP_PATH . '/settings/PicUploader.sql'; | ||
if(is_file($historyTableSqlFile)){ | ||
$historyTable = file_get_contents($historyTableSqlFile); | ||
$this->connection->exec($historyTable); | ||
} | ||
} | ||
} | ||
}catch (PDOException $e){ | ||
(new Common())->writeLog($e->getMessage(), 'error_log'); | ||
} | ||
|
||
return $this->connection; | ||
} | ||
|
||
public function __destruct () | ||
{ | ||
// TODO: Implement __destruct() method. | ||
$this->connection = null; | ||
} | ||
|
||
/** | ||
* initialize | ||
* @return DbModel | ||
*/ | ||
/*public static function initialize(){ | ||
return new self(); | ||
}*/ | ||
|
||
public function query($sql){ | ||
$res = $this->connection->query($sql); | ||
return $res->fetch(PDO::FETCH_ASSOC); | ||
} | ||
|
||
public function queryAll($sql){ | ||
$res = $this->connection->query($sql); | ||
return $res->fetchAll(PDO::FETCH_ASSOC); | ||
} | ||
|
||
public function execute($sql){ | ||
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
$res = $this->connection->exec($sql); | ||
return $res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Bruce Xie | ||
* Date: 2019-04-30 | ||
* Time: 02:32 | ||
*/ | ||
|
||
namespace settings; | ||
|
||
use uploader\Common; | ||
use zelda\Pagination; | ||
|
||
class HistoryController extends Controller { | ||
|
||
/** | ||
* 添加一条历史记录 | ||
* @param $filename | ||
* @param $url | ||
* @param $size | ||
* | ||
* @return int | ||
*/ | ||
public function Add($filename, $url, $size){ | ||
$pattern = '/http[s]?.*?/'; | ||
if(!preg_match($pattern, $url)){ | ||
return false; | ||
} | ||
$model = new HistoryModel(); | ||
return $model->createOne([ | ||
'filename' => $filename, | ||
'url' => $url, | ||
'size' => $size, | ||
]); | ||
} | ||
|
||
/** | ||
* 获取分页 | ||
* @param $totalCount | ||
* @param $curPage | ||
* @param $pageSize | ||
* | ||
* @return string | ||
*/ | ||
public function getPagination($totalCount, $curPage, $pageSize){ | ||
// var_dump($curPage);exit; | ||
$pageCount = ceil($totalCount / $pageSize); | ||
$pagination = '<span class="">共'.$pageCount.'页</span>'; | ||
$pagination .= '<span class="button' . ($curPage==1 ? ' cur' : '') . '" data-page="1">首页</span>'; | ||
$pagination .= '<span class="button' . ($curPage==1 ? ' forbidden' : '') . '" data-page="'. ($curPage==1 ? 1 : $curPage-1) .'">上一页</span>'; | ||
$pagination .= '<span class="button' . ($curPage==$pageCount ? ' forbidden' : '') . '" data-page="' . ($curPage==$pageCount ? $pageCount : $curPage+1) . '">下一页</span>'; | ||
$pagination .= '<span class="button' . ($curPage==$pageCount ? ' cur' : '') . '" data-page="' .$pageCount. '">末页</span>'; | ||
$pagination .= '当前页<input type="number" class="jump-to-page" value="'. $curPage .'">'; | ||
$pagination .= '<span class="button jump-to-page-button" data-pageCount="' .$pageCount. '">跳转</span>'; | ||
return $pagination; | ||
} | ||
|
||
/** | ||
* 获取历史记录列表 | ||
* | ||
* @return array | ||
*/ | ||
public function getList(){ | ||
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; | ||
$model = new HistoryModel(); | ||
$pageSize = 10; | ||
$total = $model->getTotal(); | ||
$pageCount = (int)ceil($total / $pageSize); | ||
$page > $pageCount && $page = $pageCount; | ||
$page < 1 && $page = 1; | ||
$offset = ($page-1) * $pageSize; | ||
$limit = $offset . ',' . $pageSize; | ||
$order = 'id DESC'; | ||
$rows = $model->findAll('', $order, $limit); | ||
$common = new Common(); | ||
foreach($rows as &$row){ | ||
$row['size'] = $common->getFileSizeHuman($row['size']); | ||
} | ||
|
||
$pagination = $this->getPagination($total, $page, $pageSize); | ||
|
||
// var_dump($res); | ||
return json_encode([ | ||
'code' => 0, | ||
'data' => $rows, | ||
'pagination' => $pagination, | ||
], JSON_UNESCAPED_SLASHES); | ||
} | ||
|
||
/** | ||
* 删除一条记录 | ||
* | ||
* @return false|string | ||
*/ | ||
public function removeItem(){ | ||
$id = isset($_GET['id']) ? $_GET['id'] : 0; | ||
$ret = [ | ||
'code' => -1, | ||
'msg' => '删除失败', | ||
]; | ||
if(!$id){ | ||
return $ret; | ||
} | ||
$model = new HistoryModel(); | ||
|
||
if($model->delete($id) !== false){ | ||
$ret = [ | ||
'code' => 0, | ||
'msg' => '删除成功', | ||
]; | ||
} | ||
return json_encode($ret, JSON_UNESCAPED_UNICODE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Bruce Xie | ||
* Date: 2019-04-30 | ||
* Time: 17:05 | ||
*/ | ||
|
||
namespace settings; | ||
|
||
class HistoryModel extends DbModel { | ||
|
||
public static $tableName = 'history'; | ||
|
||
/** | ||
* return table name | ||
* @return string | ||
*/ | ||
public static function tableName(){ | ||
return self::$tableName; | ||
} | ||
|
||
/** | ||
* Insert one row | ||
* @param $data | ||
* | ||
* @return int | ||
*/ | ||
public function createOne($data){ | ||
//开启之后,如果报错则会显示出来,否则有错也不显示 | ||
$uploadTime = date('Y-m-d H:i:s'); | ||
$sql = 'INSERT INTO `history`(`filename`, `url`, `size`, `created_at`) VALUES("'.$data['filename'].'", "'.$data['url'].'", '.$data['size'].', "'.$uploadTime.'")'; | ||
$affectedRow = $this->execute($sql); | ||
return $affectedRow; | ||
} | ||
|
||
/** | ||
* Fetch one row by id | ||
* @param $id | ||
* | ||
* @return DbModel | ||
*/ | ||
public function findOne($id){ | ||
$sql = 'SELECT * FROM `'.self::$tableName.'` WHERE id='. $id; | ||
$res = $this->query($sql); | ||
return $res; | ||
} | ||
|
||
/** | ||
* Fetch all rows by conditions | ||
* @param $where | ||
* @param $order | ||
* @param $limit | ||
* | ||
* @return array | ||
*/ | ||
public function findAll($where, $order, $limit){ | ||
$sql = 'SELECT * FROM `'.self::$tableName.'`'; | ||
$where && $sql .= ' WHERE '.$where; | ||
$order && $sql .= ' ORDER BY '.$order; | ||
$limit && $sql .= ' LIMIT '.$limit; | ||
$res = $this->queryAll($sql); | ||
return $res; | ||
} | ||
|
||
/** | ||
* Get total rows in the table | ||
* @return int | ||
*/ | ||
public function getTotal(){ | ||
$sql = 'SELECT COUNT(*) as rowCount FROM '.self::$tableName; | ||
$res = $this->query($sql); | ||
return isset($res['rowCount']) ? (int)$res['rowCount'] : 0; | ||
} | ||
|
||
/** | ||
* Delete a row | ||
* @param $id | ||
* | ||
* @return int | ||
*/ | ||
public function delete($id){ | ||
$sql = 'DELETE FROM '.self::$tableName.' WHERE id='.$id; | ||
return $this->execute($sql); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SET NAMES utf8mb4; | ||
CREATE TABLE `history` ( | ||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id', | ||
`filename` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '文件名', | ||
`url` varchar(200) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'url', | ||
`size` int(11) NOT NULL COMMENT '文件大小', | ||
`created_at` datetime NOT NULL COMMENT '创建时间', | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='PicUploader上传历史记录'; |
Oops, something went wrong.