瀏覽代碼

Added wip file downloader in browser

Toby Chui 2 年之前
父節點
當前提交
3e25ed95dd
共有 3 個文件被更改,包括 154 次插入2 次删除
  1. 8 1
      web/Browser/functions/download.js
  2. 14 0
      web/Browser/functions/sizechk.js
  3. 132 1
      web/Browser/index.html

+ 8 - 1
web/Browser/functions/download.js

@@ -6,8 +6,15 @@
     - filepath
     - filename
 */
-requirelib("http")
+requirelib("http");
 
+//Create the download folder if not exists
+requirelib("filelib");
+if (!filelib.fileExists(filepath)){
+    filelib.mkdir(filepath);
+}
+
+//Download the file to target location
 http.download(link, filepath, filename);
 
 HTTP_HEADER = "application/json; charset=utf-8";

+ 14 - 0
web/Browser/functions/sizechk.js

@@ -0,0 +1,14 @@
+/*
+    Check the download size of the current file
+    Require paramter
+
+    filepath (full path of downloading file)
+*/
+
+requirelib("filelib");
+if (!filelib.fileExists(filepath)){
+    sendJSONResp(-1);
+}else{
+    var filesize = filelib.filesize(filepath);
+    sendJSONResp(filesize);    
+}

+ 132 - 1
web/Browser/index.html

@@ -93,6 +93,16 @@
 				background-color: #e2e2e2 !important;
 			}
 
+			#downloadDropper{
+				border: 1px solid grey;
+				width: 100%;
+				height: 200px;
+				cursor: move; /* fallback if grab cursor is unsupported */
+    			cursor: grab;
+				padding: 1em;
+				text-align: center;
+			}
+
 		</style>
 	</head>
 	<body>
@@ -153,7 +163,14 @@
 		  </div>
 		  <div id="downloadManager" class="sidebar" style="display:none;">
 			<div class="ui container">
-				<h1>Download Manger</h1>
+				<div id="downloadDropper" allowdrop="true" ondrop="drop(event)" ondragstart="return false;" onclick="event.preventDefault(); openURLEnter();" ondblclick="event.preventDefault();event.stopImmediatePropagation();" ondragover="allowDrop(event);" draggable="false">
+					<p style="margin-top: 3em;">Drop a link, image into this area; or click this area to enter a URL for download</p>
+				</div>
+			</div>
+			<div class="ui divider"></div>
+			<p>Downloaded Files</p>
+			<div class="ui list" id="downloadList" style="min-height: 300px;">
+				
 			</div>
 		  </div>
 		  <div id="notvdiWarning">
@@ -169,10 +186,124 @@
 			let historyStack = [];
 			let historyPopStack = [];
 			let currentURL = "about:blank";
+			let downloadPendingURL = "newfile.txt";
+			let preferDownloadLocation = "user:/Desktop";
 			let currentTitle = "";
 			let bookmarkBuffer = [];
 			let titleBuffer = {};
 
+			//Drag drop download function
+			function drop(ev) {
+				ev.preventDefault();
+				ev.stopImmediatePropagation();
+				var target = $(ev.target);
+				if (!(target.is("div") || target.is("a"))) {
+					target = $(target).parent();
+				}
+
+				var linkObject = ev.dataTransfer.getData('text/html');
+				window.debug = ev.dataTransfer;
+            	var remoteLink = ($(linkObject).find("a").length>0 || $(linkObject).is("a"))? ($(linkObject).find("a").attr("href")|| $(linkObject).attr("href")):($(linkObject).find("img").length>0?$(linkObject).find('img').attr("src"):($(linkObject).attr('src')!=""?$(linkObject).attr('src'):undefined));
+				console.log(ev.dataTransfer, linkObject, remoteLink);
+				if (remoteLink == undefined){
+					return;
+				}
+
+				downloadToFile(remoteLink);
+			}
+
+			function allowDrop(ev) {
+				ev.preventDefault();
+			}
+
+			function openURLEnter(){
+				let url = prompt("Enter the URL to be downloaded", "http://");
+				url = url.trim();
+				if (url == null || url == undefined || url == "http://" || url == "https://"){
+					return;
+				}
+				downloadToFile(url);
+			}
+
+			function downloadToFile(downloadURL){
+				downloadPendingURL = downloadURL;
+				selectSaveLocation();
+			}
+
+			//save location selector
+			function selectSaveLocation() {
+				var filename = decodeURI(downloadPendingURL.split("/").pop().split("?").shift());
+				option = {
+					defaultName: filename //Default filename used in new operation
+				}
+				ao_module_openFileSelector(fileSelected, preferDownloadLocation, "new", false, option);
+			}
+
+			function updateDownloadProg(elementId){
+				if ($("#" + elementId).attr("status") ==  "downloading"){
+					//This task is still downloading. Get its current size and 
+					//arrange next progress update check
+					ao_module_agirun("Browser/functions/sizechk.js", {
+						filepath: $("#" + elementId).attr('fpath')
+					}, function(data){
+						console.log(data);
+						if (data == -1){
+							$("#" + elementId).find(".downproc").text("[Starting]");
+						}else{
+							$("#" + elementId).find(".downproc").text("[" + ao_module_utils.formatBytes(data, 1) + "]");
+						}
+						
+						setTimeout(function(){
+							updateDownloadProg(elementId);
+						}, 1000);
+					});
+					
+				}else{
+					$("#" + elementId).find(".downproc").text("");
+				}
+			}
+
+			function fileSelected(filedata) {
+				var fileFullpath = filedata[0].filepath;
+				var pathchunk = fileFullpath.split("/");
+				var filename = pathchunk.pop();
+				var filepath = pathchunk.join("/");
+				preferDownloadLocation = filepath;
+				
+				//Append a download pending item to the list
+				var thisDownloadUUID = "download_" + Date.now(); 
+				$("#downloadList").append(`
+					<div class="item downloadTask" id="${thisDownloadUUID}" fpath="${fileFullpath}" status="downloading">
+						<i class="download icon mainicon"></i>
+						<a class="content" onclick="openFileLocation('${filepath}', '${filename}');">
+							${filename} <span class="downproc"></span>
+						</a>
+					</div>
+				`);
+				updateDownloadProg(thisDownloadUUID);
+
+				//Call AGI to download the file
+				ao_module_agirun("Browser/functions/download.js", {
+					link: downloadPendingURL,
+					filename: filename,
+					filepath: filepath
+				}, function(data){
+					console.log(data);
+					$("#" + thisDownloadUUID).find(".mainicon").attr("class", "green checkmark icon mainicon");
+					$("#" + thisDownloadUUID).attr("status", "done");
+				}, function(){
+					//Error callback
+					$("#" + thisDownloadUUID).find(".mainicon").attr("class", "red remove icon mainicon");
+					$("#" + thisDownloadUUID).attr("status", "error");
+				})
+			}
+
+			function openFileLocation(filepath, filename){
+				ao_module_openPath(filepath, filename);
+			}
+
+
+
 			//Check if currently under vdi mode
 			if (ao_module_virtualDesktop == false){
 				$("#notvdiWarning").show();