| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>LuatOS 文件管理系统</title>
- <style>
- body {
- font-family: 'Microsoft YaHei', Arial, sans-serif;
- background-color: #f5f5f5;
- margin: 0;
- padding: 20px;
- color: #333;
- }
- .container {
- max-width: 1200px;
- margin: auto;
- background: white;
- padding: 20px;
- border-radius: 8px;
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
- }
- .header {
- background: #2c3e50;
- color: white;
- padding: 15px;
- border-radius: 5px;
- margin-bottom: 20px;
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- .login-form {
- max-width: 400px;
- margin: 100px auto;
- padding: 40px;
- background: white;
- border-radius: 8px;
- box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
- }
- .form-group {
- margin-bottom: 15px;
- }
- label {
- display: block;
- margin-bottom: 5px;
- font-weight: bold;
- }
- input[type="text"], input[type="password"] {
- width: 100%;
- padding: 10px;
- border: 1px solid #ddd;
- border-radius: 4px;
- box-sizing: border-box;
- }
- button {
- background-color: #3498db;
- color: white;
- border: none;
- padding: 10px 20px;
- border-radius: 4px;
- cursor: pointer;
- font-size: 16px;
- }
- button:hover {
- background-color: #2980b9;
- }
- .file-list {
- width: 100%;
- border-collapse: collapse;
- margin-top: 20px;
- }
- .file-list th, .file-list td {
- padding: 12px;
- text-align: left;
- border-bottom: 1px solid #ddd;
- }
- .file-list th {
- background-color: #f8f9fa;
- font-weight: bold;
- }
- .file-list tr:hover {
- background-color: #f5f5f5;
- }
- .download-btn {
- background-color: #27ae60;
- color: white;
- padding: 5px 10px;
- text-decoration: none;
- border-radius: 3px;
- font-size: 12px;
- }
- .download-btn:hover {
- background-color: #219a52;
- }
- .delete-btn {
- background-color: #e74c3c;
- color: white;
- border: none;
- padding: 5px 10px;
- border-radius: 3px;
- font-size: 12px;
- cursor: pointer;
- }
- .delete-btn:hover {
- background-color: #c0392b;
- }
- .breadcrumb {
- padding: 10px 0;
- margin-bottom: 20px;
- }
- .breadcrumb a {
- color: #3498db;
- text-decoration: none;
- cursor: pointer;
- }
- .breadcrumb a:hover {
- text-decoration: underline;
- }
- .hidden {
- display: none;
- }
- .error {
- color: #e74c3c;
- margin-top: 10px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <!-- 登录页面 -->
- <div id="loginPage" class="login-form">
- <h2>LuatOS 文件管理系统登录</h2>
- <div class="form-group">
- <label for="username">用户名:</label>
- <input type="text" id="username">
- </div>
- <div class="form-group">
- <label for="password">密码:</label>
- <input type="password" id="password">
- </div>
- <button onclick="login()">登录</button>
- <div id="loginError" class="error hidden"></div>
- </div>
- <!-- 文件管理页面 -->
- <div id="filePage" class="hidden">
- <div class="header">
- <h1>LuatOS 文件管理系统</h1>
- <div>
- <label for="uploadTarget" style="margin-right: 10px; color: white;">上传目标:</label>
- <select id="uploadTarget" style="margin-right: 10px; padding: 5px;">
- <option value="/luadb">内存</option>
- <option value="/sd">SD卡</option>
- </select>
- <button onclick="scanFiles()" style="margin-right: 10px;">扫描文件</button>
- <button onclick="logout()">退出登录</button>
- </div>
- </div>
- <div class="breadcrumb" id="breadcrumb">
- <a onclick="navigateTo('/')">根目录</a>
- <span> | </span>
- <a onclick="navigateTo('/sd')">TF/SD目录</a>
- </div>
-
- <!-- 文件上传区域 -->
- <div style="margin-bottom: 20px; padding: 15px; background-color: #f9f9f9; border-radius: 5px;">
- <div class="form-group">
- <label for="fileUpload">选择文件 (最大200KB):</label>
- <input type="file" id="fileUpload" accept="*/*">
- </div>
- <button onclick="uploadFile()" id="uploadBtn">上传文件</button>
- <div id="uploadError" class="error hidden"></div>
- <div id="uploadProgress" style="display: none; margin-top: 10px;">
- <div style="width: 100%; background-color: #ddd; border-radius: 4px;">
- <div id="progressBar" style="width: 0%; height: 20px; background-color: #4CAF50; border-radius: 4px; transition: width 0.3s;"></div>
- </div>
- <div id="progressText" style="text-align: center; margin-top: 5px;">0%</div>
- </div>
- </div>
- <table class="file-list">
- <thead>
- <tr>
- <th>名称</th>
- <th>大小</th>
- <th>操作</th>
- </tr>
- </thead>
- <tbody id="fileListBody">
- </tbody>
- </table>
- </div>
- </div>
- <script>
- let currentPath = '/';
- let isLoggedIn = false;
- function login() {
- const username = document.getElementById('username').value;
- const password = document.getElementById('password').value;
- fetch('/login', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- credentials: 'include', // 确保发送和接收cookies
- body: JSON.stringify({username: username, password: password})
- })
- .then(response => {
- // 打印所有响应头,查看Cookie设置
- console.log('登录响应头:', response.headers);
- return response.json();
- })
- .then(data => {
- console.log('登录响应数据:', data);
- if (data.success) {
- isLoggedIn = true;
- // 存储session_id到localStorage作为备用认证方式
- if (data.session_id) {
- localStorage.setItem('session_id', data.session_id);
- console.log('已存储SessionID到localStorage:', data.session_id);
- }
- document.getElementById('loginPage').classList.add('hidden');
- document.getElementById('filePage').classList.remove('hidden');
- loadFiles('/luadb');
- } else {
- document.getElementById('loginError').textContent = data.message || '登录失败';
- document.getElementById('loginError').classList.remove('hidden');
- }
- })
- .catch(error => {
- console.error('登录请求错误:', error);
- document.getElementById('loginError').textContent = '登录请求失败';
- document.getElementById('loginError').classList.remove('hidden');
- });
- }
- function logout() {
- fetch('/logout', {
- method: 'POST',
- credentials: 'include' // 确保发送cookies
- })
- .then(() => {
- isLoggedIn = false;
- // 清除localStorage中的session_id
- localStorage.removeItem('session_id');
- document.getElementById('filePage').classList.add('hidden');
- document.getElementById('loginPage').classList.remove('hidden');
- document.getElementById('username').value = '';
- document.getElementById('password').value = '';
- document.getElementById('loginError').classList.add('hidden');
- });
- }
- // 扫描文件函数
- function scanFiles() {
- if (!isLoggedIn) return;
- // 获取用户名和密码用于URL参数认证
- const username = document.getElementById('username')?.value || 'admin';
- const password = document.getElementById('password')?.value || '123456';
- // 构建带认证参数的扫描请求URL
- const url = '/scan-files?username=' + encodeURIComponent(username) +
- '&password=' + encodeURIComponent(password);
- console.log('发送文件扫描请求,URL:', url);
- // 显示扫描提示
- alert('开始扫描文件,请查看系统日志了解扫描进度...');
- fetch(url, {
- method: 'GET',
- credentials: 'include'
- })
- .then(response => {
- if (!response.ok) {
- throw new Error('扫描请求错误: ' + response.status);
- }
- return response.json();
- })
- .then(data => {
- console.log('扫描响应:', data);
- if (data && data.success) {
- alert('文件扫描完成!已扫描到 ' + data.foundFiles + ' 个文件,显示扫描到的用户文件。');
- // 重新加载文件列表
- loadFiles(currentPath);
- } else {
- alert('文件扫描失败: ' + (data.message || '未知错误'));
- }
- })
- .catch(error => {
- console.error('扫描文件请求错误:', error);
- alert('扫描文件请求失败');
- });
- }
- function loadFiles(path) {
- if (!isLoggedIn) return;
- // 准备请求头
- const headers = {
- 'Content-Type': 'application/json'
- };
- // 由于传统认证方式不可靠,我们使用URL参数认证
- // 获取用户名和密码用于URL参数认证
- const username = document.getElementById('username')?.value || 'admin';
- const password = document.getElementById('password')?.value || '123456';
- // 构建带认证参数的URL
- const url = '/list?path=' + encodeURIComponent(path) +
- '&username=' + encodeURIComponent(username) +
- '&password=' + encodeURIComponent(password);
- console.log('使用URL参数认证,请求URL:', url);
- fetch(url, {
- credentials: 'include', // 确保发送cookies
- headers: headers
- })
- .then(response => {
- if (!response.ok) {
- throw new Error('网络响应错误: ' + response.status);
- }
- return response.json();
- })
- .then(data => {
- console.log('文件列表数据:', data);
- // 只使用服务器返回的数据
- if (data && data.success && Array.isArray(data.files)) {
- displayFiles(data.files, path);
- } else {
- // 如果数据无效,显示空列表
- displayFiles([], path);
- }
- updateBreadcrumb(path);
- })
- .catch(error => {
- console.error('加载文件列表错误:', error);
- // 发生错误时显示空列表
- displayFiles([], path);
- updateBreadcrumb(path);
- });
- }
- function displayFiles(files, path) {
- const tbody = document.getElementById('fileListBody');
- tbody.innerHTML = '';
- // 确保files是数组
- if (!Array.isArray(files)) {
- files = [];
- }
- console.log('显示文件数量:', files.length);
- files.forEach(file => {
- // 确保文件对象有必要的属性
- const safeFile = {
- name: file.name || "未知文件名",
- size: file.size || 0,
- isDirectory: file.isDirectory || false,
- path: file.path || (path + '/' + (file.name || "未知文件名"))
- };
- const row = document.createElement('tr');
- let nameCell, actionCell;
- if (safeFile.isDirectory) {
- nameCell = `<td><a href="#" onclick="navigateTo('${encodeURIComponent(path + '/' + safeFile.name)}')">${safeFile.name}/</a></td>`;
- actionCell = '<td></td>';
- } else {
- nameCell = `<td>${safeFile.name}</td>`;
- // 为下载链接添加URL参数认证
- const username = document.getElementById('username')?.value || 'admin';
- const password = document.getElementById('password')?.value || '123456';
- const downloadUrl = '/download?path=' + encodeURIComponent(safeFile.path) +
- '&username=' + encodeURIComponent(username) +
- '&password=' + encodeURIComponent(password);
- // 添加下载和删除按钮
- actionCell = `<td>
- <a href="${downloadUrl}" class="download-btn" style="margin-right: 5px;">下载</a>
- <button class="delete-btn" onclick="deleteFile('${encodeURIComponent(safeFile.path)}')">删除</button>
- </td>`;
- }
- row.innerHTML = `
- ${nameCell}
- <td>${formatSize(safeFile.size)}</td>
- ${actionCell}
- `;
- tbody.appendChild(row);
- });
- }
- // 删除文件函数
- function deleteFile(filePath) {
- if (confirm('确定要删除这个文件吗?')) {
- // 获取用户名和密码用于URL参数认证
- const username = document.getElementById('username')?.value || 'admin';
- const password = document.getElementById('password')?.value || '123456';
- // 构建带认证参数的删除请求URL
- const url = '/delete?path=' + filePath +
- '&username=' + encodeURIComponent(username) +
- '&password=' + encodeURIComponent(password);
- console.log('使用URL参数认证进行删除操作,请求URL:', url);
- fetch(url, {
- method: 'POST',
- credentials: 'include'
- })
- .then(response => response.json())
- .then(data => {
- if (data.success) {
- // 删除成功后重新加载文件列表
- loadFiles(currentPath);
- } else {
- alert('删除失败: ' + (data.message || '未知错误'));
- }
- })
- .catch(error => {
- alert('删除请求失败');
- });
- }
- }
- function updateBreadcrumb(path) {
- const breadcrumb = document.getElementById('breadcrumb');
- // 先设置根目录和TF/SD目录链接
- breadcrumb.innerHTML = '<a onclick="navigateTo(\'\')">根目录</a><span> | </span><a onclick="navigateTo(\'/sd\')">TF/SD目录</a>';
- // 然后添加当前路径的层次结构(如果不是根目录)
- if (path !== '/' && path !== '/sd') {
- const parts = path.split('/').filter(p => p);
- let current = '';
- // 仅在非根目录和非SD目录时添加分隔符
- breadcrumb.innerHTML += ' > ';
- parts.forEach((part, index) => {
- current += '/' + part;
- if (index > 0) {
- breadcrumb.innerHTML += ' > ';
- }
- breadcrumb.innerHTML += '<a onclick="navigateTo(\'' + current + '\')">' + part + '</a>';
- });
- }
- }
- function navigateTo(path) {
- currentPath = path;
- loadFiles(path);
- }
- function formatSize(bytes) {
- if (bytes === 0) return '0 B';
- const k = 1024;
- const sizes = ['B', 'KB', 'MB', 'GB'];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
- return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
- }
- // 文件上传函数
- function uploadFile() {
- const fileInput = document.getElementById('fileUpload');
- const uploadTarget = document.getElementById('uploadTarget').value;
- const uploadBtn = document.getElementById('uploadBtn');
- const uploadError = document.getElementById('uploadError');
- const uploadProgress = document.getElementById('uploadProgress');
- const progressBar = document.getElementById('progressBar');
- const progressText = document.getElementById('progressText');
- // 重置错误信息
- uploadError.textContent = '';
- uploadError.classList.add('hidden');
- // 检查是否选择了文件
- if (!fileInput.files || fileInput.files.length === 0) {
- uploadError.textContent = '请先选择要上传的文件';
- uploadError.classList.remove('hidden');
- return;
- }
- const file = fileInput.files[0];
-
- // 检查文件大小(200KB限制)
- if (file.size > 200 * 1024) {
- uploadError.textContent = '文件大小超过200KB限制';
- uploadError.classList.remove('hidden');
- return;
- }
- // 获取用户名和密码用于URL参数认证
- const username = document.getElementById('username')?.value || 'admin';
- const password = document.getElementById('password')?.value || '123456';
- // 构建上传URL
- const url = '/upload?path=' + encodeURIComponent(uploadTarget) +
- '&filename=' + encodeURIComponent(file.name) +
- '&username=' + encodeURIComponent(username) +
- '&password=' + encodeURIComponent(password);
- // 显示进度条
- uploadProgress.style.display = 'block';
- progressBar.style.width = '0%';
- progressText.textContent = '0%';
- uploadBtn.disabled = true;
- // 创建FormData对象
- const formData = new FormData();
- formData.append('file', file);
- // 使用XMLHttpRequest以便监控上传进度
- const xhr = new XMLHttpRequest();
- // 监听上传进度
- xhr.upload.addEventListener('progress', function(event) {
- if (event.lengthComputable) {
- const percentComplete = Math.round((event.loaded / event.total) * 100);
- progressBar.style.width = percentComplete + '%';
- progressText.textContent = percentComplete + '%';
- }
- });
- // 监听上传完成
- xhr.addEventListener('load', function() {
- if (xhr.status === 200) {
- try {
- const response = JSON.parse(xhr.responseText);
- if (response.success) {
- alert('文件上传成功!');
- // 重新加载当前路径的文件列表
- loadFiles(uploadTarget);
- // 重置文件选择
- fileInput.value = '';
- } else {
- uploadError.textContent = '上传失败: ' + (response.message || '未知错误');
- uploadError.classList.remove('hidden');
- }
- } catch (e) {
- uploadError.textContent = '上传成功但解析响应失败';
- uploadError.classList.remove('hidden');
- }
- } else {
- uploadError.textContent = '上传失败: 服务器响应错误';
- uploadError.classList.remove('hidden');
- }
- uploadBtn.disabled = false;
- });
- // 监听上传错误
- xhr.addEventListener('error', function() {
- uploadError.textContent = '上传失败: 网络错误';
- uploadError.classList.remove('hidden');
- uploadBtn.disabled = false;
- });
- // 发送请求
- xhr.open('POST', url);
- xhr.send(formData);
- }
- // 启动后检查认证状态
- window.onload = function() {
- fetch('/check-auth', {
- credentials: 'include' // 确保发送cookies
- })
- .then(response => response.json())
- .then(data => {
- if (data.authenticated) {
- isLoggedIn = true;
- document.getElementById('loginPage').classList.add('hidden');
- document.getElementById('filePage').classList.remove('hidden');
- loadFiles('/luadb');
- }
- });
- };
- </script>
- </body>
- </html>
|