fix bugs and add html page
This commit is contained in:
255
static/common.html
Normal file
255
static/common.html
Normal file
@ -0,0 +1,255 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Unified</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Unified</h1>
|
||||
|
||||
<div>
|
||||
<h2>Current Directory: <span id="currentPath">/</span></h2>
|
||||
<button onclick="goToParent()">Parent Directory</button>
|
||||
<button onclick="refreshDirectory()">Refresh</button>
|
||||
<input type="text" id="newDirName" placeholder="New directory name">
|
||||
<button onclick="createDirectoryHere()">Create Directory</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>Directory Contents</h3>
|
||||
<div id="directoryContents">Loading...</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>File Operations</h3>
|
||||
<input type="file" id="fileUpload">
|
||||
<button onclick="uploadFileHere()">Upload to Current Directory</button>
|
||||
<div id="fileOperationResult"></div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h3>Navigation</h3>
|
||||
<input type="text" id="pathInput" placeholder="Enter path to navigate to">
|
||||
<button onclick="navigateToPath()">Go to Path</button>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const baseUrl = 'http://localhost:8080';
|
||||
let currentPath = '';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
refreshDirectory();
|
||||
});
|
||||
|
||||
function updateCurrentPathDisplay() {
|
||||
document.getElementById('currentPath').textContent = currentPath;
|
||||
document.getElementById('pathInput').value = currentPath;
|
||||
}
|
||||
|
||||
async function refreshDirectory() {
|
||||
const contentsDiv = document.getElementById('directoryContents');
|
||||
contentsDiv.innerHTML = 'Loading...';
|
||||
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/dirs/${decodeURIComponent(currentPath)}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
displayDirectoryContents(data);
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
contentsDiv.innerHTML = `<p style="color: red;">Error loading directory: ${response.status} - ${errorText}</p>`;
|
||||
}
|
||||
} catch (error) {
|
||||
contentsDiv.innerHTML = `<p style="color: red;">Network error: ${error.message}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
function displayDirectoryContents(items) {
|
||||
const contentsDiv = document.getElementById('directoryContents');
|
||||
|
||||
if (!Array.isArray(items) || items.length === 0) {
|
||||
contentsDiv.innerHTML = '<p>Directory is empty</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
let html = '<table border="1">';
|
||||
html += '<tr><th>Name</th><th>Type</th><th>Actions</th></tr>';
|
||||
|
||||
items.forEach(item => {
|
||||
const isDirectory = item.endsWith('/');
|
||||
const name = item;
|
||||
const fullPath = item;
|
||||
|
||||
html += '<tr>';
|
||||
html += `<td>${name}</td>`;
|
||||
html += `<td>${isDirectory ? 'Directory' : 'File'}</td>`;
|
||||
html += '<td>';
|
||||
|
||||
if (isDirectory) {
|
||||
html += `<button onclick="navigateToDirectory('${fullPath}')">Open</button> `;
|
||||
} else {
|
||||
html += `<button onclick="downloadFileFromPath('${fullPath}')">Download</button> `;
|
||||
}
|
||||
|
||||
html += `<button onclick="removeItem('${fullPath}')" style="color: red;">Delete</button>`;
|
||||
html += '</td>';
|
||||
html += '</tr>';
|
||||
});
|
||||
|
||||
html += '</table>';
|
||||
contentsDiv.innerHTML = html;
|
||||
}
|
||||
|
||||
function navigateToDirectory(path) {
|
||||
currentPath = path;
|
||||
updateCurrentPathDisplay();
|
||||
refreshDirectory();
|
||||
}
|
||||
|
||||
function navigateToPath() {
|
||||
const path = document.getElementById('pathInput').value;
|
||||
navigateToDirectory(path);
|
||||
}
|
||||
|
||||
function goToParent() {
|
||||
if (currentPath !== '') {
|
||||
const parts = currentPath.split('/').filter(p => p);
|
||||
parts.pop();
|
||||
currentPath = parts.length > 0 ? '' + parts.join('/') + '/' : '';
|
||||
updateCurrentPathDisplay();
|
||||
refreshDirectory();
|
||||
}
|
||||
}
|
||||
|
||||
async function createDirectoryHere() {
|
||||
const dirName = document.getElementById('newDirName').value;
|
||||
const resultDiv = document.getElementById('fileOperationResult');
|
||||
|
||||
if (!dirName) {
|
||||
resultDiv.innerHTML = '<p style="color: red;">Please enter a directory name</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
const newDirPath = currentPath === '' ? `${dirName}/` : `${currentPath}${dirName}/`;
|
||||
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/dirs`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ path: newDirPath })
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
resultDiv.innerHTML = `<p style="color: green;">Directory created: ${dirName}</p>`;
|
||||
document.getElementById('newDirName').value = '';
|
||||
await refreshDirectory();
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
resultDiv.innerHTML = `<p style="color: red;">Error: ${response.status} - ${errorText}</p>`;
|
||||
}
|
||||
} catch (error) {
|
||||
resultDiv.innerHTML = `<p style="color: red;">Network error: ${error.message}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadFileHere() {
|
||||
const fileInput = document.getElementById('fileUpload');
|
||||
const resultDiv = document.getElementById('fileOperationResult');
|
||||
|
||||
if (!fileInput.files[0]) {
|
||||
resultDiv.innerHTML = '<p style="color: red;">Please select a file</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
const file = fileInput.files[0];
|
||||
const filePath = currentPath === '/' ? `/${file.name}` : `${currentPath}/${file.name}`;
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('dirPath', JSON.stringify({ path: filePath }));
|
||||
|
||||
try {
|
||||
resultDiv.innerHTML = '<p>Uploading...</p>';
|
||||
|
||||
const response = await fetch(`${baseUrl}/files`, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
resultDiv.innerHTML = `<p style="color: green;">File uploaded: ${file.name}</p>`;
|
||||
fileInput.value = '';
|
||||
await refreshDirectory();
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
resultDiv.innerHTML = `<p style="color: red;">Error: ${response.status} - ${errorText}</p>`;
|
||||
}
|
||||
} catch (error) {
|
||||
resultDiv.innerHTML = `<p style="color: red;">Network error: ${error.message}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadFileFromPath(filePath) {
|
||||
const resultDiv = document.getElementById('fileOperationResult');
|
||||
|
||||
try {
|
||||
resultDiv.innerHTML = '<p>Downloading...</p>';
|
||||
|
||||
const response = await fetch(`${baseUrl}/files/${decodeURIComponent(filePath)}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filePath.split('/').pop() || 'download';
|
||||
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
|
||||
resultDiv.innerHTML = `<p style="color: green;">File downloaded successfully</p>`;
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
resultDiv.innerHTML = `<p style="color: red;">Error: ${response.status} - ${errorText}</p>`;
|
||||
}
|
||||
} catch (error) {
|
||||
resultDiv.innerHTML = `<p style="color: red;">Network error: ${error.message}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
async function removeItem(path) {
|
||||
const resultDiv = document.getElementById('fileOperationResult');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/common`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ path: path })
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
resultDiv.innerHTML = `<p style="color: green;">Successfully removed: ${path}</p>`;
|
||||
await refreshDirectory();
|
||||
} else {
|
||||
const errorText = await response.text();
|
||||
resultDiv.innerHTML = `<p style="color: red;">Error: ${response.status} - ${errorText}</p>`;
|
||||
}
|
||||
} catch (error) {
|
||||
resultDiv.innerHTML = `<p style="color: red;">Network error: ${error.message}</p>`;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user