mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-11-18 12:33:15 +00:00
155 lines
5.9 KiB
Go
155 lines
5.9 KiB
Go
/*
|
|
* @Author: LinkLeong link@icewhale.com
|
|
* @Date: 2022-07-26 11:08:48
|
|
* @LastEditors: LinkLeong
|
|
* @LastEditTime: 2022-07-27 15:30:08
|
|
* @FilePath: /CasaOS/route/v1/samba.go
|
|
* @Description:
|
|
* @Website: https://www.casaos.io
|
|
* Copyright (c) 2022 by icewhale, All Rights Reserved.
|
|
*/
|
|
package v1
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/samba"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
|
"github.com/IceWhaleTech/CasaOS/service"
|
|
model2 "github.com/IceWhaleTech/CasaOS/service/model"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// service
|
|
|
|
func GetSambaSharesList(c *gin.Context) {
|
|
shares := service.MyService.Shares().GetSharesList()
|
|
shareList := []model.Shares{}
|
|
for _, v := range shares {
|
|
shareList = append(shareList, model.Shares{
|
|
Anonymous: v.Anonymous,
|
|
Path: v.Path,
|
|
ID: v.ID,
|
|
})
|
|
}
|
|
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: shareList})
|
|
}
|
|
|
|
func PostSambaSharesCreate(c *gin.Context) {
|
|
shares := []model.Shares{}
|
|
c.ShouldBindJSON(&shares)
|
|
for _, v := range shares {
|
|
if v.Path == "" {
|
|
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INSUFFICIENT_PERMISSIONS, Message: common_err.GetMsg(common_err.INSUFFICIENT_PERMISSIONS)})
|
|
return
|
|
}
|
|
if !file.Exists(v.Path) {
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.DIR_NOT_EXISTS, Message: common_err.GetMsg(common_err.DIR_NOT_EXISTS)})
|
|
return
|
|
}
|
|
if len(service.MyService.Shares().GetSharesByPath(v.Path)) > 0 {
|
|
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.SHARE_ALREADY_EXISTS, Message: common_err.GetMsg(common_err.SHARE_ALREADY_EXISTS)})
|
|
return
|
|
}
|
|
if len(service.MyService.Shares().GetSharesByPath(filepath.Base(v.Path))) > 0 {
|
|
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.SHARE_NAME_ALREADY_EXISTS, Message: common_err.GetMsg(common_err.SHARE_NAME_ALREADY_EXISTS)})
|
|
return
|
|
}
|
|
|
|
}
|
|
for _, v := range shares {
|
|
shareDBModel := model2.SharesDBModel{}
|
|
shareDBModel.Anonymous = true
|
|
shareDBModel.Path = v.Path
|
|
shareDBModel.Name = filepath.Base(v.Path)
|
|
service.MyService.Shares().CreateShare(shareDBModel)
|
|
}
|
|
|
|
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: shares})
|
|
}
|
|
func DeleteSambaShares(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INSUFFICIENT_PERMISSIONS, Message: common_err.GetMsg(common_err.INSUFFICIENT_PERMISSIONS)})
|
|
return
|
|
}
|
|
service.MyService.Shares().DeleteShare(id)
|
|
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: id})
|
|
}
|
|
|
|
//client
|
|
|
|
func GetSambaConnectionsList(c *gin.Context) {
|
|
connections := service.MyService.Connections().GetConnectionsList()
|
|
connectionList := []model.Connections{}
|
|
for _, v := range connections {
|
|
connectionList = append(connectionList, model.Connections{
|
|
ID: v.ID,
|
|
Username: v.Username,
|
|
MountPoint: v.MountPoint,
|
|
Directory: v.Directory,
|
|
Port: v.Port,
|
|
Host: v.Host,
|
|
})
|
|
}
|
|
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: connectionList})
|
|
}
|
|
|
|
func PostSambaConnectionsCreate(c *gin.Context) {
|
|
connection := model.Connections{}
|
|
err := c.ShouldBindJSON(&connection)
|
|
fmt.Println(err)
|
|
if connection.Port == "" {
|
|
connection.Port = "445"
|
|
}
|
|
if connection.Username == "" || connection.Directory == "" || connection.Host == "" || connection.MountPoint == "" {
|
|
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
|
return
|
|
}
|
|
// check is exists
|
|
|
|
connections := service.MyService.Connections().GetConnectionByDirectory(connection.Directory)
|
|
if len(connections) > 0 {
|
|
for _, v := range connections {
|
|
if v.Host == connection.Host {
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.Record_ALREADY_EXIST, Message: common_err.GetMsg(common_err.Record_ALREADY_EXIST), Data: common_err.GetMsg(common_err.Record_ALREADY_EXIST)})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
// check connect is ok
|
|
if err := samba.ConnectSambaService(connection.Host, connection.Port, connection.Username, connection.Password, connection.Directory); err != nil {
|
|
fmt.Println("check", err)
|
|
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
|
|
return
|
|
}
|
|
|
|
connectionDBModel := model2.ConnectionsDBModel{}
|
|
connectionDBModel.Username = connection.Username
|
|
connectionDBModel.Password = connection.Password
|
|
connectionDBModel.Directory = connection.Directory
|
|
connectionDBModel.Host = connection.Host
|
|
connectionDBModel.Port = connection.Port
|
|
connectionDBModel.MountPoint = connection.MountPoint
|
|
file.IsNotExistMkDir(connection.MountPoint)
|
|
service.MyService.Connections().CreateConnection(&connectionDBModel)
|
|
service.MyService.Connections().MountSmaba(&connectionDBModel)
|
|
connection.ID = connectionDBModel.ID
|
|
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: connection})
|
|
}
|
|
|
|
func DeleteSambaConnections(c *gin.Context) {
|
|
id := c.Param("id")
|
|
connection := service.MyService.Connections().GetConnectionByID(id)
|
|
if connection.Username == "" {
|
|
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.Record_NOT_EXIST, Message: common_err.GetMsg(common_err.Record_NOT_EXIST)})
|
|
return
|
|
}
|
|
service.MyService.Connections().UnmountSmaba(connection.MountPoint)
|
|
service.MyService.Connections().DeleteConnection(id)
|
|
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: id})
|
|
}
|