-
Notifications
You must be signed in to change notification settings - Fork 2
/
ls-diff.php
146 lines (132 loc) · 4.3 KB
/
ls-diff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Created by PhpStorm.
* User: wangcheng
* Date: 14-9-9
* Time: 23:44
*/
define("TYPE_LIST", "list");
define("TYPE_DATA", "data");
define("DS", DIRECTORY_SEPARATOR);
$req_type = $_GET["type"]; //请求类型
$req_post = $_POST;
$res_obj = array(
'data' => array(),
'errno' => 0,
'msg' => ""
);
/**
* todo : 支持gzip
* todo : 设置header
*/
if($req_type == TYPE_LIST){
$pkgStr = $req_post["pids"];
$pids = explode(",", $pkgStr);
$result_list = array();
foreach($pids as $pid){
$result_list[$pid] = StaticReader::getList($pid);
}
$res_obj["data"] = $result_list;
echo json_encode($res_obj);
}else if($req_type == TYPE_DATA){
$result_data = array();
foreach($req_post as $pid=>$hashStr){
$package_data = array();
if(strlen($hashStr) == 0){
$hashs = null;
}else{
$hashs = explode(",", $hashStr);
}
$tmp_list = StaticReader::getList($pid);
$package_data["data"] = StaticReader::getData($pid, $hashs);
$package_data["type"] = $tmp_list["type"];
$package_data["hash"] = $tmp_list["hash"];
$result_data[$pid] = $package_data;
}
$res_obj["data"] = $result_data;
echo json_encode($res_obj);
}else{
$res_obj["errno"] = 1;
$res_obj["msg"] = "Wrong request type!";
echo json_encode($res_obj);
}
class StaticReader {
private static $LIST_EXT = ".lslist.json";
private static $DATA_EXT = ".lsdata.json";
private static $CONNECTOR = "_";
private static $module_list = array();
private static $module_data = array();
private static function getDataDir(){
return dirname(__FILE__);
}
public static function getList($pid){
$tokens = explode(self::$CONNECTOR, $pid);
$module = $tokens[0];
if(isset(self::$module_list[$module])){
if(self::$module_list[$module][$pid]){
return self::$module_list[$module][$pid];
}else{
return array();
}
}else{
$listFile = self::getDataDir() . DS . $module . self::$LIST_EXT;
//$listFile = self::getDataDir() . DS . "test" . DS . "data" . DS . $module . self::$LIST_EXT;
if(file_exists($listFile)){
$listObj = json_decode(file_get_contents($listFile), true);
self::$module_list[$module] = $listObj;
if($listObj[$pid]){
return $listObj[$pid];
}else{
return array();
}
}else{
trigger_error($listFile . "does not exist", E_USER_NOTICE);
return array();
}
}
}
public static function getData($pid, $hashs=null){
$tokens = explode(self::$CONNECTOR, $pid);
$module = $tokens[0];
if(isset(self::$module_data[$module])){
if(isset(self::$module_data[$module][$pid])){
$pkg_data = self::$module_data[$module][$pid];
}else{
$pkg_data = array();
}
}else{
//$dataFile = self::getDataDir() . DS . "test" . DS . "data" . DS . $module . self::$DATA_EXT;
$dataFile = self::getDataDir() . DS . $module . self::$DATA_EXT;
if(file_exists($dataFile)){
self::$module_data[$module] = json_decode(file_get_contents($dataFile), true);
if(isset(self::$module_data[$module][$pid])){
$pkg_data = self::$module_data[$module][$pid];
}else{
$pkg_data = array();
}
}else{
trigger_error($dataFile . "does not exist", E_USER_ERROR);
$pkg_data = array();
}
}
$newData = array();
if(isset($hashs)){
foreach($hashs as $hash){
if(isset($pkg_data[$hash])){
$newData[] = array(
"hash" => $hash,
"content" => $pkg_data[$hash]
);
}
}
}else{
foreach($pkg_data as $hash => $content){
$newData[] = array(
"hash" => $hash,
"content" => $content
);
}
}
return $newData;
}
}