shares.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * @Author: LinkLeong link@icewhale.org
  3. * @Date: 2022-07-26 11:21:14
  4. * @LastEditors: LinkLeong
  5. * @LastEditTime: 2022-08-18 11:16:25
  6. * @FilePath: /CasaOS/service/shares.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. "path/filepath"
  14. "strings"
  15. "github.com/IceWhaleTech/CasaOS/pkg/config"
  16. command2 "github.com/IceWhaleTech/CasaOS/pkg/utils/command"
  17. "github.com/IceWhaleTech/CasaOS/pkg/utils/file"
  18. "github.com/IceWhaleTech/CasaOS/service/model"
  19. model2 "github.com/IceWhaleTech/CasaOS/service/model"
  20. "gorm.io/gorm"
  21. )
  22. type SharesService interface {
  23. GetSharesList() (shares []model2.SharesDBModel)
  24. GetSharesByPath(path string) (shares []model2.SharesDBModel)
  25. GetSharesByName(name string) (shares []model2.SharesDBModel)
  26. CreateShare(share model2.SharesDBModel)
  27. DeleteShare(id string)
  28. UpdateConfigFile()
  29. InitSambaConfig()
  30. DeleteShareByPath(path string)
  31. }
  32. type sharesStruct struct {
  33. db *gorm.DB
  34. }
  35. func (s *sharesStruct) DeleteShareByPath(path string) {
  36. s.db.Where("path LIKE ?", path+"%").Delete(&model.SharesDBModel{})
  37. s.UpdateConfigFile()
  38. }
  39. func (s *sharesStruct) GetSharesByName(name string) (shares []model2.SharesDBModel) {
  40. s.db.Select("anonymous,path,id").Where("name = ?", name).Find(&shares)
  41. return
  42. }
  43. func (s *sharesStruct) GetSharesByPath(path string) (shares []model2.SharesDBModel) {
  44. s.db.Select("anonymous,path,id").Where("path = ?", path).Find(&shares)
  45. return
  46. }
  47. func (s *sharesStruct) GetSharesList() (shares []model2.SharesDBModel) {
  48. s.db.Select("anonymous,path,id").Find(&shares)
  49. return
  50. }
  51. func (s *sharesStruct) CreateShare(share model2.SharesDBModel) {
  52. s.db.Create(&share)
  53. s.InitSambaConfig()
  54. s.UpdateConfigFile()
  55. }
  56. func (s *sharesStruct) DeleteShare(id string) {
  57. s.db.Where("id= ?", id).Delete(&model.SharesDBModel{})
  58. s.UpdateConfigFile()
  59. }
  60. func (s *sharesStruct) UpdateConfigFile() {
  61. shares := []model2.SharesDBModel{}
  62. s.db.Select("anonymous,path").Find(&shares)
  63. //generated config file
  64. var configStr = ""
  65. for _, share := range shares {
  66. dirName := filepath.Base(share.Path)
  67. configStr += `
  68. [` + dirName + `]
  69. comment = CasaOS share ` + dirName + `
  70. public = Yes
  71. path = ` + share.Path + `
  72. browseable = Yes
  73. read only = No
  74. guest ok = Yes
  75. create mask = 0777
  76. directory mask = 0777
  77. force user = root
  78. `
  79. }
  80. //write config file
  81. file.WriteToPath([]byte(configStr), "/etc/samba", "smb.casa.conf")
  82. //restart samba
  83. command2.ExecResultStrArray("source " + config.AppInfo.ShellPath + "/helper.sh ;RestartSMBD")
  84. }
  85. func (s *sharesStruct) InitSambaConfig() {
  86. if file.Exists("/etc/samba/smb.conf") {
  87. str := file.ReadLine(1, "/etc/samba/smb.conf")
  88. if strings.Contains(str, "# Copyright (c) 2021-2022 CasaOS Inc. All rights reserved.") {
  89. return
  90. }
  91. file.MoveFile("/etc/samba/smb.conf", "/etc/samba/smb.conf.bak")
  92. var smbConf = ""
  93. smbConf += `# Copyright (c) 2021-2022 CasaOS Inc. All rights reserved.
  94. #
  95. #
  96. # ______ _______
  97. # ( __ \ ( ___ )
  98. # | ( \ ) | ( ) |
  99. # | | ) | | | | |
  100. # | | | | | | | |
  101. # | | ) | | | | |
  102. # | (__/ ) | (___) |
  103. # (______/ (_______)
  104. #
  105. # _ _______ _________
  106. # ( ( /| ( ___ ) \__ __/
  107. # | \ ( | | ( ) | ) (
  108. # | \ | | | | | | | |
  109. # | (\ \) | | | | | | |
  110. # | | \ | | | | | | |
  111. # | ) \ | | (___) | | |
  112. # |/ )_) (_______) )_(
  113. #
  114. # _______ _______ ______ _________ _______
  115. # ( ) ( ___ ) ( __ \ \__ __/ ( ____ \ |\ /|
  116. # | () () | | ( ) | | ( \ ) ) ( | ( \/ ( \ / )
  117. # | || || | | | | | | | ) | | | | (__ \ (_) /
  118. # | |(_)| | | | | | | | | | | | | __) \ /
  119. # | | | | | | | | | | ) | | | | ( ) (
  120. # | ) ( | | (___) | | (__/ ) ___) (___ | ) | |
  121. # |/ \| (_______) (______/ \_______/ |/ \_/
  122. #
  123. #
  124. # IMPORTANT: CasaOS will not provide technical support for any issues
  125. # caused by unauthorized modification to the configuration.
  126. [global]
  127. ## fruit settings
  128. min protocol = SMB2
  129. ea support = yes
  130. ## vfs objects = fruit streams_xattr
  131. fruit:metadata = stream
  132. fruit:model = Macmini
  133. fruit:veto_appledouble = no
  134. fruit:posix_rename = yes
  135. fruit:zero_file_id = yes
  136. fruit:wipe_intentionally_left_blank_rfork = yes
  137. fruit:delete_empty_adfiles = yes
  138. map to guest = bad user
  139. include=/etc/samba/smb.casa.conf`
  140. file.WriteToPath([]byte(smbConf), "/etc/samba", "smb.conf")
  141. }
  142. }
  143. func NewSharesService(db *gorm.DB) SharesService {
  144. return &sharesStruct{db: db}
  145. }