Parcourir la source

Split disk capacity into two module to handle better abstraction design

TC pushbot 5 il y a 4 ans
Parent
commit
59885828da

+ 99 - 0
mod/disk/diskcapacity/dftool/dftool.go

@@ -0,0 +1,99 @@
+package dftool
+
+import (
+	"errors"
+	"log"
+	"os/exec"
+	"path/filepath"
+	"runtime"
+	"strconv"
+	"strings"
+
+	"imuslab.com/arozos/mod/disk/diskspace"
+)
+
+type Capacity struct {
+	PhysicalDevice    string //The ID of the physical device, like C:/ or /dev/sda1
+	MountingHierarchy string //The Mounting Hierarchy of the vroot
+	Used              int64  //Used capacity in bytes
+	Avilable          int64  //Avilable capacity in bytes
+	Total             int64  //Total capacity in bytes
+}
+
+func GetCapacityInfoFromPath(realpath string) (*Capacity, error) {
+	rpathAbs, err := filepath.Abs(realpath)
+	if err != nil {
+		return nil, err
+	}
+
+	if runtime.GOOS == "windows" {
+		//Windows
+		//Extract disk ID from path
+		rpathAbs = filepath.ToSlash(filepath.Clean(rpathAbs))
+		diskRoot := strings.Split(rpathAbs, "/")[0]
+
+		//Match the disk space info generated from diskspace
+		logicDiskInfo := diskspace.GetAllLogicDiskInfo()
+		for _, ldi := range logicDiskInfo {
+			if strings.TrimSpace(ldi.Device) == strings.TrimSpace(diskRoot) {
+				//Matching device ID
+				return &Capacity{
+					PhysicalDevice: ldi.Device,
+					Used:           ldi.Used,
+					Avilable:       ldi.Available,
+					Total:          ldi.Volume,
+				}, nil
+			}
+		}
+
+	} else {
+		//Assume Linux or Mac
+		//Use command: df -P {abs_path}
+		cmd := exec.Command("df", "-P", rpathAbs)
+		log.Println("df", "-P", rpathAbs)
+		out, err := cmd.CombinedOutput()
+		if err != nil {
+			return nil, err
+		}
+
+		//Get the last line of the output
+		diskInfo := strings.TrimSpace(string(out))
+		tmp := strings.Split(diskInfo, "\n")
+		targetDiskInfo := strings.Join(tmp[len(tmp)-1:], " ")
+		for strings.Contains(targetDiskInfo, "  ") {
+			targetDiskInfo = strings.ReplaceAll(targetDiskInfo, "  ", " ")
+		}
+
+		diskInfoSlice := strings.Split(targetDiskInfo, " ")
+
+		if len(diskInfoSlice) < 4 {
+			return nil, errors.New("Malformed output for df -P")
+		}
+
+		//Extract capacity information from df output
+		total, err := strconv.ParseInt(diskInfoSlice[1], 10, 64)
+		if err != nil {
+			return nil, errors.New("Malformed output for df -P")
+		}
+
+		used, err := strconv.ParseInt(diskInfoSlice[2], 10, 64)
+		if err != nil {
+			return nil, errors.New("Malformed output for df -P")
+		}
+
+		availbe, err := strconv.ParseInt(diskInfoSlice[3], 10, 64)
+		if err != nil {
+			return nil, errors.New("Malformed output for df -P")
+		}
+
+		//Return the capacity info struct, capacity is reported in 1024 bytes block
+		return &Capacity{
+			PhysicalDevice: diskInfoSlice[0],
+			Used:           used * 1024,
+			Avilable:       availbe * 1024,
+			Total:          total * 1024,
+		}, nil
+	}
+
+	return nil, errors.New("Unable to resolve matching disk capacity information")
+}

+ 3 - 94
mod/disk/diskcapacity/diskcapacity.go

@@ -2,17 +2,11 @@ package diskcapacity
 
 import (
 	"encoding/json"
-	"errors"
 	"net/http"
 	"path/filepath"
-	"runtime"
-	"strings"
-	"log"
-	"strconv"
-	"os/exec"
 
 	"imuslab.com/arozos/mod/common"
-	"imuslab.com/arozos/mod/disk/diskspace"
+	"imuslab.com/arozos/mod/disk/diskcapacity/dftool"
 	"imuslab.com/arozos/mod/user"
 )
 
@@ -28,14 +22,6 @@ type Resolver struct {
 	UserHandler *user.UserHandler
 }
 
