Browse Source

Update Manga Cafe to load nature sorted manga fiels

Toby Chui 3 years ago
parent
commit
7a19ecbc82

+ 1 - 0
AGI Documentation.md

@@ -455,6 +455,7 @@ For glob and aglob, developer can pass in the following sorting modes (case sens
 - largeToSmall
 - mostRecent
 - leastRecent
+- smart (Added in v1.119, AGI only, for sorting filename containing digits with no zero pads)
 
 ```
 //Example for sorting the desktop files to largeToSmall

+ 1 - 1
agi.go

@@ -17,7 +17,7 @@ func AGIInit() {
 		BuildVersion:         build_version,
 		InternalVersion:      internal_version,
 		LoadedModule:         moduleHandler.GetModuleNameList(),
-		ReservedTables:       []string{"auth", "permisson", "desktop"},
+		ReservedTables:       []string{"auth", "permisson", "register", "desktop"},
 		ModuleRegisterParser: moduleHandler.RegisterModuleFromAGI,
 		PackageManager:       packageManager,
 		UserHandler:          userHandler,

+ 3 - 1
mod/filesystem/fssort/fssort.go

@@ -58,6 +58,8 @@ func SortFileList(filelistRealpath []string, sortMode string) []string {
 		sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime > parsedFilelist[j].ModTime })
 	} else if sortMode == "leastRecent" {
 		sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime < parsedFilelist[j].ModTime })
+	} else if sortMode == "smart" {
+		parsedFilelist = SortNaturalFilelist(parsedFilelist)
 	}
 
 	results := []string{}
@@ -69,7 +71,7 @@ func SortFileList(filelistRealpath []string, sortMode string) []string {
 }
 
 func SortModeIsSupported(sortMode string) bool {
-	if !contains(sortMode, []string{"default", "reverse", "smallToLarge", "largeToSmall", "mostRecent", "leastRecent"}) {
+	if !contains(sortMode, []string{"default", "reverse", "smallToLarge", "largeToSmall", "mostRecent", "leastRecent", "smart"}) {
 		return false
 	}
 	return true

+ 59 - 0
mod/filesystem/fssort/smartsort.go

@@ -0,0 +1,59 @@
+package fssort
+
+/*
+	Smart Sort
+
+	Sort file based on natural string sorting logic, design to sort filename
+	that contains digit but without zero paddings
+*/
+
+import (
+	"fmt"
+	"regexp"
+	"sort"
+	"strconv"
+	"strings"
+)
+
+func SortNaturalFilelist(filelist []*sortBufferedStructure) []*sortBufferedStructure {
+	filenameList := []string{}
+	filenameMap := map[string]*sortBufferedStructure{}
+	for _, file := range filelist {
+		filenameList = append(filenameList, file.Filename)
+		filenameMap[file.Filename] = file
+	}
+
+	sortedFilenameList := sortNaturalStrings(filenameList)
+	sortedFileList := []*sortBufferedStructure{}
+	for _, thisFilename := range sortedFilenameList {
+		sortedFileList = append(sortedFileList, filenameMap[thisFilename])
+	}
+
+	return sortedFileList
+}
+
+func sortNaturalStrings(array []string) []string {
+	bufKey := []string{}
+	bufMap := map[string]string{}
+	for _, thisString := range array {
+		re := regexp.MustCompile("[0-9]+")
+		matchings := re.FindAllString(thisString, -1)
+		cs := thisString
+		for _, matchPoint := range matchings {
+			mpi, _ := strconv.Atoi(matchPoint)
+			replacement := fmt.Sprintf("%018d", mpi)
+			cs = strings.ReplaceAll(cs, matchPoint, replacement)
+		}
+
+		bufKey = append(bufKey, cs)
+		bufMap[cs] = thisString
+	}
+
+	sort.Strings(bufKey)
+
+	result := []string{}
+	for _, key := range bufKey {
+		result = append(result, bufMap[key])
+	}
+	return result
+}

+ 2 - 2
web/Manga Cafe/backend/getMangaInfo.js

@@ -15,7 +15,7 @@ var parentFolder = tmp.join("/");
 var title = [tmp.pop(), chapterName];
 
 //Scan the manga content, process the image if nessary
-var pages = filelib.aglob(targetFolder + "*");
+var pages = filelib.aglob(targetFolder + "*", "smart");
 var validPages = [];
 for (var i = 0; i < pages.length; i++){
     var thisPage = pages[i];
@@ -59,7 +59,7 @@ for (var i = 0; i < pages.length; i++){
 }
 
 //Search for other chapter links
-var otherChapterCandidate = filelib.aglob(parentFolder + "/*");
+var otherChapterCandidate = filelib.aglob(parentFolder + "/*", "smart");
 var otherChapters = [];
 for (var i =0; i < otherChapterCandidate.length; i++){
     var basename = otherChapterCandidate[i].split('/').pop();

+ 3 - 3
web/Manga Cafe/backend/listTitles.js

@@ -22,13 +22,13 @@ var scannedTitles = [];
 for (var i =0; i < rootList.length; i++){
     var thisRoot = rootList[i];
     if (filelib.fileExists(thisRoot + "Photo/Manga")){
-        var titleList = filelib.aglob(thisRoot + "Photo/Manga/*");
+        var titleList = filelib.aglob(thisRoot + "Photo/Manga/*", "smart");
         for (var k =0; k < titleList.length; k++){
             var thisFileObject = titleList[k];
             //Only scan this if this is a directory and it is not start with "."
             if (filelib.isDir(thisFileObject) && thisFileObject.split("/").pop().substr(0, 1) != "."){
                 //This should be manga title. Get its chapter count
-                var chaptersInThisTitle = filelib.aglob(thisFileObject + "/*");
+                var chaptersInThisTitle = filelib.aglob(thisFileObject + "/*", "smart");
                 var foldersInTitle = [];
                 var chapterCount = 0;
                 for (var j = 0; j < chaptersInThisTitle.length; j++){
@@ -46,7 +46,7 @@ for (var i =0; i < rootList.length; i++){
                 }else{
                     //Get the first image from the first chapter
                     var firstChapterFolder = foldersInTitle[0];
-                    var firstChapterImagaes = filelib.aglob(firstChapterFolder + "/*.jpg");
+                    var firstChapterImagaes = filelib.aglob(firstChapterFolder + "/*.jpg", "smart");
                     
                     //Get the first image that is not horizontal
                     titleImagePath = firstChapterImagaes[0];

+ 0 - 20
web/Memo/backend/dblist.js

@@ -1,20 +0,0 @@
-
-onsole.log("Testing Database Listing API");
-if (newDBTableIfNotExists("testdb")){
-	writeDBItem("testdb","One","Hello World")
-	writeDBItem("testdb","Two","This is a text message")
-	writeDBItem("testdb","Three","For listing")
-	writeDBItem("testdb","Four","123456")
-	writeDBItem("testdb","Five","You can also put JSON string here")
-	
-	//Try to list db table
-	var entries = listDBTable("testdb");
-	sendJSONResp(JSON.stringify(entries));
-	//Drop the table after testing
-	dropDBTable("testdb");
-	console.log("Testdb table dropped");
-
-	
-}else{
-	sendResp("Failed creating new db");
-}

+ 0 - 18
web/Memo/backend/dbtest.js

@@ -1,18 +0,0 @@
-console.log("Testing Database API");
-if (newDBTableIfNotExists("testdb")){
-	if (writeDBItem("testdb","message","Hello World")){
-		//Test suceed. Set Response message to the message
-		sendResp("Database access return value: " + readDBItem("testdb","message"));
-		//Delete the entry
-		
-		console.log("Delete entry:" + deleteDBItem("testdb","message"))
-		//Drop the table after testing
-		dropDBTable("testdb");
-		console.log("Testdb table dropped");
-	}else{
-		sendResp("Failed to write to db");
-	}
-	
-}else{
-	sendResp("Failed creating new db");
-}

+ 21 - 25
web/desktop.system

@@ -6235,24 +6235,9 @@
 
         var screenshotBuf = [];
         var totalFloatWindowsLeft = 0;
-        var baseCanvas = null;
         function screenshot(){
             totalFloatWindowsLeft = $(".floatWindow").length;
-            baseCanvas = null;
             screenshotBuf = [];
-            //Get the base canvas
-            
-            html2canvas(document.querySelector("body")).then(basecanvas => {
-                //Set the base canvas
-                baseCanvas = basecanvas;
-
-                //Check if the other iframe screenshot done
-                if (totalFloatWindowsLeft == 0){
-                    mergeScreenshotCanvas();
-                }
-            });
-            
-
             //Get a screenshot from each floatWindow
             $(".floatWindow").each(function(){
                 let contentWindow = $(this).find("iframe")[0].contentWindow;
@@ -6263,7 +6248,7 @@
                         screenshotBuf.push([thisWindowId,offsets,capture]);
                         totalFloatWindowsLeft--;
 
-                        if (totalFloatWindowsLeft == 0 && baseCanvas != null){
+                        if (totalFloatWindowsLeft == 0){
                             //Merge the canvas
                             mergeScreenshotCanvas();
                         }
@@ -6277,23 +6262,34 @@
 
         function mergeScreenshotCanvas(){
             //Merge the screenshots
-            var baseCtx=baseCanvas.getContext('2d');
             for (var i = 0; i < screenshotBuf.length; i++){
                 var thisWindowID = screenshotBuf[i][0];
                 var thisOffsets = screenshotBuf[i][1];
                 var thisCanvas = screenshotBuf[i][2];
-                var thisCtx = thisCanvas.getContext('2d');
+                var imgLink = thisCanvas.toDataURL();
 
-                //Draw thisCtx onto baseCtx at offsets thisOffsets
-                baseCtx.drawImage(thisCanvas, thisOffsets[0], thisOffsets[1]);
+                //Overlay the iframe with image of screenshot
+                let targetFw = getFloatWindowByID(thisWindowID);
+                $(targetFw).find(".iframecover").append(`<img src="${imgLink}"/>`);
+                $(targetFw).find(".iframecover").show();
             }
 
-            downloadCanvas(baseCanvas);
+            html2canvas(document.querySelector("body")).then(basecanvas => {
+                downloadCanvas(basecanvas);
 
-            //Reset paramters
-            totalFloatWindowsLeft = 0;
-            baseCanvas = null;
-            screenshotBuf = [];
+                //Clear up the mess
+                screenshotBuf.forEach(function(entry){
+                    let targetFw = getFloatWindowByID(entry[0]);
+                    $(targetFw).find(".iframecover").html("");
+                    $(targetFw).find(".iframecover").hide();
+                });
+
+                 //Reset paramters
+                totalFloatWindowsLeft = 0;
+                screenshotBuf = [];
+            });
+
+           
         }