From a9c2ff3dbbacd952b95210dd5a04a2ea88a5ffe8 Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Tue, 10 Oct 2023 07:40:52 +0100 Subject: [PATCH] add function --- route/v1.go | 1 + route/v1/disk.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/route/v1.go b/route/v1.go index e0aeea8..7a322a0 100644 --- a/route/v1.go +++ b/route/v1.go @@ -47,6 +47,7 @@ func InitV1Router() *gin.Engine { v1DisksGroup.GET("/usb", v1.GetDisksUSBList) v1DisksGroup.DELETE("/usb", v1.DeleteDiskUSB) v1DisksGroup.DELETE("", v1.DeleteDisksUmount) + v1DisksGroup.GET("/size", v1.GetDiskSize) } v1StorageGroup := v1Group.Group("/storage") diff --git a/route/v1/disk.go b/route/v1/disk.go index 24e2a94..c5d3d2d 100644 --- a/route/v1/disk.go +++ b/route/v1/disk.go @@ -13,6 +13,7 @@ import ( model1 "github.com/IceWhaleTech/CasaOS-LocalStorage/model" "github.com/IceWhaleTech/CasaOS-LocalStorage/service" "github.com/gin-gonic/gin" + "github.com/shirou/gopsutil/v3/disk" "go.uber.org/zap" ) @@ -231,3 +232,21 @@ func DeleteDisksUmount(c *gin.Context) { c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: path}) } +func GetDiskSize(c *gin.Context) { + path := c.Query("path") + if len(path) == 0 { + c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)}) + return + } + p, err := disk.Usage(path) + if err != nil { + c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()}) + return + } + data := map[string]interface{}{ + "path": path, + "free": p.Free, + "used": p.Used, + } + c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data}) +}