Skip to content

Commit

Permalink
Add upload history list page
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Xie committed Apr 30, 2019
1 parent 4315145 commit 50f0197
Show file tree
Hide file tree
Showing 18 changed files with 692 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
/config/.settings
/config/.config.json
/uploads
/uploader/UploadCoroutine.php
/uploader/UploadCoroutine.php
/db
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,16 @@
//0: 使用随机字符串文件名(这样不会覆盖原图)
//1: 使用原文件名,有可能会覆盖原图(同一天上传同一文件名的文件两次以上,后面的就会覆盖前面的)
'keepOriginalFilename' => 0,

//数据库是用于存储上传历史记录的,不存在配置
'database' => [
// 如果使用Sqlite,不需要指定db的具体路径,不需要修改,直接这样就能用
'dsn' => 'sqlite:PicUploader.db',
//dbname=PicUploader指定的是数据库名,这个数据库必须要自己手动在mysql中创建,而表则会自动创建
'dsn' => 'mysql:host=127.0.0.1:3306;dbname=PicUploader',
'username' => 'root',
'password' => '123',
],
];

return $config;
3 changes: 3 additions & 0 deletions dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
</ul>
</div>
</div>
<div class="icons upload-history" title="上传历史">
<i class="fa fa-history"></i>
</div>
</div>
<div class="main">
<div class="show-save-tip">
Expand Down
5 changes: 5 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
require_once APP_PATH . '/' . str_replace('\\', '/', $class_name) . '.php';
});

/*if((new \settings\DbModel())->connection)
// $arr = (new \settings\HistoryController())->getList(1);
// var_dump($arr);
(new \settings\HistoryController())->Add('试试中文.png', 'https://3243.jpg', 43242);
exit;*/
//获取配置
$config = call_user_func([(new SettingController()), 'getMergeSettings']);

Expand Down
13 changes: 13 additions & 0 deletions settings/Controller.php
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 {

}
89 changes: 89 additions & 0 deletions settings/DbModel.php
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;
}
}
114 changes: 114 additions & 0 deletions settings/HistoryController.php
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);
}
}
86 changes: 86 additions & 0 deletions settings/HistoryModel.php
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 added settings/PicUploader-tpl.db
Binary file not shown.
9 changes: 9 additions & 0 deletions settings/PicUploader.sql
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上传历史记录';
Loading

0 comments on commit 50f0197

Please sign in to comment.