Explorar el Código

Update agi image

Toby Chui hace 2 años
padre
commit
241a4ae927

BIN
documents/publication/arozos_banner.png


BIN
documents/publication/arozos_banner.psd


BIN
documents/publication/card.png


BIN
documents/publication/card.psd


BIN
documents/publication/card2.png


BIN
documents/publication/card2.psd


+ 52 - 13
mod/agi/agi.image.go

@@ -19,6 +19,7 @@ import (
 	"github.com/robertkrimen/otto"
 
 	"imuslab.com/arozos/mod/filesystem"
+	"imuslab.com/arozos/mod/filesystem/arozfs"
 	"imuslab.com/arozos/mod/neuralnet"
 	user "imuslab.com/arozos/mod/user"
 	"imuslab.com/arozos/mod/utils"
@@ -60,6 +61,7 @@ func (g *Gateway) injectImageLibFunctions(vm *otto.Otto, u *user.User, scriptFsh
 
 		openingPath := imagePath
 		var closerFunc func()
+		var file arozfs.File
 		if fsh.RequireBuffer {
 			bufferPath, cf := g.getUserSpecificTempFilePath(u, imagePath)
 			closerFunc = cf
@@ -71,12 +73,18 @@ func (g *Gateway) injectImageLibFunctions(vm *otto.Otto, u *user.User, scriptFsh
 			}
 			os.WriteFile(bufferPath, c, 0775)
 			openingPath = bufferPath
-		}
 
-		file, err := os.Open(openingPath)
-		if err != nil {
-			g.raiseError(err)
-			return otto.FalseValue()
+			file, err = os.Open(openingPath)
+			if err != nil {
+				g.raiseError(err)
+				return otto.FalseValue()
+			}
+		} else {
+			file, err = fsh.FileSystemAbstraction.Open(openingPath)
+			if err != nil {
+				g.raiseError(err)
+				return otto.FalseValue()
+			}
 		}
 
 		image, _, err := image.DecodeConfig(file)
@@ -144,35 +152,66 @@ func (g *Gateway) injectImageLibFunctions(vm *otto.Otto, u *user.User, scriptFsh
 
 		resizeOpeningFile := rsrc
 		resizeWritingFile := rdest
-		var srcCloser func()
-		var destCloser func()
+		var srcFile arozfs.File
+		var destFile arozfs.File
 		if srcfsh.RequireBuffer {
-			resizeOpeningFile, srcCloser, err = g.bufferRemoteResourcesToLocal(srcfsh, u, rsrc)
+			resizeOpeningFile, _, err = g.bufferRemoteResourcesToLocal(srcfsh, u, rsrc)
+			if err != nil {
+				g.raiseError(err)
+				return otto.FalseValue()
+			}
+
+			srcFile, err = os.Open(resizeOpeningFile)
+			if err != nil {
+				g.raiseError(err)
+				return otto.FalseValue()
+			}
+		} else {
+			srcFile, err = srcfsh.FileSystemAbstraction.Open(resizeOpeningFile)
 			if err != nil {
 				g.raiseError(err)
 				return otto.FalseValue()
 			}
-			defer srcCloser()
 		}
+		defer srcFile.Close()
 
 		if destfsh.RequireBuffer {
-			resizeWritingFile, destCloser, err = g.bufferRemoteResourcesToLocal(destfsh, u, rdest)
+			resizeWritingFile, _, err = g.bufferRemoteResourcesToLocal(destfsh, u, rdest)
+			if err != nil {
+				g.raiseError(err)
+				return otto.FalseValue()
+			}
+
+			destFile, err = os.Open(resizeWritingFile)
+			if err != nil {
+				g.raiseError(err)
+				return otto.FalseValue()
+			}
+		} else {
+			destFile, err = destfsh.FileSystemAbstraction.Open(resizeOpeningFile)
 			if err != nil {
 				g.raiseError(err)
 				return otto.FalseValue()
 			}
-			defer destCloser()
 		}
+		defer destFile.Close()
 
 		//Resize the image
-		src, err := imaging.Open(resizeOpeningFile)
+		//src, err := imaging.Open(resizeOpeningFile)
+		src, err := imaging.Decode(srcFile)
 		if err != nil {
 			//Opening failed
 			g.raiseError(err)
 			return otto.FalseValue()
 		}
 		src = imaging.Resize(src, int(width), int(height), imaging.Lanczos)
-		err = imaging.Save(src, resizeWritingFile)
+		//err = imaging.Save(src, resizeWritingFile)
+		f, err := imaging.FormatFromFilename(resizeWritingFile)
+		if err != nil {
+			g.raiseError(err)
+			return otto.FalseValue()
+		}
+		err = imaging.Encode(destFile, src, f)
 		if err != nil {
 			g.raiseError(err)
 			return otto.FalseValue()

+ 172 - 169
mod/network/ssdp/ssdp.go

@@ -1,169 +1,172 @@
-package ssdp
-
-import (
-	"errors"
-	"io/ioutil"
-	"log"
-	"net"
-	"net/http"
-	"os/exec"
-	"runtime"
-	"strconv"
-	"strings"
-	"time"
-
-	ssdp "github.com/koron/go-ssdp"
-	"github.com/valyala/fasttemplate"
-)
-
-type SSDPOption struct {
-	URLBase   string
-	Hostname  string
-	Vendor    string
-	VendorURL string
-	ModelName string
-	ModelDesc string
-	Serial    string
-	UUID      string
-}
-
-type SSDPHost struct {
-	ADV              *ssdp.Advertiser
-	advStarted       bool
-	SSDPTemplateFile string
-	Option           *SSDPOption
-	quit             chan bool
-}
-
-func NewSSDPHost(outboundIP string, port int, templateFile string, option SSDPOption) (*SSDPHost, error) {
-	if runtime.GOOS == "linux" {
-		//In case there are more than 1 network interface connect to the same LAN, choose the first one by default
-		interfaceName, err := getFirstNetworkInterfaceName()
-		if err != nil {
-			//Ignore the interface binding
-			log.Println("[WARN] No connected network interface. Starting SSDP anyway.")
-		} else {
-			mainNIC, err := net.InterfaceByName(interfaceName)
-			if err != nil {
-				log.Println("[WARN] Unable to get interface by name: " + interfaceName + ". Starting SSDP on all IPv4 interfaces.")
-			} else {
-				ssdp.Interfaces = []net.Interface{*mainNIC}
-			}
-
-		}
-	}
-
-	ad, err := ssdp.Advertise(
-		"upnp:rootdevice",   // send as "ST"
-		"uuid:"+option.UUID, // send as "USN"
-		"http://"+outboundIP+":"+strconv.Itoa(port)+"/ssdp.xml", // send as "LOCATION"
-		"arozos/"+outboundIP, // send as "SERVER"
-		30)                   // send as "maxAge" in "CACHE-CONTROL"
-	if err != nil {
-		return &SSDPHost{}, err
-	}
-
-	return &SSDPHost{
-		ADV:              ad,
-		advStarted:       false,
-		SSDPTemplateFile: templateFile,
-		Option:           &option,
-	}, nil
-}
-
-func (a *SSDPHost) Start() {
-	//Advertise ssdp
-	http.HandleFunc("/ssdp.xml", a.handleSSDP)
-	log.Println("Starting SSDP Discovery Service: " + a.Option.URLBase)
-	var aliveTick <-chan time.Time
-	aliveTick = time.Tick(time.Duration(5) * time.Second)
-
-	quit := make(chan bool)
-	a.quit = quit
-	a.advStarted = true
-	go func(ad *ssdp.Advertiser) {
-		for {
-			select {
-			case <-aliveTick:
-				ad.Alive()
-			case <-quit:
-				ad.Bye()
-				ad.Close()
-				break
-			}
-		}
-	}(a.ADV)
-}
-
-func (a *SSDPHost) Close() {
-	if a != nil {
-		if a.advStarted {
-			a.quit <- true
-		}
-	}
-
-}
-
-//Serve the xml file with the given properties
-func (a *SSDPHost) handleSSDP(w http.ResponseWriter, r *http.Request) {
-	//Load the ssdp xml from file
-	template, err := ioutil.ReadFile(a.SSDPTemplateFile)
-	if err != nil {
-		w.Write([]byte("SSDP.XML NOT FOUND"))
-		return
-	}
-
-	t := fasttemplate.New(string(template), "{{", "}}")
-	s := t.ExecuteString(map[string]interface{}{
-		"urlbase":   a.Option.URLBase,
-		"hostname":  a.Option.Hostname,
-		"vendor":    a.Option.Vendor,
-		"vendorurl": a.Option.VendorURL,
-		"modeldesc": a.Option.ModelDesc,
-		"modelname": a.Option.ModelName,
-		"uuid":      a.Option.UUID,
-		"serial":    a.Option.Serial,
-	})
-
-	w.Write([]byte(s))
-}
-
-//Helper functions
-func getFirstNetworkInterfaceName() (string, error) {
-	if runtime.GOOS == "linux" {
-		if pkg_exists("ip") {
-			//Use the fast method
-			cmd := exec.Command("bash", "-c", `ip route | grep default | sed -e "s/^.*dev.//" -e "s/.proto.*//"`)
-			out, _ := cmd.CombinedOutput()
-			if strings.TrimSpace(string(out)) == "" {
-				//No interface found.
-				return "", errors.New("No interface found")
-			} else {
-				return strings.Split(strings.TrimSpace(string(out)), "\n")[0], nil
-			}
-		} else if pkg_exists("ifconfig") {
-			//Guess it from ifconfig list
-			cmd := exec.Command("bash", "-c", `ifconfig -a | sed -E 's/[[:space:]:].*//;/^$/d'`)
-			out, _ := cmd.CombinedOutput()
-			if strings.TrimSpace(string(out)) == "" {
-				//No interface found.
-				return "", errors.New("No interface found")
-			} else {
-				return strings.Split(strings.TrimSpace(string(out)), "\n")[0], nil
-			}
-		}
-	}
-
-	return "", errors.New("Not supported platform or missing package")
-}
-
-func pkg_exists(pkgname string) bool {
-	cmd := exec.Command("which", pkgname)
-	out, _ := cmd.CombinedOutput()
-
-	if len(string(out)) > 1 {
-		return true
-	} else {
-		return false
-	}
-}
+package ssdp
+
+import (
+	"errors"
+	"io/ioutil"
+	"log"
+	"net"
+	"net/http"
+	"os/exec"
+	"runtime"
+	"strconv"
+	"strings"
+	"time"
+
+	ssdp "github.com/koron/go-ssdp"
+	"github.com/valyala/fasttemplate"
+)
+
+type SSDPOption struct {
+	URLBase   string
+	Hostname  string
+	Vendor    string
+	VendorURL string
+	ModelName string
+	ModelDesc string
+	Serial    string
+	UUID      string
+}
+
+type SSDPHost struct {
+	ADV              *ssdp.Advertiser
+	advStarted       bool
+	SSDPTemplateFile string
+	Option           *SSDPOption
+	quit             chan bool
+}
+
+func NewSSDPHost(outboundIP string, port int, templateFile string, option SSDPOption) (*SSDPHost, error) {
+	if runtime.GOOS == "linux" {
+		//In case there are more than 1 network interface connect to the same LAN, choose the first one by default
+		interfaceName, err := getFirstNetworkInterfaceName()
+		if err != nil {
+			//Ignore the interface binding
+			log.Println("[WARN] No connected network interface. Starting SSDP anyway.")
+		} else {
+			mainNIC, err := net.InterfaceByName(interfaceName)
+			if err != nil {
+				log.Println("[WARN] Unable to get interface by name: " + interfaceName + ". Starting SSDP on all IPv4 interfaces.")
+			} else {
+				ssdp.Interfaces = []net.Interface{*mainNIC}
+			}
+
+		}
+	}
+
+	ad, err := ssdp.Advertise(
+		"upnp:rootdevice",   // send as "ST"
+		"uuid:"+option.UUID, // send as "USN"
+		"http://"+outboundIP+":"+strconv.Itoa(port)+"/ssdp.xml", // send as "LOCATION"
+		"arozos/"+outboundIP, // send as "SERVER"
+		30)                   // send as "maxAge" in "CACHE-CONTROL"
+	if err != nil {
+		return &SSDPHost{}, err
+	}
+
+	return &SSDPHost{
+		ADV:              ad,
+		advStarted:       false,
+		SSDPTemplateFile: templateFile,
+		Option:           &option,
+	}, nil
+}
+
+func (a *SSDPHost) Start() {
+	//Advertise ssdp
+	http.HandleFunc("/ssdp.xml", a.handleSSDP)
+	log.Println("Starting SSDP Discovery Service: " + a.Option.URLBase)
+	var aliveTick <-chan time.Time
+	aliveTick = time.Tick(time.Duration(5) * time.Second)
+
+	quit := make(chan bool)
+	a.quit = quit
+	a.advStarted = true
+	go func(ad *ssdp.Advertiser) {
+		for {
+			select {
+			case <-aliveTick:
+				if ad != nil {
+					ad.Alive()
+				}
+
+			case <-quit:
+				ad.Bye()
+				ad.Close()
+				break
+			}
+		}
+	}(a.ADV)
+}
+
+func (a *SSDPHost) Close() {
+	if a != nil {
+		if a.advStarted {
+			a.quit <- true
+		}
+	}
+
+}
+
+//Serve the xml file with the given properties
+func (a *SSDPHost) handleSSDP(w http.ResponseWriter, r *http.Request) {
+	//Load the ssdp xml from file
+	template, err := ioutil.ReadFile(a.SSDPTemplateFile)
+	if err != nil {
+		w.Write([]byte("SSDP.XML NOT FOUND"))
+		return
+	}
+
+	t := fasttemplate.New(string(template), "{{", "}}")
+	s := t.ExecuteString(map[string]interface{}{
+		"urlbase":   a.Option.URLBase,
+		"hostname":  a.Option.Hostname,
+		"vendor":    a.Option.Vendor,
+		"vendorurl": a.Option.VendorURL,
+		"modeldesc": a.Option.ModelDesc,
+		"modelname": a.Option.ModelName,
+		"uuid":      a.Option.UUID,
+		"serial":    a.Option.Serial,
+	})
+
+	w.Write([]byte(s))
+}
+
+//Helper functions
+func getFirstNetworkInterfaceName() (string, error) {
+	if runtime.GOOS == "linux" {
+		if pkg_exists("ip") {
+			//Use the fast method
+			cmd := exec.Command("bash", "-c", `ip route | grep default | sed -e "s/^.*dev.//" -e "s/.proto.*//"`)
+			out, _ := cmd.CombinedOutput()
+			if strings.TrimSpace(string(out)) == "" {
+				//No interface found.
+				return "", errors.New("No interface found")
+			} else {
+				return strings.Split(strings.TrimSpace(string(out)), "\n")[0], nil
+			}
+		} else if pkg_exists("ifconfig") {
+			//Guess it from ifconfig list
+			cmd := exec.Command("bash", "-c", `ifconfig -a | sed -E 's/[[:space:]:].*//;/^$/d'`)
+			out, _ := cmd.CombinedOutput()
+			if strings.TrimSpace(string(out)) == "" {
+				//No interface found.
+				return "", errors.New("No interface found")
+			} else {
+				return strings.Split(strings.TrimSpace(string(out)), "\n")[0], nil
+			}
+		}
+	}
+
+	return "", errors.New("Not supported platform or missing package")
+}
+
+func pkg_exists(pkgname string) bool {
+	cmd := exec.Command("which", pkgname)
+	out, _ := cmd.CombinedOutput()
+
+	if len(string(out)) > 1 {
+		return true
+	} else {
+		return false
+	}
+}

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

@@ -1,73 +1,73 @@
-/*
-    Manga Cafe scan mangas
-
-    This script will scan all vroots with Picture/Manga/ directories
-*/
-
-//Require filelib
-requirelib("filelib");
-
-//Require imagelib
-requirelib("imagelib");
-
-//Make Manga folder if not exists
-if (!filelib.fileExists("user:/Photo/Manga")){
-    filelib.mkdir("user:/Photo/Manga")
-}
-
-
-//Scan all roots for other manga
-var rootList = filelib.glob("/")
-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/*", "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 + "/*", "smart");
-                var foldersInTitle = [];
-                var chapterCount = 0;
-                for (var j = 0; j < chaptersInThisTitle.length; j++){
-                    var basename = chaptersInThisTitle[j].split("/").pop();
-                    if (filelib.isDir(chaptersInThisTitle[j]) && basename.substring(0,1) != "."){
-                        chapterCount++;
-                        foldersInTitle.push(chaptersInThisTitle[j]);
-                    }
-                }
-
-                //Check if title image exists. If not, use ch1 image 1
-                var titleImagePath = ""
-                if (filelib.fileExists(thisFileObject + "/title.png")){
-                    titleImagePath = thisFileObject + "/title.png"
-                }else{
-                    //Get the first image from the first chapter
-                    var firstChapterFolder = foldersInTitle[0];
-                    var firstChapterImagaes = filelib.aglob(firstChapterFolder + "/*.jpg", "smart");
-                    
-                    //Get the first image that is not horizontal
-                    titleImagePath = firstChapterImagaes[0];
-                    var index = 0;
-                    var size = imagelib.getImageDimension(titleImagePath);
-                    while(size[0] > size[1] && index < firstChapterImagaes.length - 1){
-                        //Not this one. Next image
-                        index++;
-                        titleImagePath = firstChapterImagaes[index]; 
-                        size = imagelib.getImageDimension(titleImagePath);
-                    }
-                }
-
-                //Get the starting chapter
-                var startChapter = foldersInTitle[0];
-
-                //Prase the return output, src folder, chapter count and title image path
-                scannedTitles.push([thisFileObject, chapterCount, titleImagePath, startChapter]);
-            }
-        }
-    }
-}
-
+/*
+    Manga Cafe scan mangas
+
+    This script will scan all vroots with Picture/Manga/ directories
+*/
+
+//Require filelib
+requirelib("filelib");
+
+//Require imagelib
+requirelib("imagelib");
+
+//Make Manga folder if not exists
+if (!filelib.fileExists("user:/Photo/Manga")){
+    filelib.mkdir("user:/Photo/Manga")
+}
+
+
+//Scan all roots for other manga
+var rootList = filelib.glob("/")
+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/*", "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 + "/*", "smart");
+                var foldersInTitle = [];
+                var chapterCount = 0;
+                for (var j = 0; j < chaptersInThisTitle.length; j++){
+                    var basename = chaptersInThisTitle[j].split("/").pop();
+                    if (filelib.isDir(chaptersInThisTitle[j]) && basename.substring(0,1) != "."){
+                        chapterCount++;
+                        foldersInTitle.push(chaptersInThisTitle[j]);
+                    }
+                }
+
+                //Check if title image exists. If not, use ch1 image 1
+                var titleImagePath = ""
+                if (filelib.fileExists(thisFileObject + "/title.png")){
+                    titleImagePath = thisFileObject + "/title.png"
+                }else{
+                    //Get the first image from the first chapter
+                    var firstChapterFolder = foldersInTitle[0];
+                    var firstChapterImagaes = filelib.aglob(firstChapterFolder + "/*.jpg", "smart");
+                    
+                    //Get the first image that is not horizontal
+                    titleImagePath = firstChapterImagaes[0];
+                    var index = 0;
+                    var size = imagelib.getImageDimension(titleImagePath);
+                    while(size[0] > size[1] && index < firstChapterImagaes.length - 1){
+                        //Not this one. Next image
+                        index++;
+                        titleImagePath = firstChapterImagaes[index]; 
+                        size = imagelib.getImageDimension(titleImagePath);
+                    }
+                }
+
+                //Get the starting chapter
+                var startChapter = foldersInTitle[0];
+
+                //Prase the return output, src folder, chapter count and title image path
+                scannedTitles.push([thisFileObject, chapterCount, titleImagePath, startChapter]);
+            }
+        }
+    }
+}
+
 sendJSONResp(JSON.stringify(scannedTitles));