func (a *ApplicationController) SaveApps(r *knot.WebContext) interface{} {
	// upload handler
	err, fileName := helper.UploadHandler(r, "userfile", zipSource)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	o := new(colonycore.Application)
	o.ID = r.Request.FormValue("id")
	o.AppsName = r.Request.FormValue("AppsName")
	enable, err := strconv.ParseBool(r.Request.FormValue("Enable"))
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	o.Enable = enable
	o.AppsName = r.Request.FormValue("AppsName")

	err = colonycore.Delete(o)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = colonycore.Save(o)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	var zipFile string
	if fileName != "" {
		zipFile = fmt.Sprintf("%s", filepath.Join(zipSource, fileName))
	}

	if zipFile != "" && o.ID != "" {
		newDirName = o.ID
		directoryTree, _ := Unzip(zipFile)
		createJson(directoryTree)
	}
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, nil, "")
}
Beispiel #2
0
func (w *WidgetController) SaveWidget(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	err, fileName := helper.UploadHandler(r, "userfile", compressedSource)
	if r.Request.FormValue("mode") != "editor" {
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
	}

	widget := new(colonycore.Widget)
	widget.ID = r.Request.FormValue("_id")
	widget.Title = r.Request.FormValue("title")
	// datasource := r.Request.FormValue("dataSourceId")
	widget.Description = r.Request.FormValue("description")

	// widget.DataSourceId = strings.Split(datasource, ",")

	widgetConfig := toolkit.Ms{}
	if fileName != "" {
		// widget.URL = w.Server.Address
		widgetConfig, err = widget.ExtractFile(compressedSource, fileName)
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
		widget.Config = widgetConfig
	}

	if r.Request.FormValue("mode") == "editor" && fileName == "" {
		data := colonycore.Widget{}
		data.ID = widget.ID
		data.GetById()
		widget.Config = data.Config
		// widget.URL = data.URL
	}

	if err := widget.Save(); err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	return helper.CreateResult(true, widget, "")
}
func (a *ApplicationController) SaveApps(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	err, fileName := helper.UploadHandler(r, "userfile", zipSource)

	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	o := new(colonycore.Application)
	o.ID = r.Request.FormValue("_id")
	o.AppsName = r.Request.FormValue("AppsName")
	o.Type = r.Request.FormValue("Type")
	o.Port = r.Request.FormValue("Port")

	var Command, Variable interface{}
	err = json.Unmarshal([]byte(r.Request.FormValue("Command")), &Command)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	err = json.Unmarshal([]byte(r.Request.FormValue("Variable")), &Variable)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	o.Command = Command
	o.Variable = Variable

	err = colonycore.Delete(o)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	cursor, err := colonycore.Find(new(colonycore.Application), dbox.Eq("Port", o.Port))
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}
	defer cursor.Close()

	err = colonycore.Delete(o)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	data := []colonycore.Application{}
	err = cursor.Fetch(&data, 0, false)
	if len(data) > 0 {
		return helper.CreateResult(false, nil, fmt.Sprintf("Port %s already in use", o.Port))
	}

	err = colonycore.Save(o)
	if err != nil {
		return helper.CreateResult(false, nil, err.Error())
	}

	fileExtract := strings.Join([]string{zipSource, fileName}, toolkit.PathSeparator)
	destinationExtract := strings.Join([]string{zipSource, o.ID}, toolkit.PathSeparator)

	if runtime.GOOS == "windows" {
		err = exec.Command("cmd", "-c", "rmdir", "/s", "/q", destinationExtract).Run()
		// if err != nil {
		// 	return helper.CreateResult(false, nil, err.Error())
		// }
	} else {
		err = exec.Command("rm", "-rf", destinationExtract).Run()
		// if err != nil {
		// 	return helper.CreateResult(false, nil, err.Error())
		// }
	}

	if strings.Contains(fileName, ".tar.gz") {
		err = toolkit.TarGzExtract(fileExtract, destinationExtract)
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
	} else if strings.Contains(fileName, ".gz") {
		err = toolkit.GzExtract(fileExtract, destinationExtract)
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
	} else if strings.Contains(fileName, ".tar") {
		err = toolkit.TarExtract(fileExtract, destinationExtract)
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
	} else if strings.Contains(fileName, ".zip") {
		err = toolkit.ZipExtract(fileExtract, destinationExtract)
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
	}

	os.Remove(filepath.Join(zipSource, fileName))
	var zipFile string
	if fileName != "" {
		zipFile = filepath.Join(zipSource, fileName)
	}

	if zipFile != "" && o.ID != "" {
		newDirName = o.ID
		directoryTree, zipName, _ := unzip(zipFile)
		createJson(directoryTree)

		o.ZipName = zipName
		err = colonycore.Save(o)
		if err != nil {
			return helper.CreateResult(false, nil, err.Error())
		}
	}

	return helper.CreateResult(true, nil, "")
}