コード例 #1
0
ファイル: app_files.go プロジェクト: Jack1996/cli
func AppFilesInDir(dir string) (appFiles []models.AppFileFields, err error) {
	dir, err = filepath.Abs(dir)
	if err != nil {
		return
	}

	err = WalkAppFiles(dir, func(fileName string, fullPath string) (err error) {
		fileInfo, err := os.Lstat(fullPath)
		if err != nil {
			return
		}

		appFile := models.AppFileFields{
			Path: filepath.ToSlash(fileName),
			Size: fileInfo.Size(),
		}

		if fileInfo.IsDir() {
			appFile.Sha1 = "0"
			appFile.Size = 0
		} else {
			hash := sha1.New()
			err = fileutils.CopyPathToWriter(fullPath, hash)
			if err != nil {
				return
			}
			appFile.Sha1 = fmt.Sprintf("%x", hash.Sum(nil))
		}

		appFiles = append(appFiles, appFile)
		return
	})
	return
}
コード例 #2
0
ファイル: app_files.go プロジェクト: vframbach/cli
func (appfiles ApplicationFiles) AppFilesInDir(dir string) (appFiles []models.AppFileFields, err error) {
	dir, err = filepath.Abs(dir)
	if err != nil {
		return
	}

	err = appfiles.WalkAppFiles(dir, func(fileName string, fullPath string) error {
		fileInfo, err := os.Lstat(fullPath)
		if err != nil {
			return err
		}

		appFile := models.AppFileFields{
			Path: filepath.ToSlash(fileName),
			Size: fileInfo.Size(),
		}

		if fileInfo.IsDir() {
			appFile.Sha1 = "0"
			appFile.Size = 0
		} else {
			hash := sha1.New()
			file, err := os.Open(fullPath)
			if err != nil {
				return err
			}
			defer file.Close()

			_, err = io.Copy(hash, file)
			if err != nil {
				return err
			}

			appFile.Sha1 = fmt.Sprintf("%x", hash.Sum(nil))
		}

		appFiles = append(appFiles, appFile)

		return nil
	})

	return
}
コード例 #3
0
ファイル: app_files.go プロジェクト: Reejoshi/cli
func (appfiles ApplicationFiles) AppFilesInDir(dir string) ([]models.AppFileFields, error) {
	appFiles := []models.AppFileFields{}

	fullDirPath, toplevelErr := filepath.Abs(dir)
	if toplevelErr != nil {
		return appFiles, toplevelErr
	}

	toplevelErr = appfiles.WalkAppFiles(fullDirPath, func(fileName string, fullPath string) error {
		fileInfo, err := os.Lstat(fullPath)
		if err != nil {
			return err
		}

		appFile := models.AppFileFields{
			Path: filepath.ToSlash(fileName),
			Size: fileInfo.Size(),
		}

		if fileInfo.IsDir() {
			appFile.Sha1 = "0"
			appFile.Size = 0
		} else {
			sha, err := appfiles.shaFile(fullPath)
			if err != nil {
				return err
			}
			appFile.Sha1 = sha
		}

		appFiles = append(appFiles, appFile)

		return nil
	})

	return appFiles, toplevelErr
}