explorer.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>LuatOS 文件管理系统</title>
  7. <style>
  8. body {
  9. font-family: 'Microsoft YaHei', Arial, sans-serif;
  10. background-color: #f5f5f5;
  11. margin: 0;
  12. padding: 20px;
  13. color: #333;
  14. }
  15. .container {
  16. max-width: 1200px;
  17. margin: auto;
  18. background: white;
  19. padding: 20px;
  20. border-radius: 8px;
  21. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  22. }
  23. .header {
  24. background: #2c3e50;
  25. color: white;
  26. padding: 15px;
  27. border-radius: 5px;
  28. margin-bottom: 20px;
  29. display: flex;
  30. justify-content: space-between;
  31. align-items: center;
  32. }
  33. .login-form {
  34. max-width: 400px;
  35. margin: 100px auto;
  36. padding: 40px;
  37. background: white;
  38. border-radius: 8px;
  39. box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  40. }
  41. .form-group {
  42. margin-bottom: 15px;
  43. }
  44. label {
  45. display: block;
  46. margin-bottom: 5px;
  47. font-weight: bold;
  48. }
  49. input[type="text"], input[type="password"] {
  50. width: 100%;
  51. padding: 10px;
  52. border: 1px solid #ddd;
  53. border-radius: 4px;
  54. box-sizing: border-box;
  55. }
  56. button {
  57. background-color: #3498db;
  58. color: white;
  59. border: none;
  60. padding: 10px 20px;
  61. border-radius: 4px;
  62. cursor: pointer;
  63. font-size: 16px;
  64. }
  65. button:hover {
  66. background-color: #2980b9;
  67. }
  68. .file-list {
  69. width: 100%;
  70. border-collapse: collapse;
  71. margin-top: 20px;
  72. }
  73. .file-list th, .file-list td {
  74. padding: 12px;
  75. text-align: left;
  76. border-bottom: 1px solid #ddd;
  77. }
  78. .file-list th {
  79. background-color: #f8f9fa;
  80. font-weight: bold;
  81. }
  82. .file-list tr:hover {
  83. background-color: #f5f5f5;
  84. }
  85. .download-btn {
  86. background-color: #27ae60;
  87. color: white;
  88. padding: 5px 10px;
  89. text-decoration: none;
  90. border-radius: 3px;
  91. font-size: 12px;
  92. }
  93. .download-btn:hover {
  94. background-color: #219a52;
  95. }
  96. .delete-btn {
  97. background-color: #e74c3c;
  98. color: white;
  99. border: none;
  100. padding: 5px 10px;
  101. border-radius: 3px;
  102. font-size: 12px;
  103. cursor: pointer;
  104. }
  105. .delete-btn:hover {
  106. background-color: #c0392b;
  107. }
  108. .breadcrumb {
  109. padding: 10px 0;
  110. margin-bottom: 20px;
  111. }
  112. .breadcrumb a {
  113. color: #3498db;
  114. text-decoration: none;
  115. cursor: pointer;
  116. }
  117. .breadcrumb a:hover {
  118. text-decoration: underline;
  119. }
  120. .hidden {
  121. display: none;
  122. }
  123. .error {
  124. color: #e74c3c;
  125. margin-top: 10px;
  126. }
  127. </style>
  128. </head>
  129. <body>
  130. <div class="container">
  131. <!-- 登录页面 -->
  132. <div id="loginPage" class="login-form">
  133. <h2>LuatOS 文件管理系统登录</h2>
  134. <div class="form-group">
  135. <label for="username">用户名:</label>
  136. <input type="text" id="username">
  137. </div>
  138. <div class="form-group">
  139. <label for="password">密码:</label>
  140. <input type="password" id="password">
  141. </div>
  142. <button onclick="login()">登录</button>
  143. <div id="loginError" class="error hidden"></div>
  144. </div>
  145. <!-- 文件管理页面 -->
  146. <div id="filePage" class="hidden">
  147. <div class="header">
  148. <h1>LuatOS 文件管理系统</h1>
  149. <div>
  150. <label for="uploadTarget" style="margin-right: 10px; color: white;">上传目标:</label>
  151. <select id="uploadTarget" style="margin-right: 10px; padding: 5px;">
  152. <option value="/luadb">内存</option>
  153. <option value="/sd">SD卡</option>
  154. </select>
  155. <button onclick="scanFiles()" style="margin-right: 10px;">扫描文件</button>
  156. <button onclick="logout()">退出登录</button>
  157. </div>
  158. </div>
  159. <div class="breadcrumb" id="breadcrumb">
  160. <a onclick="navigateTo('/')">根目录</a>
  161. <span> | </span>
  162. <a onclick="navigateTo('/sd')">TF/SD目录</a>
  163. </div>
  164. <!-- 文件上传区域 -->
  165. <div style="margin-bottom: 20px; padding: 15px; background-color: #f9f9f9; border-radius: 5px;">
  166. <div class="form-group">
  167. <label for="fileUpload">选择文件 (最大200KB):</label>
  168. <input type="file" id="fileUpload" accept="*/*">
  169. </div>
  170. <button onclick="uploadFile()" id="uploadBtn">上传文件</button>
  171. <div id="uploadError" class="error hidden"></div>
  172. <div id="uploadProgress" style="display: none; margin-top: 10px;">
  173. <div style="width: 100%; background-color: #ddd; border-radius: 4px;">
  174. <div id="progressBar" style="width: 0%; height: 20px; background-color: #4CAF50; border-radius: 4px; transition: width 0.3s;"></div>
  175. </div>
  176. <div id="progressText" style="text-align: center; margin-top: 5px;">0%</div>
  177. </div>
  178. </div>
  179. <table class="file-list">
  180. <thead>
  181. <tr>
  182. <th>名称</th>
  183. <th>大小</th>
  184. <th>操作</th>
  185. </tr>
  186. </thead>
  187. <tbody id="fileListBody">
  188. </tbody>
  189. </table>
  190. </div>
  191. </div>
  192. <script>
  193. let currentPath = '/';
  194. let isLoggedIn = false;
  195. function login() {
  196. const username = document.getElementById('username').value;
  197. const password = document.getElementById('password').value;
  198. fetch('/login', {
  199. method: 'POST',
  200. headers: {
  201. 'Content-Type': 'application/json',
  202. },
  203. credentials: 'include', // 确保发送和接收cookies
  204. body: JSON.stringify({username: username, password: password})
  205. })
  206. .then(response => {
  207. // 打印所有响应头,查看Cookie设置
  208. console.log('登录响应头:', response.headers);
  209. return response.json();
  210. })
  211. .then(data => {
  212. console.log('登录响应数据:', data);
  213. if (data.success) {
  214. isLoggedIn = true;
  215. // 存储session_id到localStorage作为备用认证方式
  216. if (data.session_id) {
  217. localStorage.setItem('session_id', data.session_id);
  218. console.log('已存储SessionID到localStorage:', data.session_id);
  219. }
  220. document.getElementById('loginPage').classList.add('hidden');
  221. document.getElementById('filePage').classList.remove('hidden');
  222. loadFiles('/luadb');
  223. } else {
  224. document.getElementById('loginError').textContent = data.message || '登录失败';
  225. document.getElementById('loginError').classList.remove('hidden');
  226. }
  227. })
  228. .catch(error => {
  229. console.error('登录请求错误:', error);
  230. document.getElementById('loginError').textContent = '登录请求失败';
  231. document.getElementById('loginError').classList.remove('hidden');
  232. });
  233. }
  234. function logout() {
  235. fetch('/logout', {
  236. method: 'POST',
  237. credentials: 'include' // 确保发送cookies
  238. })
  239. .then(() => {
  240. isLoggedIn = false;
  241. // 清除localStorage中的session_id
  242. localStorage.removeItem('session_id');
  243. document.getElementById('filePage').classList.add('hidden');
  244. document.getElementById('loginPage').classList.remove('hidden');
  245. document.getElementById('username').value = '';
  246. document.getElementById('password').value = '';
  247. document.getElementById('loginError').classList.add('hidden');
  248. });
  249. }
  250. // 扫描文件函数
  251. function scanFiles() {
  252. if (!isLoggedIn) return;
  253. // 获取用户名和密码用于URL参数认证
  254. const username = document.getElementById('username')?.value || 'admin';
  255. const password = document.getElementById('password')?.value || '123456';
  256. // 构建带认证参数的扫描请求URL
  257. const url = '/scan-files?username=' + encodeURIComponent(username) +
  258. '&password=' + encodeURIComponent(password);
  259. console.log('发送文件扫描请求,URL:', url);
  260. // 显示扫描提示
  261. alert('开始扫描文件,请查看系统日志了解扫描进度...');
  262. fetch(url, {
  263. method: 'GET',
  264. credentials: 'include'
  265. })
  266. .then(response => {
  267. if (!response.ok) {
  268. throw new Error('扫描请求错误: ' + response.status);
  269. }
  270. return response.json();
  271. })
  272. .then(data => {
  273. console.log('扫描响应:', data);
  274. if (data && data.success) {
  275. alert('文件扫描完成!已扫描到 ' + data.foundFiles + ' 个文件,显示扫描到的用户文件。');
  276. // 重新加载文件列表
  277. loadFiles(currentPath);
  278. } else {
  279. alert('文件扫描失败: ' + (data.message || '未知错误'));
  280. }
  281. })
  282. .catch(error => {
  283. console.error('扫描文件请求错误:', error);
  284. alert('扫描文件请求失败');
  285. });
  286. }
  287. function loadFiles(path) {
  288. if (!isLoggedIn) return;
  289. // 准备请求头
  290. const headers = {
  291. 'Content-Type': 'application/json'
  292. };
  293. // 由于传统认证方式不可靠,我们使用URL参数认证
  294. // 获取用户名和密码用于URL参数认证
  295. const username = document.getElementById('username')?.value || 'admin';
  296. const password = document.getElementById('password')?.value || '123456';
  297. // 构建带认证参数的URL
  298. const url = '/list?path=' + encodeURIComponent(path) +
  299. '&username=' + encodeURIComponent(username) +
  300. '&password=' + encodeURIComponent(password);
  301. console.log('使用URL参数认证,请求URL:', url);
  302. fetch(url, {
  303. credentials: 'include', // 确保发送cookies
  304. headers: headers
  305. })
  306. .then(response => {
  307. if (!response.ok) {
  308. throw new Error('网络响应错误: ' + response.status);
  309. }
  310. return response.json();
  311. })
  312. .then(data => {
  313. console.log('文件列表数据:', data);
  314. // 只使用服务器返回的数据
  315. if (data && data.success && Array.isArray(data.files)) {
  316. displayFiles(data.files, path);
  317. } else {
  318. // 如果数据无效,显示空列表
  319. displayFiles([], path);
  320. }
  321. updateBreadcrumb(path);
  322. })
  323. .catch(error => {
  324. console.error('加载文件列表错误:', error);
  325. // 发生错误时显示空列表
  326. displayFiles([], path);
  327. updateBreadcrumb(path);
  328. });
  329. }
  330. function displayFiles(files, path) {
  331. const tbody = document.getElementById('fileListBody');
  332. tbody.innerHTML = '';
  333. // 确保files是数组
  334. if (!Array.isArray(files)) {
  335. files = [];
  336. }
  337. console.log('显示文件数量:', files.length);
  338. files.forEach(file => {
  339. // 确保文件对象有必要的属性
  340. const safeFile = {
  341. name: file.name || "未知文件名",
  342. size: file.size || 0,
  343. isDirectory: file.isDirectory || false,
  344. path: file.path || (path + '/' + (file.name || "未知文件名"))
  345. };
  346. const row = document.createElement('tr');
  347. let nameCell, actionCell;
  348. if (safeFile.isDirectory) {
  349. nameCell = `<td><a href="#" onclick="navigateTo('${encodeURIComponent(path + '/' + safeFile.name)}')">${safeFile.name}/</a></td>`;
  350. actionCell = '<td></td>';
  351. } else {
  352. nameCell = `<td>${safeFile.name}</td>`;
  353. // 为下载链接添加URL参数认证
  354. const username = document.getElementById('username')?.value || 'admin';
  355. const password = document.getElementById('password')?.value || '123456';
  356. const downloadUrl = '/download?path=' + encodeURIComponent(safeFile.path) +
  357. '&username=' + encodeURIComponent(username) +
  358. '&password=' + encodeURIComponent(password);
  359. // 添加下载和删除按钮
  360. actionCell = `<td>
  361. <a href="${downloadUrl}" class="download-btn" style="margin-right: 5px;">下载</a>
  362. <button class="delete-btn" onclick="deleteFile('${encodeURIComponent(safeFile.path)}')">删除</button>
  363. </td>`;
  364. }
  365. row.innerHTML = `
  366. ${nameCell}
  367. <td>${formatSize(safeFile.size)}</td>
  368. ${actionCell}
  369. `;
  370. tbody.appendChild(row);
  371. });
  372. }
  373. // 删除文件函数
  374. function deleteFile(filePath) {
  375. if (confirm('确定要删除这个文件吗?')) {
  376. // 获取用户名和密码用于URL参数认证
  377. const username = document.getElementById('username')?.value || 'admin';
  378. const password = document.getElementById('password')?.value || '123456';
  379. // 构建带认证参数的删除请求URL
  380. const url = '/delete?path=' + filePath +
  381. '&username=' + encodeURIComponent(username) +
  382. '&password=' + encodeURIComponent(password);
  383. console.log('使用URL参数认证进行删除操作,请求URL:', url);
  384. fetch(url, {
  385. method: 'POST',
  386. credentials: 'include'
  387. })
  388. .then(response => response.json())
  389. .then(data => {
  390. if (data.success) {
  391. // 删除成功后重新加载文件列表
  392. loadFiles(currentPath);
  393. } else {
  394. alert('删除失败: ' + (data.message || '未知错误'));
  395. }
  396. })
  397. .catch(error => {
  398. alert('删除请求失败');
  399. });
  400. }
  401. }
  402. function updateBreadcrumb(path) {
  403. const breadcrumb = document.getElementById('breadcrumb');
  404. // 先设置根目录和TF/SD目录链接
  405. breadcrumb.innerHTML = '<a onclick="navigateTo(\'\')">根目录</a><span> | </span><a onclick="navigateTo(\'/sd\')">TF/SD目录</a>';
  406. // 然后添加当前路径的层次结构(如果不是根目录)
  407. if (path !== '/' && path !== '/sd') {
  408. const parts = path.split('/').filter(p => p);
  409. let current = '';
  410. // 仅在非根目录和非SD目录时添加分隔符
  411. breadcrumb.innerHTML += ' > ';
  412. parts.forEach((part, index) => {
  413. current += '/' + part;
  414. if (index > 0) {
  415. breadcrumb.innerHTML += ' > ';
  416. }
  417. breadcrumb.innerHTML += '<a onclick="navigateTo(\'' + current + '\')">' + part + '</a>';
  418. });
  419. }
  420. }
  421. function navigateTo(path) {
  422. currentPath = path;
  423. loadFiles(path);
  424. }
  425. function formatSize(bytes) {
  426. if (bytes === 0) return '0 B';
  427. const k = 1024;
  428. const sizes = ['B', 'KB', 'MB', 'GB'];
  429. const i = Math.floor(Math.log(bytes) / Math.log(k));
  430. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  431. }
  432. // 文件上传函数
  433. function uploadFile() {
  434. const fileInput = document.getElementById('fileUpload');
  435. const uploadTarget = document.getElementById('uploadTarget').value;
  436. const uploadBtn = document.getElementById('uploadBtn');
  437. const uploadError = document.getElementById('uploadError');
  438. const uploadProgress = document.getElementById('uploadProgress');
  439. const progressBar = document.getElementById('progressBar');
  440. const progressText = document.getElementById('progressText');
  441. // 重置错误信息
  442. uploadError.textContent = '';
  443. uploadError.classList.add('hidden');
  444. // 检查是否选择了文件
  445. if (!fileInput.files || fileInput.files.length === 0) {
  446. uploadError.textContent = '请先选择要上传的文件';
  447. uploadError.classList.remove('hidden');
  448. return;
  449. }
  450. const file = fileInput.files[0];
  451. // 检查文件大小(200KB限制)
  452. if (file.size > 200 * 1024) {
  453. uploadError.textContent = '文件大小超过200KB限制';
  454. uploadError.classList.remove('hidden');
  455. return;
  456. }
  457. // 获取用户名和密码用于URL参数认证
  458. const username = document.getElementById('username')?.value || 'admin';
  459. const password = document.getElementById('password')?.value || '123456';
  460. // 构建上传URL
  461. const url = '/upload?path=' + encodeURIComponent(uploadTarget) +
  462. '&filename=' + encodeURIComponent(file.name) +
  463. '&username=' + encodeURIComponent(username) +
  464. '&password=' + encodeURIComponent(password);
  465. // 显示进度条
  466. uploadProgress.style.display = 'block';
  467. progressBar.style.width = '0%';
  468. progressText.textContent = '0%';
  469. uploadBtn.disabled = true;
  470. // 创建FormData对象
  471. const formData = new FormData();
  472. formData.append('file', file);
  473. // 使用XMLHttpRequest以便监控上传进度
  474. const xhr = new XMLHttpRequest();
  475. // 监听上传进度
  476. xhr.upload.addEventListener('progress', function(event) {
  477. if (event.lengthComputable) {
  478. const percentComplete = Math.round((event.loaded / event.total) * 100);
  479. progressBar.style.width = percentComplete + '%';
  480. progressText.textContent = percentComplete + '%';
  481. }
  482. });
  483. // 监听上传完成
  484. xhr.addEventListener('load', function() {
  485. if (xhr.status === 200) {
  486. try {
  487. const response = JSON.parse(xhr.responseText);
  488. if (response.success) {
  489. alert('文件上传成功!');
  490. // 重新加载当前路径的文件列表
  491. loadFiles(uploadTarget);
  492. // 重置文件选择
  493. fileInput.value = '';
  494. } else {
  495. uploadError.textContent = '上传失败: ' + (response.message || '未知错误');
  496. uploadError.classList.remove('hidden');
  497. }
  498. } catch (e) {
  499. uploadError.textContent = '上传成功但解析响应失败';
  500. uploadError.classList.remove('hidden');
  501. }
  502. } else {
  503. uploadError.textContent = '上传失败: 服务器响应错误';
  504. uploadError.classList.remove('hidden');
  505. }
  506. uploadBtn.disabled = false;
  507. });
  508. // 监听上传错误
  509. xhr.addEventListener('error', function() {
  510. uploadError.textContent = '上传失败: 网络错误';
  511. uploadError.classList.remove('hidden');
  512. uploadBtn.disabled = false;
  513. });
  514. // 发送请求
  515. xhr.open('POST', url);
  516. xhr.send(formData);
  517. }
  518. // 启动后检查认证状态
  519. window.onload = function() {
  520. fetch('/check-auth', {
  521. credentials: 'include' // 确保发送cookies
  522. })
  523. .then(response => response.json())
  524. .then(data => {
  525. if (data.authenticated) {
  526. isLoggedIn = true;
  527. document.getElementById('loginPage').classList.add('hidden');
  528. document.getElementById('filePage').classList.remove('hidden');
  529. loadFiles('/luadb');
  530. }
  531. });
  532. };
  533. </script>
  534. </body>
  535. </html>