wasm-rdp/webrdp/assets/file.js
2024-05-08 22:56:40 +08:00

109 lines
3.9 KiB
JavaScript

window.filegatewayUrl = "/filegw/"
function initFile() {
window.fileShareName = "drive_c$";
window.currentPath = [];
listFile();
setInterval(listFile, 20000);
}
function getCurrentPath() { return window.filegatewayUrl + window.fileSessionId + "/" + encodeURI(window.fileShareName) + "/" + encodeURI(window.currentPath.join("/")) + "/" }
function getSize(fileSize) {
var size = parseFloat(fileSize);
var units = ['B', 'KB', 'MB', 'GB', 'TB'];
var i = 0;
while (size >= 1024 && i < units.length - 1) {
size /= 1024;
i++;
}
return size.toFixed(2) + ' ' + units[i];
}
function listFile() {
$.ajax({
url: getCurrentPath(),
method: "GET",
success: function (response) {
var data = response;
var table = document.getElementById("filesharelist");
table.innerHTML = "";
if (window.currentPath.length > 0) {
var row = table.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "<a href='#' onclick='window.currentPath=window.currentPath.slice(0,-1);updatePathText();listFile();false'>↑🔝🔝🔝↑</a>";
cell2.innerHTML = "向上";
}
for (var i = 0; i < data.entries.length; i++) {
if (data.entries[i].fileAttributes.indexOf("Hidden") != -1) continue;
var row = table.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var filename = data.entries[i].filename.slice(2);
if (data.entries[i].type == "Directory") {
cell1.innerHTML = "<a href='#' onclick='onClickOpenDir(this)'>" + filename + "</a>";
cell2.innerHTML = "目录";
}
else {
cell1.innerHTML = "<a href='" + getCurrentPath() + encodeURI(filename) +"'>" + filename + "</a>";
cell2.innerHTML = getSize(data.entries[i].fileSize);
}
}
},
error: function (xhr, status, error) {
alert("文件夹列出失败,可能没有权限或者凭证过期,刷新网页后再试");
}
});
}
function uploadFile(force) {
var fileInput = document.getElementById('fileupload');
var file = fileInput.files[0];
var suffix = "";
var blob = new Blob([file], { type: "application/octet-stream" });
if (force)
suffix="?force=1"
$.ajax({
url: getCurrentPath() + encodeURI(file.name) + suffix,
method: "POST",
data: blob,
contentType: false,
processData: false,
success: function (response) {
if (response.exist && !force) {
if (confirm("文件已存在,是否覆盖?")) {
uploadFile(true);
}
}
else if (!response.exist && !response.error)
alert("上传成功");
},
error: function (xhr, status, error) {
alert("上传失败,可能没有权限访问。")
console.error("upload error", xhr, status, error)
}
});
}
function mkdir() {
var dirName = prompt("请输入文件夹名称");
if (dirName) {
$.ajax({
url: getCurrentPath() + encodeURI(dirName),
method: "PUT",
success: function (response) {
if (!response.error)
alert("创建成功");
},
error: function (xhr, status, error) {
alert("创建文件夹失败,可能是没有权限");
}
});
}
}
function onClickOpenDir(element) {
window.currentPath = window.currentPath.concat(element.innerHTML);
updatePathText();
listFile();
}
function updatePathText() {
$("#currentPath").text("/"+window.fileShareName+"/"+window.currentPath.join("/")+"/")
}