Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #276

Merged
merged 16 commits into from
Jun 10, 2022
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

## [0.3.2-pre] - 2022-06-08
## [0.3.2] - 2022-06-10

### Added

Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ func main() {
if err != nil {
fmt.Println(err)
}
err = cron2.AddFunc("0/3 * * * * *", func() {
err = cron2.AddFunc("0/5 * * * * *", func() {
if service.ClientCount > 0 {
route.SendNetINfoBySocket()
route.SendCPUBySocket()
route.SendMemBySocket()
route.SendDiskBySocket()
route.SendUSBBySocket()
// route.SendNetINfoBySocket()
// route.SendCPUBySocket()
// route.SendMemBySocket()
// route.SendDiskBySocket()
// route.SendUSBBySocket()
route.SendAllHardwareStatusBySocket()
}
})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: LinkLeong [email protected]
* @Date: 2022-05-20 16:27:12
* @LastEditors: LinkLeong
* @LastEditTime: 2022-06-08 15:40:33
* @LastEditTime: 2022-06-09 18:18:46
* @FilePath: /CasaOS/model/file.go
* @Description:
* @Website: https://www.casaos.io
Expand All @@ -16,6 +16,8 @@ type FileOperate struct {
TotalSize int64 `json:"total_size"`
ProcessedSize int64 `json:"processed_size"`
To string `json:"to" binding:"required"`
Style string `json:"style"`
Finished bool `json:"finished"`
}

type FileItem struct {
Expand Down
40 changes: 20 additions & 20 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func ReadFullFile(path string) []byte {
}

// File copies a single file from src to dst
func CopyFile(src, dst string) error {
func CopyFile(src, dst, style string) error {
var err error
var srcfd *os.File
var dstfd *os.File
Expand All @@ -197,20 +197,13 @@ func CopyFile(src, dst string) error {
if !strings.HasSuffix(dst, "/") {
dst += "/"
}
dstPath := dst
dst += lastPath
for i := 0; Exists(dst); i++ {
name := strings.Split(lastPath, ".")
nameIndex := 0
if len(name) > 2 {
nameIndex = len(name) - 2
}
name[nameIndex] = name[nameIndex] + "(Copy)"
dst = dstPath
for _, v := range name {
dst += v + "."
if Exists(dst) {
if style == "skip" {
return nil
} else {
os.Remove(dst)
}
dst = strings.TrimSuffix(dst, ".")
}

if srcfd, err = os.Open(src); err != nil {
Expand Down Expand Up @@ -244,7 +237,7 @@ func GetNoDuplicateFileName(fullPath string) string {
}

// Dir copies a whole directory recursively
func CopyDir(src string, dst string) error {
func CopyDir(src string, dst string, style string) error {
var err error
var fds []os.FileInfo
var srcinfo os.FileInfo
Expand All @@ -253,16 +246,23 @@ func CopyDir(src string, dst string) error {
return err
}
if !srcinfo.IsDir() {
if err = CopyFile(src, dst); err != nil {
if err = CopyFile(src, dst, style); err != nil {
fmt.Println(err)
}
return nil
}
dstPath := dst
//dstPath := dst
lastPath := src[strings.LastIndex(src, "/")+1:]
dst += "/" + lastPath
for i := 0; Exists(dst); i++ {
dst = dstPath + "/" + lastPath + strconv.Itoa(i+1)
// for i := 0; Exists(dst); i++ {
// dst = dstPath + "/" + lastPath + strconv.Itoa(i+1)
// }
if Exists(dst) {
if style == "skip" {
return nil
} else {
os.Remove(dst)
}
}
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
return err
Expand All @@ -275,11 +275,11 @@ func CopyDir(src string, dst string) error {
dstfp := dst //path.Join(dst, fd.Name())

if fd.IsDir() {
if err = CopyDir(srcfp, dstfp); err != nil {
if err = CopyDir(srcfp, dstfp, style); err != nil {
fmt.Println(err)
}
} else {
if err = CopyFile(srcfp, dstfp); err != nil {
if err = CopyFile(srcfp, dstfp, style); err != nil {
fmt.Println(err)
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/utils/oasis_err/e.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
FILE_READ_ERROR = 60002
FILE_DELETE_ERROR = 60003
DIR_NOT_EXISTS = 60004
SOURCE_DES_SAME = 60005

//shortcuts
SHORTCUTS_URL_ERROR = 70001
Expand Down Expand Up @@ -88,6 +89,7 @@ var MsgFlags = map[int]string{
FORMAT_ERROR: "Formatting failed, please check if the directory is occupied",

//
SOURCE_DES_SAME: "Source and destination cannot be the same.",
FILE_DOES_NOT_EXIST: "File does not exist",

DIR_NOT_EXISTS: "Directory does not exist",
Expand Down
132 changes: 131 additions & 1 deletion route/periodical.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: LinkLeong [email protected]
* @Date: 2022-05-27 15:55:36
* @LastEditors: LinkLeong
* @LastEditTime: 2022-05-27 18:57:40
* @LastEditTime: 2022-06-10 12:17:59
* @FilePath: /CasaOS/route/periodical.go
* @Description:
* @Website: https://www.casaos.io
Expand Down Expand Up @@ -153,3 +153,133 @@ func SendUSBBySocket() {
}
service.MyService.Notify().SendUSBInfoBySocket(usb)
}

func SendAllHardwareStatusBySocket() {

netList := service.MyService.System().GetNetInfo()
newNet := []model.IOCountersStat{}
nets := service.MyService.System().GetNet(true)
for _, n := range netList {
for _, netCardName := range nets {
if n.Name == netCardName {
item := *(*model.IOCountersStat)(unsafe.Pointer(&n))
item.State = strings.TrimSpace(service.MyService.ZiMa().GetNetState(n.Name))
item.Time = time.Now().Unix()
newNet = append(newNet, item)
break
}
}
}

cpu := service.MyService.System().GetCpuPercent()
num := service.MyService.System().GetCpuCoreNum()
cpuData := make(map[string]interface{})
cpuData["percent"] = cpu
cpuData["num"] = num

list := service.MyService.Disk().LSBLK(true)

summary := model.Summary{}
healthy := true
findSystem := 0

for i := 0; i < len(list); i++ {
if len(list[i].Children) > 0 && findSystem == 0 {

for j := 0; j < len(list[i].Children); j++ {

if len(list[i].Children[j].Children) > 0 {
for _, v := range list[i].Children[j].Children {
if v.MountPoint == "/" {
s, _ := strconv.ParseUint(v.FSSize, 10, 64)
a, _ := strconv.ParseUint(v.FSAvail, 10, 64)
u, _ := strconv.ParseUint(v.FSUsed, 10, 64)
summary.Size += s
summary.Avail += a
summary.Used += u
findSystem = 1
break
}
}
} else {
if list[i].Children[j].MountPoint == "/" {
s, _ := strconv.ParseUint(list[i].Children[j].FSSize, 10, 64)
a, _ := strconv.ParseUint(list[i].Children[j].FSAvail, 10, 64)
u, _ := strconv.ParseUint(list[i].Children[j].FSUsed, 10, 64)
summary.Size += s
summary.Avail += a
summary.Used += u
findSystem = 1
break
}
}
}

}
if findSystem == 1 {
findSystem += 1
continue
}
if list[i].Tran == "sata" || list[i].Tran == "nvme" || list[i].Tran == "spi" || list[i].Tran == "sas" {
temp := service.MyService.Disk().SmartCTL(list[i].Path)
if reflect.DeepEqual(temp, model.SmartctlA{}) {
continue
}

//list[i].Temperature = temp.Temperature.Current
if !temp.SmartStatus.Passed {
healthy = false
}
if len(list[i].Children) > 0 {
for _, v := range list[i].Children {
s, _ := strconv.ParseUint(v.FSSize, 10, 64)
a, _ := strconv.ParseUint(v.FSAvail, 10, 64)
u, _ := strconv.ParseUint(v.FSUsed, 10, 64)
summary.Size += s
summary.Avail += a
summary.Used += u
}
}

}
}

summary.Health = healthy

usbList := service.MyService.Disk().LSBLK(false)
usb := []model.DriveUSB{}
for _, v := range usbList {
if v.Tran == "usb" {
temp := model.DriveUSB{}
temp.Model = v.Model
temp.Name = v.Name
temp.Size = v.Size
mountTemp := true
if len(v.Children) == 0 {
mountTemp = false
}
for _, child := range v.Children {
if len(child.MountPoint) > 0 {
avail, _ := strconv.ParseUint(child.FSAvail, 10, 64)
temp.Avail += avail
used, _ := strconv.ParseUint(child.FSUsed, 10, 64)
temp.Used += used
} else {
mountTemp = false
}
}
temp.Mount = mountTemp
usb = append(usb, temp)
}
}
memInfo := service.MyService.System().GetMemInfo()
memData := make(map[string]interface{})
memData["total"] = memInfo.Total
memData["available"] = memInfo.Available
memData["used"] = memInfo.Used
memData["free"] = memInfo.Free
memData["usedPercent"] = memInfo.UsedPercent

service.MyService.Notify().SendAllHardwareStatusBySocket(summary, usb, memData, cpuData, newNet)

}
4 changes: 2 additions & 2 deletions route/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: LinkLeong [email protected]
* @Date: 2022-05-23 17:18:56
* @LastEditors: LinkLeong
* @LastEditTime: 2022-06-08 16:31:24
* @LastEditTime: 2022-06-09 21:48:10
* @FilePath: /CasaOS/route/socket.go
* @Description:
* @Website: https://www.casaos.io
Expand Down Expand Up @@ -49,7 +49,7 @@ func SocketInit(msg chan notify.Message) {
go func(msg chan notify.Message) {
for v := range msg {
f.Broadcast("", v.Path, &v.Msg)
time.Sleep(time.Millisecond * 300)
time.Sleep(time.Millisecond * 100)
}

}(msg)
Expand Down
27 changes: 17 additions & 10 deletions route/v1/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func GetDownloadFile(c *gin.Context) {
defer ar.Close()
commonDir := file.CommonPrefix(filepath.Separator, list...)


currentPath := filepath.Base(commonDir)

name := "_" + currentPath
Expand Down Expand Up @@ -278,17 +277,20 @@ func DirPath(c *gin.Context) {

//Hide the files or folders in operation
fileQueue := make(map[string]string)
for _, v := range service.OpStrArr {
v, ok := service.FileQueue.Load(v)
if !ok {
continue
}
vt := v.(model.FileOperate)
for _, i := range vt.Item {
lastPath := i.From[strings.LastIndex(i.From, "/")+1:]
fileQueue[vt.To+"/"+lastPath] = i.From
if len(service.OpStrArr) > 0 {
for _, v := range service.OpStrArr {
v, ok := service.FileQueue.Load(v)
if !ok {
continue
}
vt := v.(model.FileOperate)
for _, i := range vt.Item {
lastPath := i.From[strings.LastIndex(i.From, "/")+1:]
fileQueue[vt.To+"/"+lastPath] = i.From
}
}
}

pathList := []model.Path{}
for i := 0; i < len(info); i++ {
if _, ok := fileQueue[info[i].Path]; !ok {
Expand Down Expand Up @@ -495,6 +497,11 @@ func PostOperateFileOrDir(c *gin.Context) {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
if list.To == list.Item[0].From[:strings.LastIndex(list.Item[0].From, "/")] {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SOURCE_DES_SAME, Message: oasis_err2.GetMsg(oasis_err2.SOURCE_DES_SAME)})
return
}

var total int64 = 0
for i := 0; i < len(list.Item); i++ {

Expand Down
2 changes: 1 addition & 1 deletion service/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (d *diskService) SmartCTL(path string) model.SmartctlA {
loger.Error("Failed to unmarshal json", zap.Any("err", err))
}
if !reflect.DeepEqual(m, model.SmartctlA{}) {
Cache.Add(key, m, time.Second*10)
Cache.Add(key, m, time.Hour*24)
}
return m
}
Expand Down
Loading