CasaOS/service/connections.go

79 lines
2.7 KiB
Go
Raw Normal View History

/*
* @Author: LinkLeong link@icewhale.org
* @Date: 2022-07-26 18:13:22
* @LastEditors: LinkLeong
* @LastEditTime: 2022-08-04 20:10:31
* @FilePath: /CasaOS/service/connections.go
* @Description:
* @Website: https://www.casaos.io
* Copyright (c) 2022 by icewhale, All Rights Reserved.
*/
package service
import (
2023-04-24 03:24:22 +00:00
"fmt"
"github.com/IceWhaleTech/CasaOS/service/model"
model2 "github.com/IceWhaleTech/CasaOS/service/model"
2023-02-09 02:31:15 +00:00
"github.com/moby/sys/mount"
2023-04-24 03:24:22 +00:00
"golang.org/x/sys/unix"
"gorm.io/gorm"
)
type ConnectionsService interface {
GetConnectionsList() (connections []model2.ConnectionsDBModel)
GetConnectionByHost(host string) (connections []model2.ConnectionsDBModel)
GetConnectionByID(id string) (connections model2.ConnectionsDBModel)
CreateConnection(connection *model2.ConnectionsDBModel)
DeleteConnection(id string)
UpdateConnection(connection *model2.ConnectionsDBModel)
2023-04-24 03:24:22 +00:00
MountSmaba(username, host, directory, port, mountPoint, password string) error
2023-02-09 02:31:15 +00:00
UnmountSmaba(mountPoint string) error
}
type connectionsStruct struct {
db *gorm.DB
}
func (s *connectionsStruct) GetConnectionByHost(host string) (connections []model2.ConnectionsDBModel) {
s.db.Select("username,host,status,id").Where("host = ?", host).Find(&connections)
return
}
func (s *connectionsStruct) GetConnectionByID(id string) (connections model2.ConnectionsDBModel) {
s.db.Select("username,password,host,status,id,directories,mount_point,port").Where("id = ?", id).First(&connections)
return
}
func (s *connectionsStruct) GetConnectionsList() (connections []model2.ConnectionsDBModel) {
s.db.Select("username,host,port,status,id,mount_point").Find(&connections)
return
}
func (s *connectionsStruct) CreateConnection(connection *model2.ConnectionsDBModel) {
s.db.Create(connection)
}
func (s *connectionsStruct) UpdateConnection(connection *model2.ConnectionsDBModel) {
s.db.Save(connection)
}
func (s *connectionsStruct) DeleteConnection(id string) {
s.db.Where("id= ?", id).Delete(&model.ConnectionsDBModel{})
}
2023-04-24 03:24:22 +00:00
func (s *connectionsStruct) MountSmaba(username, host, directory, port, mountPoint, password string) error {
err := unix.Mount(
fmt.Sprintf("//%s/%s", host, directory),
mountPoint,
"cifs",
unix.MS_NOATIME|unix.MS_NODEV|unix.MS_NOSUID,
fmt.Sprintf("username=%s,password=%s", username, password),
)
return err
//str := command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;MountCIFS " + username + " " + host + " " + directory + " " + port + " " + mountPoint + " " + password)
//return str
}
2023-02-09 02:31:15 +00:00
func (s *connectionsStruct) UnmountSmaba(mountPoint string) error {
return mount.Unmount(mountPoint)
}
func NewConnectionsService(db *gorm.DB) ConnectionsService {
return &connectionsStruct{db: db}
}