-type Capacity struct {
-	PhysicalDevice    string //The ID of the physical device, like C:/ or /dev/sda1
-	MountingHierarchy string //The Mounting Hierarchy of the vroot
-	Used              int64  //Used capacity in bytes
-	Avilable          int64  //Avilable capacity in bytes
-	Total             int64  //Total capacity in bytes
-}
-
 //Create a new Capacity Resolver with the given user handler
 func NewCapacityResolver(u *user.UserHandler) *Resolver {
 	return &Resolver{
@@ -78,7 +64,7 @@ func (cr *Resolver) HandleCapacityResolving(w http.ResponseWriter, r *http.Reque
 
 }
 
-func (cr *Resolver) ResolveCapacityInfo(username string, vpath string) (*Capacity, error) {
+func (cr *Resolver) ResolveCapacityInfo(username string, vpath string) (*dftool.Capacity, error) {
 	//Resolve the vpath for this user
 	userinfo, err := cr.UserHandler.GetUserInfoFromUsername(username)
 	if err != nil {
@@ -91,83 +77,6 @@ func (cr *Resolver) ResolveCapacityInfo(username string, vpath string) (*Capacit
 	}
 
 	realpath = filepath.ToSlash(filepath.Clean(realpath))
-	return cr.GetCapacityInfo(realpath)
-}
-
-func (cr *Resolver) GetCapacityInfo(realpath string) (*Capacity, error) {
-	rpathAbs, err := filepath.Abs(realpath)
-	if err != nil {
-		return nil, err
-	}
-
-	if runtime.GOOS == "windows" {
-		//Windows
-		//Extract disk ID from path
-		rpathAbs = filepath.ToSlash(filepath.Clean(rpathAbs))
-		diskRoot := strings.Split(rpathAbs, "/")[0]
-
-		//Match the disk space info generated from diskspace
-		logicDiskInfo := diskspace.GetAllLogicDiskInfo()
-		for _, ldi := range logicDiskInfo {
-			if strings.TrimSpace(ldi.Device) == strings.TrimSpace(diskRoot) {
-				//Matching device ID
-				return &Capacity{
-					PhysicalDevice: ldi.Device,
-					Used:           ldi.Used,
-					Avilable:       ldi.Available,
-					Total:          ldi.Volume,
-				}, nil
-			}
-		}
-
-	} else {
-		//Assume Linux or Mac
-		//Use command: df -P {abs_path}
-		cmd := exec.Command("df", "-P", rpathAbs)
-		log.Println("df", "-P", rpathAbs)
-		out, err := cmd.CombinedOutput()
-		if err != nil {
-			return nil, err
-		}
-
-		//Get the last line of the output
-		diskInfo := strings.TrimSpace(string(out))
-		tmp := strings.Split(diskInfo, "\n")
-		targetDiskInfo := strings.Join(tmp[len(tmp) - 1:], " ");
-		for strings.Contains(targetDiskInfo, "  "){
-			targetDiskInfo = strings.ReplaceAll(targetDiskInfo, "  ", " ")
-		}
-
-		diskInfoSlice := strings.Split(targetDiskInfo, " ")
-
-		if len(diskInfoSlice) < 4{
-			return nil, errors.New("Malformed output for df -P")
-		}
-
-		//Extract capacity information from df output
-		total, err := strconv.ParseInt(diskInfoSlice[1], 10, 64)
-		if err != nil{
-			return nil, errors.New("Malformed output for df -P")
-		}
-
-		used, err := strconv.ParseInt(diskInfoSlice[2], 10, 64)
-		if err != nil{
-			return nil, errors.New("Malformed output for df -P")
-		}
-
-		availbe, err := strconv.ParseInt(diskInfoSlice[3], 10, 64)
-		if err != nil{
-			return nil, errors.New("Malformed output for df -P")
-		}
-
-		//Return the capacity info struct, capacity is reported in 1024 bytes block
-		return &Capacity{
-			PhysicalDevice: diskInfoSlice[0],
-			Used: used * 1024,
-			Avilable: availbe * 1024,
-			Total: total * 1024,
-		}, nil
-	}
 
-	return nil, errors.New("Unable to resolve matching disk capacity information")
+	return dftool.GetCapacityInfoFromPath(realpath)
 }

BIN
web/Timer/sound/A Himitsu - Adventures [Argofox].mp3


+ 0 - 8
web/Timer/sound/LICENSE

@@ -1,8 +0,0 @@
-This music is composed by Argofox, the origin can be found on
-https://soundcloud.com/argofox/a-himitsu-adventures
-
-This track is licensed under CC BY. Read the full license term here:
-https://creativecommons.org/licenses/by/3.0/legalcode
-
-Description required by author: 
-A Himitsu - Adventures: youtu.be/8BXNwnxaVQE

+ 4 - 0
web/Timer/sound/LICENSE.txt

@@ -0,0 +1,4 @@
+imuslab_theme.mp3
+
+CopyRight tobychui, All Right Reserved
+If you are distributing the ArozOS, please replace this with your own music.

BIN
web/Timer/sound/imuslab_theme.mp3


+ 4 - 4
web/Timer/timer.html

@@ -56,7 +56,7 @@
                         <i class="pause icon"></i>
                     </button>
                     <button id="stopalarm" class="ts negative tiny icon button disabled" onClick="stopAlarm();" style="position:fixed;top:80px;right:3px;">
-                        <i class="volume down icon"></i>
+                        <i class="alarm mute icon"></i>
                     </button>
                 </div>
             </div>
@@ -181,8 +181,8 @@
                 if (alarmStartTime != 0){
                     stopAlarm();
                 }
-                audio = new Audio('sound/A Himitsu - Adventures [Argofox].mp3');
-                audio.volume = 0.1;
+                audio = new Audio('sound/imuslab_theme.mp3');
+                audio.volume = 0.02;
                 audio.play();
                 alarmStartTime = time();
                 volIncreaseInterval = setInterval( increaseVolumeALittleBit, 5000);
@@ -200,7 +200,7 @@
             }
             
             function increaseVolumeALittleBit(){
-                audio.volume += 0.05;
+                audio.volume += 0.02;
                 console.log("[Timer] Alarm volume increased to: " + audio.volume);
             }