Эх сурвалжийг харах

Fixed upload path incorrect bug on path travel in file explorer upload function

TC pushbot 5 4 жил өмнө
parent
commit
d51b29477c

BIN
documents/Docs drawing/195142394_968360550605087_7691154838050937170_n.png


+ 18 - 0
web/Blog/backend/helper.js

@@ -0,0 +1,18 @@
+/*
+    Helper Class
+
+    This script handles basic stuffs
+*/
+
+function filepathClean(webroot){
+    if (webroot.substr(webroot.length -1, 1) == "/"){
+        webroot = webroot.substr(0, webroot.length - 1);
+    }
+    return webroot;
+}
+
+function retrunErrorObject(message){
+    sendJSONResp(JSON.stringify({
+        error: message
+    }))
+}

+ 65 - 0
web/Blog/backend/indexManager.js

@@ -0,0 +1,65 @@
+/*
+    Index Manager
+
+    This script handle adding and removing blog post from the index list.
+    Use with include only. Call directly via AGI return nothing
+*/
+
+includes("helper.js");
+
+function getIndexList(webroot){
+    webroot = filepathClean(webroot);
+    var indexFile = webroot + "/blog/posts.json"
+    if (!filelib.fileExists(indexFile)){
+        //Create it
+        filelib.writeFile(indexFile, JSON.stringify([]));
+        //Return an empty list
+        return [];
+    }else{
+        //Get the index file
+        var content = filelib.readFile(indexFile);
+        return JSON.parse(content);
+    }
+}
+
+function writeIndexList(webroot, newListObject){
+    webroot = filepathClean(webroot);
+    var indexFile = webroot + "/blog/posts.json"
+    return filelib.writeFile(indexFile, JSON.stringify(newListObject));
+}
+
+//This inject a new title into the post list
+function injectTitleIntoPostList(webroot, postObjectName){
+    var currentIndexList = getIndexList(webroot);
+    if (currentIndexList.indexOf(postObjectName) > 0){
+        //Already exists in the list. No need to add it in
+
+    }else{
+        //Push to the list
+        currentIndexList.push(postObjectName);
+        writeIndexList(webroot,currentIndexList);
+    }
+}
+
+//This remove a post from the post list base on title
+function removeTitleFromPostList(webroot, postObjectName){
+    var currentIndexList = getIndexList(webroot);
+    if (currentIndexList.indexOf(postObjectName) > 0){
+        //Already removed
+
+    }else{
+        var newarray = [];
+        for (var i = 0; i < currentIndexList.length; i++){
+            var thisIndex = currentIndexList[i];
+            if (thisIndex != postObjectName){
+                newarray.push(thisIndex);
+            }
+        }
+        writeIndexList(webroot, newarray);
+    }
+}
+
+//Get the current post in the system
+function getCurrentPostList(webroot, ){
+
+}

+ 74 - 4
web/Blog/backend/publish.js

@@ -9,17 +9,87 @@
 */
 
 requirelib("filelib");
+requirelib("appdata");
 includes("config.js");
+includes("helper.js");
+includes("indexManager.js");
 
 //Inject content into template
