コード例 #1
0
func checkDir(basepath string, scanDir string, dirName string) error {
	dirList, err := ioutil.ReadDir(scanDir)
	if err != nil {
		return err
	}

	if toolkit.SliceLen(dirList) == 1 {
		for _, f := range dirList {
			if f.IsDir() {
				oldpath := filepath.Join(scanDir, f.Name())
				temp_oldpath := filepath.Join(scanDir, dirName)
				if err := os.Rename(oldpath, temp_oldpath); err != nil {
					return err
				}
				if err := toolkit.ZipCompress(temp_oldpath, scanDir+".zip"); err != nil {
					return err
				}
				if err := os.RemoveAll(scanDir); err != nil {
					return err
				}
				if err := toolkit.ZipExtract(scanDir+".zip", basepath); err != nil {
					return err
				}
				if err := os.Remove(scanDir + ".zip"); err != nil {
					return err
				}
			}
		}
	}

	return nil
}
コード例 #2
0
ファイル: widget.go プロジェクト: eaciit/colony-core
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
}
コード例 #3
0
ファイル: widget_m.go プロジェクト: nadnawidy/colony-core
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
}
コード例 #4
0
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
}
コード例 #5
0
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, "")
}