wasm-rdp/webrdp/assets/file.js

170 lines
6.3 KiB
JavaScript

function initFile(sessionId, filegateway) {
window.fileSessionId = sessionId;
window.filegatewayUrl = filegateway.url
window.fileShareName = filegateway.shares[0];
var shares = [];
for (var i = 0; i < filegateway.shares.length; i++) {
shares.push($('<option></option>').val(filegateway.shares[i]).text(filegateway.shares[i]));
}
console.log("shares", shares);
$('#shares').append(shares);
window.currentPath = [];
updatePathText();
listFile();
setInterval(listFile, 20000);
}
function updateShareChoice(shareName, event) {
window.fileShareName = shareName;
window.currentPath = [];
updatePathText();
listFile();
if (event) {
event.preventDefault();
return false;
}
}
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) +"' target=\"_blank\">" + 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" });
$.ajax({
url: getCurrentPath() + encodeURI(file.name) + suffix,
method: "POST",
data: blob,
contentType: false,
processData: false,
success: function (response) {
console.log("upload response", response)
if (response.exists && !force) {
$.confirm({
title: '文件已存在!',
content: '是否替换?',
buttons: {
replace: {
text: '替换',
btnClass: 'btn-red',
keys: ['enter', 'shift'],
action: function () {
uploadFile(true)
}
},
cancel: {
text: '取消',
action: function () {}
}
}
});
}
else if (!response.exists && !response.error)
$.alert("上传成功");
},
error: function (xhr, status, error) {
$.alert("上传失败,可能没有权限访问。")
console.error("upload error", xhr, status, error)
}
});
}
function mkdir() {
$.confirm({
title: "请输入文件夹名称",
content: '' +
'<form action="" class="formName">' +
'<div class="form-group">' +
'<label>文件夹:</label>' +
'<input type="text" placeholder="名称" class="name form-control" required />' +
'</div>' +
'</form>',
buttons: {
formSubmit: {
text: '确定',
btnClass: 'btn-blue',
action: function () {
var dirName = this.$content.find('.name').val();
$.ajax({
url: getCurrentPath() + encodeURI(dirName),
method: "PUT",
success: function (response) {
if (!response.error)
$.alert("创建成功");
},
error: function (xhr, status, error) {
$.alert("创建文件夹失败,可能是没有权限");
}
});
}
},
cancel: {
text: '取消',
action: function() {}
},
},
onContentReady: function () {
// bind to events
var jc = this;
this.$content.find('form').on('submit', function (e) {
// if the user submits the form by pressing enter in the field.
e.preventDefault();
jc.$$formSubmit.trigger('click'); // reference the button and click it
});
}
});
}
function onClickOpenDir(element) {
window.currentPath = window.currentPath.concat(element.innerHTML);
updatePathText();
listFile();
}
function updatePathText() {
$("#currentPath").text("/"+window.fileShareName+"/"+window.currentPath.join("/")+(window.currentPath.length==0?"":"/"))
}