-function inject(template, key, value){
-    template = template.split("{{" + key + "}}").join(value);
-    return template;
+function applyTemplate(templateFilepath, templateObjects){
+    var templateContent = appdata.readFile(templateFilepath);
+    if (templateContent == false){
+        return false;
+    }
+
+    for (var i = 0; i < templateObjects.length; i++){
+        var thisObject = templateObjects[i];
+        templateContent = templateContent.split("{{" + thisObject.key + "}}").join(thisObject.value);
+    }
+
+    return templateContent;
+}
+
+function loadFooter(){
+    var footerTemplate = appdata.readFile("Blog/template/footer.html");
+    if (footerTemplate == false){
+        return "Powered by ArozOS Blog Engine";
+    }else{
+        return footerTemplate;
+    }
 }
 
+function loadBlogInfo(){
+    return ["My Blog", "An amazing blog written by me!"]
+}
+
+
 //Main function to publish the file to webroot
 function publish(){
-    
+    //Check if the blog post object exists
+    if (!filelib.fileExists(filepath)){
+        retrunErrorObject("Given post not exists: " + filepath);
+        return
+    }
+
+    //Get its content. Validate it correctless
+    var content = filelib.readFile(filepath);
+    if (typeof(content) == "undefined" || typeof(content) == "null" || content == ""){
+        retrunErrorObject("Invalid blog post object");
+        return
+    }
+
+    //Get the modification time of the file as blog date
+    var modtime = filelib.mtime(filepath, false);
+    var blogInfo = loadBlogInfo();
+    var blogObject = JSON.parse(content);
+
+    //Apply template
+    var renderedPage = applyTemplate("Blog/template/post.html", [
+        {key: "blog_title", value: blogInfo[0]},
+        {key: "blog_subtitle", value: blogInfo[1]},
+        {key: "post_date", value: modtime},
+        {key: "post_title", value: blogObject.Title},
+        {key: "post_tags", value: blogObject.Tags.join(", ")},
+        {key: "post_content", value: blogObject.Content},
+        {key: "footer", value: loadFooter()}
+    ]);
+
+    //Create the posts folder if not exists
+    if (!filelib.fileExists(webroot + "/blog/posts/")){
+        filelib.mkdir(webroot + "/blog/posts/");
+    }
+
+    //Write out to the user web root
+    webroot = filepathClean(webroot);
+    var postObjectName = filepath.split("/").pop();
+    postObjectName = postObjectName.replace(".json", "");
+
+    var targetPostLocation = webroot + "/blog/posts/" + postObjectName + ".html";
+    filelib.writeFile(targetPostLocation, renderedPage);
+
+    //Update the index file
+    injectTitleIntoPostList(webroot, postObjectName)
+
 }
 
 publish();

+ 14 - 4
web/Blog/index.html

@@ -137,8 +137,18 @@
 				
 			}
 
