CasaOS/route/v1/persion.go

285 lines
8.8 KiB
Go
Raw Normal View History

2022-02-17 10:43:25 +00:00
package v1
import (
2022-02-28 06:14:39 +00:00
"encoding/json"
"fmt"
2022-03-18 03:13:07 +00:00
"io/ioutil"
"net/http"
2022-03-18 03:13:07 +00:00
"reflect"
"strconv"
2022-02-17 10:43:25 +00:00
"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/config"
oasis_err2 "github.com/IceWhaleTech/CasaOS/pkg/utils/oasis_err"
2022-02-17 10:43:25 +00:00
"github.com/IceWhaleTech/CasaOS/service"
model2 "github.com/IceWhaleTech/CasaOS/service/model"
"github.com/IceWhaleTech/CasaOS/types"
2022-02-17 10:43:25 +00:00
"github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid"
)
func PersonTest(c *gin.Context) {
2022-03-16 07:41:14 +00:00
token := c.Query("token")
2022-02-17 10:43:25 +00:00
//service.MyService.Person().GetPersionInfo("fb2333a1-72b2-4cb4-9e31-61ccaffa55b9")
2022-02-28 06:14:39 +00:00
msg := model.MessageModel{}
2022-03-18 03:13:07 +00:00
msg.Type = types.PERSONHELLO
2022-03-16 07:41:14 +00:00
msg.Data = ""
2022-02-28 06:14:39 +00:00
msg.From = config.ServerInfo.Token
2022-03-16 07:41:14 +00:00
msg.To = token
msg.UUId = uuid.NewV4().String()
2022-03-18 03:13:07 +00:00
dd, err := service.Dial(msg, true)
2022-02-18 11:06:03 +00:00
if err == nil {
2022-03-16 07:41:14 +00:00
fmt.Println(err)
2022-02-18 11:06:03 +00:00
}
2022-03-16 07:41:14 +00:00
fmt.Println(dd)
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
2022-02-17 10:43:25 +00:00
}
2022-03-18 03:13:07 +00:00
// @Summary retry download file
2022-03-16 07:41:14 +00:00
// @Produce application/json
// @Accept application/json
// @Tags persion
2022-03-18 03:13:07 +00:00
// @Param uui path string true "download uuid"
// @Param path query string true "file path"
2022-03-16 07:41:14 +00:00
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
2022-03-18 03:13:07 +00:00
// @Router /persion/refile/{uuid} [get]
func GetPersionReFile(c *gin.Context) {
path := c.Query("path")
uuid := c.Param("uuid")
if len(path) == 0 && len(uuid) == 0 {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
task := service.MyService.Download().GetDownloadById(uuid)
if reflect.DeepEqual(task, model2.PersionDownloadDBModel{}) {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.PERSION_REMOTE_ERROR, Message: oasis_err2.GetMsg(oasis_err2.PERSION_REMOTE_ERROR)})
return
}
token := task.From
if _, ok := service.UDPAddressMap[token]; !ok {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.PERSION_REMOTE_ERROR, Message: oasis_err2.GetMsg(oasis_err2.PERSION_REMOTE_ERROR)})
return
}
m := model.MessageModel{}
m.Data = path
m.From = config.ServerInfo.Token
m.To = token
m.Type = types.PERSONDOWNLOAD
m.UUId = uuid
go service.Dial(m, false)
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
}
// @Summary download file
// @Produce application/json
// @Accept application/json
// @Tags persion
// @Param token query string true "opponent token"
// @Param path query string true "file path"
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /persion/file [get]
func GetPersionFile(c *gin.Context) {
2022-03-16 07:41:14 +00:00
path := c.Query("path")
2022-03-16 07:41:14 +00:00
token := c.Query("token")
if len(path) == 0 && len(token) == 0 {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
2022-03-18 03:13:07 +00:00
if _, ok := service.UDPAddressMap[token]; !ok {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.PERSION_REMOTE_ERROR, Message: oasis_err2.GetMsg(oasis_err2.PERSION_REMOTE_ERROR)})
return
}
// task id
uuid := uuid.NewV4().String()
task := model2.PersionDownloadDBModel{}
task.UUID = uuid
task.Name = ""
task.Length = 0
2022-03-18 03:13:07 +00:00
task.From = token
task.Size = 0
task.State = types.DOWNLOADAWAIT
task.Type = 0
2022-03-16 07:41:14 +00:00
service.MyService.Download().AddDownloadTask(task)
m := model.MessageModel{}
m.Data = path
m.From = config.ServerInfo.Token
m.To = token
2022-03-18 03:13:07 +00:00
m.Type = types.PERSONDOWNLOAD
2022-03-16 07:41:14 +00:00
m.UUId = uuid
2022-03-18 03:13:07 +00:00
go service.Dial(m, false)
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
}
2022-03-16 07:41:14 +00:00
// @Summary delete download file records
// @Produce application/json
// @Accept application/json
// @Tags persion
2022-03-18 03:13:07 +00:00
// @Param uuid path string true "download uuid"
2022-03-16 07:41:14 +00:00
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
2022-03-18 03:13:07 +00:00
// @Router /persion/file/{uuid} [delete]
2022-03-16 07:41:14 +00:00
func DeletePersionDownloadFile(c *gin.Context) {
2022-03-18 03:13:07 +00:00
id := c.Param("uuid")
2022-03-16 07:41:14 +00:00
if len(id) == 0 {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
2022-03-16 07:41:14 +00:00
service.MyService.Download().DelDownload(id)
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
}
2022-03-16 07:41:14 +00:00
// @Summary get file download list
// @Produce application/json
// @Accept application/json
// @Tags persion
2022-03-18 03:13:07 +00:00
// @Param state query int true "wait:1,downloading:1,pause:2,finish:3,error:4" Enums(0,1,2,4)
2022-03-16 07:41:14 +00:00
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /persion/list [get]
func GetPersionDownloadList(c *gin.Context) {
state := c.DefaultQuery("state", "")
list := service.MyService.Download().GetDownloadListByState(state)
2022-03-18 03:13:07 +00:00
//if it is downloading, it need to add 'already'
if state == strconv.Itoa(types.DOWNLOADING) {
for i := 0; i < len(list); i++ {
tempDir := config.AppInfo.RootPath + "/temp" + "/" + list[i].UUID
files, err := ioutil.ReadDir(tempDir)
if err == nil {
list[i].Already = len(files)
}
}
}
2022-03-16 07:41:14 +00:00
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: list})
}
2022-03-18 03:13:07 +00:00
// @Summary edit friend's nick
// @Produce application/json
// @Accept application/json
// @Tags persion
2022-03-18 03:13:07 +00:00
// @Param token path string true "token"
// @Param nick formData string true "nick name"
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
2022-03-18 03:13:07 +00:00
// @Router /persion/nick/{token} [put]
func PutPersionNick(c *gin.Context) {
token := c.Param("token")
nick := c.PostForm("nick")
if len(token) == 0 || len(nick) == 0 {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
friend := model2.FriendModel{}
friend.Token = token
friend.NickName = nick
service.MyService.Friend().EditFriendNick(friend)
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
}
2022-03-18 03:13:07 +00:00
// @Summary get friend list
// @Produce application/json
// @Accept application/json
// @Tags persion
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /persion/users [get]
func GetPersionFriend(c *gin.Context) {
list := service.MyService.Friend().GetFriendList()
2022-03-18 03:13:07 +00:00
for i := 0; i < len(list); i++ {
if v, ok := service.UDPAddressMap[list[i].Token]; ok && len(v) > 0 {
list[i].OnLine = true
}
}
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: list})
}
// @Summary add friend
// @Produce application/json
// @Accept application/json
// @Tags persion
// @Param token formData int true "Opponent token"
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /persion/user [post]
func PostAddPersionFriend(c *gin.Context) {
token := c.PostForm("token")
if len(token) == 0 {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
2022-03-16 07:41:14 +00:00
msg := model.MessageModel{}
2022-03-18 03:13:07 +00:00
msg.Type = types.PERSONCONNECTION
msg.Data = token
msg.From = config.ServerInfo.Token
2022-03-16 07:41:14 +00:00
msg.To = token
msg.UUId = uuid.NewV4().String()
2022-03-16 07:41:14 +00:00
2022-03-18 03:13:07 +00:00
go service.Dial(msg, true)
friend := model2.FriendModel{}
friend.Token = token
service.MyService.Friend().AddFriend(friend)
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
}
2022-03-16 07:41:14 +00:00
// @Summary get directory list
// @Produce application/json
// @Accept application/json
// @Tags persion
2022-03-18 03:13:07 +00:00
// @Param token query string true "Opponent token"
// @Param path query string true "dir path"
2022-03-16 07:41:14 +00:00
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /persion/directory [get]
func GetPersionDirectory(c *gin.Context) {
path := c.Query("path")
2022-03-16 07:41:14 +00:00
token := c.Query("token")
if len(path) == 0 && len(token) == 0 {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
2022-03-18 03:13:07 +00:00
if _, ok := service.UDPAddressMap[token]; !ok {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.PERSION_REMOTE_ERROR, Message: oasis_err2.GetMsg(oasis_err2.PERSION_REMOTE_ERROR)})
return
}
uuid := uuid.NewV4().String()
m := model.MessageModel{}
m.Data = path
m.From = config.ServerInfo.Token
2022-03-16 07:41:14 +00:00
m.To = token
2022-03-18 03:13:07 +00:00
m.Type = types.PERSONDIRECTORY
m.UUId = uuid
2022-03-18 03:13:07 +00:00
result, err := service.Dial(m, false)
if err != nil {
2022-03-18 03:13:07 +00:00
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
return
}
dataModel := []model.Path{}
2022-03-18 03:13:07 +00:00
if uuid == m.UUId {
dataModelByte, _ := json.Marshal(result.Data)
err := json.Unmarshal(dataModelByte, &dataModel)
2022-03-18 03:13:07 +00:00
if err != nil {
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
return
}
}
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS), Data: dataModel})
}