Beispiel #1
0
func (w *Widget) ExtractFile(compressedSource string, fileName string) (toolkit.Ms, error) {
	compressedFile := filepath.Join(compressedSource, fileName)
	extractDest := filepath.Join(compressedSource, w.ID)

	if err := os.RemoveAll(extractDest); err != nil {
		return nil, err
	}

	if strings.Contains(fileName, ".tar.gz") {
		if err := toolkit.TarGzExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	} else if strings.Contains(fileName, ".gz") {
		if err := toolkit.GzExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	} else if strings.Contains(fileName, ".tar") {
		if err := toolkit.TarExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	} else if strings.Contains(fileName, ".zip") {
		if err := toolkit.ZipExtract(compressedFile, extractDest); err != nil {
			return nil, err
		}
	}

	if err := os.Remove(compressedFile); err != nil {
		return nil, err
	}

	path, err := GetWidgetPath(extractDest)
	if path == "" {
		return nil, errors.New("directory doesn't contains index.html")
	}
	if err != nil {
		return nil, err
	}

	getConfigFile := filepath.Join(path, "config.json")
	result, err := GetJsonFile(getConfigFile)
	if err != nil {
		return nil, err
	}
	return result, nil
}
Beispiel #2
0
func (w *Widget) ExtractFile(compressedSource string, fileName string) error {
	compressedFile := filepath.Join(compressedSource, fileName)
	extractDest := filepath.Join(compressedSource, w.ID)

	if err := os.RemoveAll(extractDest); err != nil {
		return err
	}

	if strings.Contains(fileName, ".tar.gz") {
		if err := toolkit.TarGzExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".gz") {
		if err := toolkit.GzExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".tar") {
		if err := toolkit.TarExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".zip") {
		if err := toolkit.ZipExtract(compressedFile, extractDest); err != nil {
			return err
		}
	}

	if err := os.Remove(compressedFile); err != nil {
		return err
	}

	getConfigFile := filepath.Join(extractDest, "config.json")
	content, err := ioutil.ReadFile(getConfigFile)
	if err != nil {
		return err
	}

	// contentstring := string(content)
	var result = Config{}.data
	toolkit.Unjson(content, &result)
	toolkit.Println(result, string(content))
	// checkDir(compressedSource, extractDest, w.ID)

	return nil
}
func (w *Widget) ExtractFile(compressedSource string, fileName string) error {
	compressedFile := filepath.Join(compressedSource, fileName)
	extractDest := filepath.Join(compressedSource, w.ID)

	if runtime.GOOS == "windows" {
		exec.Command("cmd", "/C", "rmdir", "/s", "/q", extractDest).Run()
	} else {
		exec.Command("rm", "-rf", extractDest).Run()
	}

	if strings.Contains(fileName, ".tar.gz") {
		if err := toolkit.TarGzExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".gz") {
		if err := toolkit.GzExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".tar") {
		if err := toolkit.TarExtract(compressedFile, extractDest); err != nil {
			return err
		}
	} else if strings.Contains(fileName, ".zip") {
		if err := toolkit.ZipExtract(compressedFile, extractDest); err != nil {
			return err
		}
	}

	if err := os.Remove(compressedFile); err != nil {
		return err
	}

	// checkDir(compressedSource, extractDest, w.ID)

	return nil
}
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, "")
}