-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
267 lines (233 loc) · 7.76 KB
/
api.php
File metadata and controls
267 lines (233 loc) · 7.76 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST");
header("Access-Control-Allow-Headers: Content-Type");
$servername = "127.0.0.1";
$username = "photo_moejue_cn";
$password = "3zWdFaL7WZYzp26n";
$dbname = "photo_moejue_cn";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die(json_encode(["error" => "连接数据库失败: " . $conn->connect_error]));
}
// 缓存相关设置
$cacheTime = 600; // 缓存时间5分钟(300秒)
$cacheDir = __DIR__ . '/cache/';
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0755, true);
}
// 获取分页参数
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 10;
$offset = ($page - 1) * $limit;
// 获取请求 URL 作为缓存文件名
$requestUrl = $_SERVER['REQUEST_URI'];
$cacheFile = $cacheDir . md5($requestUrl) . '.json';
// 检查缓存文件是否存在且未过期
if (file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTime) {
echo file_get_contents($cacheFile);
exit; // 直接返回缓存内容
}
if (isset($_GET['action'])) {
$action = $_GET['action'];
ob_start(); // 启用输出缓冲区
switch ($action) {
case "photos":
getPhotos($conn, $offset, $limit);
break;
case "albums":
getAlbums($conn);
break;
case "random":
getRandomMedia($conn, $offset, $limit);
break;
default:
echo json_encode(["error" => "无效的接口请求"]);
}
$output = ob_get_clean(); // 获取输出缓冲区内容
// 将输出内容写入缓存文件
file_put_contents($cacheFile, $output);
echo $output; // 输出内容到客户端
} else {
echo json_encode(["error" => "未指定接口操作"]);
}
$conn->close();
// 日期分组格式化函数
function formatDataByDate($data) {
$groupedData = [];
$today = strtotime('today');
$yesterday = strtotime('yesterday');
$dayBeforeYesterday = strtotime('-2 days');
$currentYear = date("Y");
foreach ($data as $item) {
$itemDate = strtotime($item['created_at']);
$itemYear = date("Y", $itemDate);
// 根据日期确定显示的日期标签
if ($itemDate >= $today) {
$dateKey = "今天";
} elseif ($itemDate >= $yesterday) {
$dateKey = "昨天";
} elseif ($itemDate >= $dayBeforeYesterday) {
$dateKey = "前天";
} else {
// 判断年份是否为当前年份
$dateFormat = ($itemYear === $currentYear) ? "m月d日" : "Y年m月d日";
$dateKey = date($dateFormat, $itemDate);
}
// 初始化分组数据
if (!isset($groupedData[$dateKey])) {
$groupedData[$dateKey] = ['date' => $dateKey, 'items' => []];
}
// 添加项目数据
$groupedData[$dateKey]['items'][] = [
'src' => $item['data'],
'alt' => $item['name'],
'isVideo' => $item['type'] == 1,
'duration' => $item['duration'] ?? null
];
}
return array_values($groupedData);
}
// 照片接口
function getPhotos($conn, $offset, $limit) {
// 获取可选参数 folder_id、password 和 search
$folder_id = isset($_GET['folder_id']) ? (int)$_GET['folder_id'] : null;
$password = isset($_GET['password']) ? $_GET['password'] : null;
$search = isset($_GET['search']) ? $_GET['search'] : null;
// 验证文件夹密码(如果指定了 folder_id 且该文件夹需要密码)
if ($folder_id) {
$query = "SELECT password FROM folders WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $folder_id);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
// 如果文件夹设置了密码且密码不匹配,返回错误
if ($row['password'] && $row['password'] !== $password) {
echo json_encode(["error" => "文件夹密码错误"]);
return;
}
} else {
echo json_encode(["error" => "文件夹不存在"]);
return;
}
}
// 构建 SQL 查询
$query = "SELECT * FROM files";
$conditions = [];
$params = [];
$types = "";
// 处理 folder_id 条件
if ($folder_id) {
$conditions[] = "folder_id = ?";
$params[] = $folder_id;
$types .= "i";
} else {
if ($search) {
$conditions[] = "folder_id IN (SELECT id FROM folders WHERE attribute = 0)";
}else {
$conditions[] = "type = 0 AND folder_id IN (SELECT id FROM folders WHERE attribute = 0)";
}
}
// 处理 search 条件
if ($search) {
$conditions[] = "(name LIKE ? OR data LIKE ?)";
$params[] = "%$search%";
$params[] = "%$search%";
$types .= "ss";
}
// 将条件组合到查询中
if ($conditions) {
$query .= " WHERE " . implode(" AND ", $conditions);
}
$query .= " ORDER BY created_at DESC LIMIT ?, ?";
// 设置分页参数
$params[] = $offset;
$params[] = $limit;
$types .= "ii";
// 准备并执行查询
$stmt = $conn->prepare($query);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
// 获取查询结果并格式化数据
$photos = [];
while ($row = $result->fetch_assoc()) {
$photos[] = $row;
}
echo json_encode(formatDataByDate($photos));
}
// 相册接口
function getAlbums($conn) {
$query = "
SELECT
f.id AS folder_id,
f.name AS folder_name,
f.created_at, f.attribute,
f.password,
COUNT(files.id) AS photo_count,
(SELECT files.data
FROM files
WHERE files.folder_id = f.id
ORDER BY files.created_at DESC
LIMIT 1) AS latest_image
FROM
folders f
LEFT JOIN
files ON files.folder_id = f.id
GROUP BY
f.id
ORDER BY
f.created_at DESC";
$result = $conn->query($query);
$albums = [];
while ($row = $result->fetch_assoc()) {
$albums[] = [
"folder_id" => $row["folder_id"],
"folder_name" => $row["folder_name"],
"created_at" => $row["created_at"],
"photo_count" => $row["photo_count"],
"latest_image" => $row["latest_image"],
'attribute' => $row['attribute'],
"ispassword" => !!$row['password']
];
}
echo json_encode($albums);
}
// 随机接口(该方法效率较低,尽可能不要频繁执行,并确保有缓存存在)
function getRandomMedia($conn, $page, $limit) {
// 每种类型分配的记录数,尽量平分
$typeLimit = ceil($limit / 2);
// 分别查询图片和视频
$query = "
(
SELECT f.*
FROM files f
JOIN folders fo ON f.folder_id = fo.id
WHERE fo.attribute = 0 AND f.type = 0
ORDER BY RAND()
LIMIT ?
)
UNION ALL
(
SELECT f.*
FROM files f
JOIN folders fo ON f.folder_id = fo.id
WHERE fo.attribute = 0 AND f.type = 1
ORDER BY RAND()
LIMIT ?
)
ORDER BY RAND()
LIMIT ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("iii", $typeLimit, $typeLimit, $limit);
$stmt->execute();
$result = $stmt->get_result();
$media = [];
while ($row = $result->fetch_assoc()) {
$media[] = $row;
}
echo json_encode($media);
}
?>