|
- /* ******************************************************************************
- * 2019 - present Contributed by Apulis Technology (Shenzhen) Co. LTD
- *
- * This program and the accompanying materials are made available under the
- * terms of the MIT License, which is available at
- * https://www.opensource.org/licenses/MIT
- *
- * See the NOTICE file distributed with this work for additional
- * information regarding copyright ownership.
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- * SPDX-License-Identifier: MIT
- ******************************************************************************/
- package controllers
-
- import (
- "encoding/json"
- "fmt"
- "os"
- "os/exec"
- "path"
- "strconv"
-
- "github.com/apulis/app/ai-arts-backend/internal/dto"
- "github.com/apulis/app/ai-arts-backend/internal/services"
- "github.com/gin-gonic/gin"
- )
-
- type WebHookController struct{}
-
- func (controllers *WebHookController) WebHook(c *gin.Context) {
- var req dto.WebHookReq
-
- if bindAndValidate(c, &req) {
- fmt.Println(c.Request.Header)
- data, _ := json.Marshal(req)
- fmt.Println(string(data))
- fileServerPvcPath := req.StoragePath
-
- srcFile := services.GetGlobalPvcService().ConvertPvcPathToLocalPath(fileServerPvcPath)
- srcFile = path.Join(srcFile)
-
- task_type, exist := req.Metadata["taskType"]
- if !exist {
- fail(c, fmt.Errorf("taskType isEmpty"))
- return
- }
-
- var err error
- if task_type == "aiarts_dev_upload_data" {
- err = controllers.DevUploadData(srcFile, req.Metadata)
- }
-
- if err == nil {
- ok(c)
- } else {
- fmt.Println("webhook err:", err)
- fail(c, err)
- }
- }
- }
-
- func (controllers *WebHookController) DevUploadData(srcFile string, metadata map[string]string) error {
- s, _ := json.Marshal(metadata)
- data := dto.DevUploadData{}
- err := json.Unmarshal(s, &data)
- if err != nil {
- return err
- }
- destFile := ""
- pid, err := strconv.Atoi(data.ProjectId)
- if err != nil {
- return err
- }
- uid, err := strconv.Atoi(data.UserId)
- if err != nil {
- return err
- }
-
- gid, err := strconv.Atoi(data.UserGroupId)
- if err != nil {
- return err
- }
-
- switch data.DirType {
- case "code":
- creator := dto.CodeResourceCreator{
- ResourceCreator: dto.ResourceCreator{UserName: data.UserName},
- ProjectId: int64(pid),
- UserId: int64(uid),
- }
- destFile = creator.Path()
- case "userdata":
- creator := dto.UserDataResourceCreator{
- ResourceCreator: dto.ResourceCreator{UserName: data.UserName},
- UserId: int64(uid),
- }
- destFile = creator.Path()
- case "teamdata":
-
- creator := dto.TeamDataResourceCreator{
- ResourceCreator: dto.ResourceCreator{UserName: data.UserName},
- GroupId: int64(gid),
- }
- destFile = creator.Path()
- }
- destFile = services.GetGlobalPvcService().ConvertPvcPathToLocalPath(destFile)
-
- services.GetGlobalPvcService().CreateDir(destFile)
- fmt.Printf("mv file from %s to %s", srcFile, destFile)
- //return nil
- cmd := exec.Command("mv", srcFile, destFile)
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
- fmt.Println(cmd.String())
- err = cmd.Run()
- fmt.Println(cmd.String(), err)
- if err != nil {
- return err
- }
- return nil
- }
|