connections.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * @Author: LinkLeong link@icewhale.org
  3. * @Date: 2022-07-26 18:13:22
  4. * @LastEditors: LinkLeong
  5. * @LastEditTime: 2022-08-04 20:10:31
  6. * @FilePath: /CasaOS/service/connections.go
  7. * @Description:
  8. * @Website: https://www.casaos.io
  9. * Copyright (c) 2022 by icewhale, All Rights Reserved.
  10. */
  11. package service
  12. import (
  13. "fmt"
  14. "github.com/IceWhaleTech/CasaOS/service/model"
  15. model2 "github.com/IceWhaleTech/CasaOS/service/model"
  16. "github.com/moby/sys/mount"
  17. "golang.org/x/sys/unix"
  18. "gorm.io/gorm"
  19. )
  20. type ConnectionsService interface {
  21. GetConnectionsList() (connections []model2.ConnectionsDBModel)
  22. GetConnectionByHost(host string) (connections []model2.ConnectionsDBModel)
  23. GetConnectionByID(id string) (connections model2.ConnectionsDBModel)
  24. CreateConnection(connection *model2.ConnectionsDBModel)
  25. DeleteConnection(id string)
  26. UpdateConnection(connection *model2.ConnectionsDBModel)
  27. MountSmaba(username, host, directory, port, mountPoint, password string) error
  28. UnmountSmaba(mountPoint string) error
  29. }
  30. type connectionsStruct struct {
  31. db *gorm.DB
  32. }
  33. func (s *connectionsStruct) GetConnectionByHost(host string) (connections []model2.ConnectionsDBModel) {
  34. s.db.Select("username,host,status,id").Where("host = ?", host).Find(&connections)
  35. return
  36. }
  37. func (s *connectionsStruct) GetConnectionByID(id string) (connections model2.ConnectionsDBModel) {
  38. s.db.Select("username,password,host,status,id,directories,mount_point,port").Where("id = ?", id).First(&connections)
  39. return
  40. }
  41. func (s *connectionsStruct) GetConnectionsList() (connections []model2.ConnectionsDBModel) {
  42. s.db.Select("username,host,port,status,id,mount_point").Find(&connections)
  43. return
  44. }
  45. func (s *connectionsStruct) CreateConnection(connection *model2.ConnectionsDBModel) {
  46. s.db.Create(connection)
  47. }
  48. func (s *connectionsStruct) UpdateConnection(connection *model2.ConnectionsDBModel) {
  49. s.db.Save(connection)
  50. }
  51. func (s *connectionsStruct) DeleteConnection(id string) {
  52. s.db.Where("id= ?", id).Delete(&model.ConnectionsDBModel{})
  53. }
  54. func (s *connectionsStruct) MountSmaba(username, host, directory, port, mountPoint, password string) error {
  55. err := unix.Mount(
  56. fmt.Sprintf("//%s/%s", host, directory),
  57. mountPoint,
  58. "cifs",
  59. unix.MS_NOATIME|unix.MS_NODEV|unix.MS_NOSUID,
  60. fmt.Sprintf("username=%s,password=%s", username, password),
  61. )
  62. return err
  63. //str := command2.ExecResultStr("source " + config.AppInfo.ShellPath + "/helper.sh ;MountCIFS " + username + " " + host + " " + directory + " " + port + " " + mountPoint + " " + password)
  64. //return str
  65. }
  66. func (s *connectionsStruct) UnmountSmaba(mountPoint string) error {
  67. return mount.Unmount(mountPoint)
  68. }
  69. func NewConnectionsService(db *gorm.DB) ConnectionsService {
  70. return &connectionsStruct{db: db}
  71. }