diff options
| author | Roland Stigge <stigge@antcom.de> | 2018-01-07 16:07:49 +0100 | 
|---|---|---|
| committer | Roland Stigge <stigge@antcom.de> | 2018-01-07 16:07:49 +0100 | 
| commit | fb24f0bc60d9ff910d49646aaeadad99f22826e5 (patch) | |
| tree | e3a92e1bfd8c6c4c1f34137700371e68ad2e0340 | |
| parent | ce94c74b8bfb69b94bdb78aaa60d7dce2d058184 (diff) | |
Implemented file move
| -rw-r--r-- | TODO | 6 | ||||
| -rw-r--r-- | html/index.html | 13 | ||||
| -rw-r--r-- | html/webbox.js | 100 | ||||
| -rw-r--r-- | src/webbox.cpp | 110 | 
4 files changed, 220 insertions, 9 deletions
| @@ -1,11 +1,9 @@  Prio 1 (for next version)  ====== -Icons: menu, file, directory +Icons: menu, refresh, file, directory  debian/ -implement TBD features  handle spaces/umlauts -download zip  Prio 2 (for future versions)  ====== @@ -16,6 +14,6 @@ chromecast  player  handle writability  register GET/POST functions -handle too small window +handle too small popup window  sandclock during operations  i18n diff --git a/html/index.html b/html/index.html index 3cb221d..49dafef 100644 --- a/html/index.html +++ b/html/index.html @@ -8,7 +8,7 @@  	</head>  	<body onload="initMainpage();">  		<div> -			<h1 class="title"></h1> +			<h1 class="title"> </h1>  			<input id="uploadfile" type="file" onchange="onUploadFile();" hidden/>  			<table class="menu">  				<tr> @@ -16,7 +16,7 @@  					<td class="entry" onclick="createDir();">New folder</td>  					<td class="entry" onclick="download();">Download</td>  					<td class="entry" onclick="upload();">Upload</td> -					<td class="entry" onclick="deleteItem();">Delete</td> +					<td class="entry" onclick="deleteItems();">Delete</td>  					<td class="entry" onclick="move();">Move</td>  					<td class="entry" onclick="info();">Info</td>  					<td class="entry" onclick="selectAll();">Select All</td> @@ -49,6 +49,15 @@  			Download multiple files as ZIP?<br>  		</div> +		<div id="move-dialog" hidden> +			Moving files to folder:<br> +			<input type="text" id="movedir" class="textinput"></input> +			<br/> +			Files to move: +			<br/> +			<div id="movefiles"></div> +		</div> +  		<a download="webbox-download.zip" id="download-a" hidden/>  		<div class="footer"> diff --git a/html/webbox.js b/html/webbox.js index faa2bb2..58b04fb 100644 --- a/html/webbox.js +++ b/html/webbox.js @@ -341,6 +341,7 @@ function createDir() {  	}  	document.getElementById("dialog").innerHTML = document.getElementById("create-dir-dialog").innerHTML; +	document.getElementById("newdir").focus();  }  function upload() { @@ -373,12 +374,105 @@ function onUploadFile() {          xhr.send(formData);  } -function deleteItem() { -	alert("TBD"); +function deleteItems() { +	showDialog(); +	 +	var files = getSelectedFiles(); +	if (files.length == 0) { +		document.getElementById("dialog").innerHTML = "No files selected."; +		document.getElementById("okbutton").onclick = hideDialog; +		return; +	} + +	var message = "Are sure to delete the following files and directories?<br/><br/>"; + +	for (var i = 0; i < files.length; i++) { +		message += files[i] + "<br/>"; +	} + +	document.getElementById("dialog").innerHTML = message; +	 +	document.getElementById("okbutton").onclick = function() { + +		var xhr = new XMLHttpRequest(); + +		xhr.onreadystatechange = function() { +			if (this.readyState != 4 || this.status != 200) { +				return; +			} + +			document.getElementById("dialog").innerHTML = xhr.responseText; +			document.getElementById("okbutton").onclick = hideDialog; +			loadContents(currentDir); // load new file list with deleted items +		} + +		var parser = new DOMParser(); +		var xmlDocument = parser.parseFromString("<files></files>", "text/xml"); +		var filesElement = xmlDocument.getElementsByTagName("files")[0]; + +		for (var i = 0; i < files.length; i++) { +			var fileElement = xmlDocument.createElement("file"); +			fileElement.appendChild(document.createTextNode(files[i])); +			filesElement.appendChild(fileElement); +		} + +		xhr.open("POST", "/bin/query" + currentDir + "?command=delete", true); +		xhr.setRequestHeader("Content-type", "text/xml"); +		xhr.send(xmlDocument); +	}  }  function move() { -	alert("TBD"); +	showDialog(); +	 +	var files = getSelectedFiles(); +	if (files.length == 0) { +		document.getElementById("dialog").innerHTML = "No files selected."; +		document.getElementById("okbutton").onclick = hideDialog; +		return; +	} + +	var message = ""; +	for (var i = 0; i < files.length; i++) { +		message += files[i] + "<br/>"; +	} + +	document.getElementById("dialog").innerHTML = document.getElementById("move-dialog").innerHTML; +	document.getElementById("movedir").focus(); +	document.getElementById("movefiles").innerHTML = message; +	 +	document.getElementById("okbutton").onclick = function() { + +		var xhr = new XMLHttpRequest(); + +		xhr.onreadystatechange = function() { +			if (this.readyState != 4 || this.status != 200) { +				return; +			} + +			document.getElementById("dialog").innerHTML = xhr.responseText; +			document.getElementById("okbutton").onclick = hideDialog; +			loadContents(currentDir); // load new file list with deleted items +		} + +		var parser = new DOMParser(); +		var xmlDocument = parser.parseFromString("<request></request>", "text/xml"); +		var filesElement = xmlDocument.getElementsByTagName("request")[0]; +			 +		var targetElement = xmlDocument.createElement("target"); +		targetElement.appendChild(document.createTextNode(document.getElementById("movedir").value)); +		filesElement.appendChild(targetElement); + +		for (var i = 0; i < files.length; i++) { +			var fileElement = xmlDocument.createElement("file"); +			fileElement.appendChild(document.createTextNode(files[i])); +			filesElement.appendChild(fileElement); +		} + +		xhr.open("POST", "/bin/query" + currentDir + "?command=move", true); +		xhr.setRequestHeader("Content-type", "text/xml"); +		xhr.send(xmlDocument); +	}  }  // File info: date, size, type diff --git a/src/webbox.cpp b/src/webbox.cpp index 4575551..8f85332 100644 --- a/src/webbox.cpp +++ b/src/webbox.cpp @@ -290,6 +290,116 @@ int main(int argc, char* argv[]) {  					}  				}  			} +		} else if (command == "delete") { // POST! +			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp)); +			bool ok; +			int contentLength = contentLengthString.toInt(&ok); + +			if (!ok) { +				FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); +				FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out); +			} else { +				QByteArray content(contentLength, 0); +				 +				int result = FCGX_GetStr(content.data(), content.size(), request.in); +				if (result != content.size()) { +					FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); +					FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out); +				} else { +					QXmlStreamReader xml(content); +					 +					QString response = ""; + +					while (!xml.atEnd()) { +						while (xml.readNextStartElement()) { +							if (xml.name() == "files") { +								while (xml.readNextStartElement()) { +									if (xml.name() == "file") { +										QString filename = xml.readElementText(); + +										QFileInfo fileInfo(path + "/" + filename); +										if (fileInfo.isDir()) { +											QDir dir(path); +											if (!dir.rmdir(filename)) { +												response += QString("Error on removing directory %1<br/>").arg(filename); +											} +										} else if (fileInfo.isFile()) { +											QFile file(path + "/" + filename); +											if (!file.remove()) { +												response += QString("Error on removing file %1<br/>").arg(filename); +											} +										} else { +											response += QString("Error: %1 is neither file nor directory.<br/>").arg(filename); +										} +									} +								} +							} +						} +					} + +					if (response == "") { +						response = "OK"; +					} +					 +					FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); +					FCGX_PutS(response.toUtf8().data(), request.out); +				} +			} +		} else if (command == "move") { // POST! +			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp)); +			bool ok; +			int contentLength = contentLengthString.toInt(&ok); + +			if (!ok) { +				FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); +				FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out); +			} else { +				QByteArray content(contentLength, 0); +				 +				int result = FCGX_GetStr(content.data(), content.size(), request.in); +				if (result != content.size()) { +					FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); +					FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out); +				} else { +					QXmlStreamReader xml(content); +					 +					QString response = ""; +					QString targetDir; + +					while (!xml.atEnd()) { +						while (xml.readNextStartElement()) { +							if (xml.name() == "request") { +								while (xml.readNextStartElement()) { +									if (xml.name() == "target") { +										targetDir = xml.readElementText(); +									} else if (xml.name() == "file") { +										QString filename = xml.readElementText(); + +										QFileInfo fileInfo(path + "/" + filename); +										if (fileInfo.isDir()) { +											response += QString("Note: Moving directory %1 not supported.<br/>").arg(filename); +										} else if (fileInfo.isFile()) { +											QFile file(path + "/" + filename); +											if (!file.rename(path + "/" + targetDir + "/" + filename)) { +												response += QString("Error on moving file %1<br/>").arg(filename); +											} +										} else { +											response += QString("Error: %1 is neither file nor directory.<br/>").arg(filename); +										} +									} +								} +							} +						} +					} + +					if (response == "") { +						response = "OK"; +					} +					 +					FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); +					FCGX_PutS(response.toUtf8().data(), request.out); +				} +			}  		} else if (command == "upload") { // POST!  			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp));  			bool ok; | 