-			function publishPost(){
-				
+			function publishPostButton(object){
+				var filepath = $(object).attr("filepath");
+				publishPost(filepath);
+			}
+
+			function publishPost(filepath){
+				ao_module_agirun("Blog/backend/publish.js", {
+					filepath: filepath,
+					webroot: currentWebDeployRoot
+				}, function(data){
+					console.log(data);
+				})
 			}
 
 			function openBlog(){
@@ -147,7 +157,7 @@
 						//Get the username of the current session
 						$.get("../system/desktop/user", function(userinfo){
 							//Open the page in new tab
-							window.open("../www/" + userinfo.Username + "/");
+							window.open("../www/" + userinfo.Username + "/blog/");
 						});
 					}else{
 						alert("Personal page not enabled");
@@ -241,7 +251,7 @@
 			
 							<div class="postControls">
 								<button style="${publicPostOnly}" class="ts mini icon button blog red" title="Hide Post"><i class="hide icon"></i></button>
-								<button style="${privatePostOnly}" class="ts mini icon button blog green" title="Publish Post"><i class="upload icon"></i></button>
+								<button onclick="publishPostButton(this);" filepath="${post.filepath}" style="${privatePostOnly}" class="ts mini icon button blog green" title="Publish Post"><i class="upload icon"></i></button>
 							</div>
 						</div>`)
 						})

+ 15 - 0
web/Blog/template/blog.css

@@ -0,0 +1,15 @@
+/*
+    Blog.css
+
+    The default theme for ArozOS Blog Engine
+    Author: tobychui
+*/
+
+@font-face {
+    font-family: taipeiSans;
+    src: url(../../../script/font/TaipeiSansTCBeta-Regular.ttf);
+}
+
+p, span, div, h1, h2, h3, h4, h5, h6, a {
+    font-family: taipeiSans !important;
+}

+ 1 - 0
web/Blog/template/footer.html

@@ -0,0 +1 @@
+Powered by the ArozOS Blog Engine

+ 66 - 0
web/Blog/template/index.html

@@ -0,0 +1,66 @@
+<html>
+	<head>
+		<meta charset="UTF-8">
+		<meta name="apple-mobile-web-app-capable" content="yes" />
+		<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
+		<meta name="theme-color" content="#ff9224">
+		<script src="../../../script/jquery.min.js"></script>
+		<link rel="stylesheet" href="../../../script/semantic/semantic.min.css">
+		<script src="../../../script/semantic/semantic.min.js"></script>
+		<link rel="stylesheet" href="blog.css">
+		<title>托比的神奇蹦蹦部落格</title>
+		<style>
+
+		</style>
+	</head>
+	<body>
+		<div class="ui padded attached black inverted segment">
+			<br><br>
+			<div class="ui container" >
+				<h1 class="ui header" style="color: white;">
+					托比的神奇蹦蹦部落格
+					<div class="sub header" style="color: white;">技術測試用的網站</div>
+				</h1>
+			</div>
+			<br><br>
+		</div>
+		<br>
+		<div class="ui narrow container relaxed stackable grid">
+			<div class="twelve wide column">
+				<!-- Post List -->
+				<div class="ui segment post">
+					<div class="ui blue ribbon label"> 2021-05-30</div>
+					<h2 class="ui header">
+						<a href="" style="color: inherit;">洨洨安群居地</a> 
+						<div class="sub header">
+							<div class="ui breadcrumb">
+								<a class="section">yami</a>
+								<span class="divider">,</span>
+								<a class="section">earlySpring</a>
+								<span class="divider">,</span>
+								<a class="section">teacat</a>
+						  </div>
+						</div>
+					</h2>
+					<p>
+						沒有,2021 年始春還沒有到來…
+					</p>
+				</div>
+				
+			</div>
+			<div class="four wide column">
+				<div class="ui flatted  segment">
+					<h3>Hello World</h3>
+					<p>github.com/abc</p>
+				</div>
+			</div>
+		</div>
+		
+		<!-- footer -->
+		<div class="ui container">
+			<div class="ui divider"></div>
+			Template CopyRight ArozOS v1.114
+		</div>
+		
+	</body>
+</html>

+ 48 - 0
web/Blog/template/post.html

@@ -0,0 +1,48 @@
+<html>
+	<head>
+		<meta charset="UTF-8">
+		<meta name="apple-mobile-web-app-capable" content="yes" />
+		<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
+		<meta name="theme-color" content="#ff9224">
+		<script src="../../../../script/jquery.min.js"></script>
+		<link rel="stylesheet" href="../../../../script/semantic/semantic.min.css">
+		<script src="../../../../script/semantic/semantic.min.js"></script>
+		<link rel="stylesheet" href="blog.css">
+		<title>{{blog_title}}</title>
+		<style>
+
+		</style>
+	</head>
+	<body>
+		<div class="ui padded attached black inverted segment">
+			<br><br>
+			<a href="../index.html" class="ui container" >
+				<h1 class="ui header" style="color: white;">
+					{{blog_title}}
+					<div class="sub header" style="color: white;">{{blog_subtitle}}</div>
+				</h1>
+			</a>
+			<br><br>
+		</div>
+		<br>
+		<div class="ui container">
+			<div class="ui segment">
+				<div class="ui blue ribbon label">{{post_date}}</div>
+				<h1>{{post_title}}</h1>
+				<p>{{post_tags}}</p>
+				<div class="ui divider"></div>
+				{{post_content}}
+
+				<div class="ui divider"></div>
+				<a href="index.html"><i class="angle left icon"></i> Back</a>
+			</div>
+		</div>
+		
+		<!-- footer -->
+		<div class="ui container">
+			<div class="ui divider"></div>
+			{{footer}}
+		</div>
+		
+	</body>
+</html>

+ 1 - 1
web/SystemAO/file_system/file_explorer.html

@@ -4300,11 +4300,11 @@
 
                 //Prase upload Form
                 let uploadCurrentPath = JSON.parse(JSON.stringify(currentPath));
-                let url = '../../system/file_system/upload?path=' + uploadCurrentPath
                 if (targetDir !== undefined){
                     //The upload paramter supplied targetDir
                     uploadCurrentPath = targetDir;
                 }
+                let url = '../../system/file_system/upload?path=' + uploadCurrentPath
                 //console.log("Uploading To => ", uploadCurrentPath, targetDir, currentPath);
                 let formData = new FormData()
                 let xhr = new XMLHttpRequest()