Example #1
0
func makeFolderByDate(target string, date time.Time) (string, error) {
	dateString := date.Format("2006-01-02")
	folderPath := filepath.Join(target, dateString)

	if pcopylib.IsFileExist(folderPath) != pcopylib.FileExistStatus_Directory {
		os.Mkdir(folderPath, os.ModePerm|os.ModeDir)
	}

	if pcopylib.IsFileExist(folderPath) != pcopylib.FileExistStatus_Directory {
		return "", errors.New("pclassify: error: make folder by month failed")
	}

	return folderPath, nil
}
Example #2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	if err := parseArgs(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	sourceStatus := pcopylib.IsFileExist(source)
	if sourceStatus == pcopylib.FileExistStatus_NotExist {
		fmt.Println(shortUsage(fmt.Sprintf("pcopy: error: %s: No such file or directory", source)))
		os.Exit(1)
	}

	if sourceStatus == pcopylib.FileExistStatus_File {
		if err := pcopylib.CopyFile(source, target, moveMode, fullHashMode); err != nil {
			fmt.Println(shortUsage(fmt.Sprint(err)))
			os.Exit(1)
		}
	} else {
		if err := pcopylib.CopyDirectory(source, target, moveMode, fullHashMode, recursiveMode); err != nil {
			fmt.Println(shortUsage(fmt.Sprint(err)))
			os.Exit(1)
		}
	}
}
Example #3
0
func makeFolderByBirthday(target string, date time.Time, file string) (string, error) {
	birthday := time.Date(2011, 3, 16, 13, 12, 30, 0, time.Local)

	deltaYear := date.Year() - birthday.Year()
	deltaMonth := date.Month() - birthday.Month()

	monthAfterBirth := int(deltaYear)*12 + int(deltaMonth)
	if date.Day() >= 16 {
		monthAfterBirth += 1
	}

	if monthAfterBirth < 0 {
		return "", errors.New("pclassify: error: the date photo taken is earlier than birthday")
	}

	yearTag := monthAfterBirth / 12
	monthTag := monthAfterBirth % 12
	if monthTag == 0 {
		yearTag -= 1
		monthTag = 12
	}

	dateString := ""
	extName := strings.ToLower(filepath.Ext(file))
	switch {
	case extName == ".jpg" || extName == ".cr2":
		dateString = fmt.Sprintf("%d岁%d月照", yearTag, monthTag)
	case extName == ".mp4" || extName == ".mov" || extName == ".3gp":
		dateString = fmt.Sprintf("%d岁%d月视频", yearTag, monthTag)
	}

	folderPath := filepath.Join(target, dateString)

	if pcopylib.IsFileExist(folderPath) != pcopylib.FileExistStatus_Directory {
		os.Mkdir(folderPath, os.ModePerm|os.ModeDir)
	}

	if pcopylib.IsFileExist(folderPath) != pcopylib.FileExistStatus_Directory {
		return "", errors.New("pclassify: error: make folder by month failed")
	}

	return folderPath, nil
}
Example #4
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	if err := parseArgs(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if pcopylib.IsFileExist(source) != pcopylib.FileExistStatus_Directory {
		fmt.Println(shortUsage(fmt.Sprintf("pclassify: error: %s: No such directory", source)))
		os.Exit(1)
	}

	if pcopylib.IsFileExist(target) != pcopylib.FileExistStatus_Directory {
		fmt.Println(shortUsage(fmt.Sprintf("pclassify: error: %s: No such directory", target)))
		os.Exit(1)
	}

	jobsNum := 1
	if !copyMode {
		jobsNum = 20
	}

	classifyJob := make(chan string, jobsNum)
	classifyDone := make(chan struct{}, jobsNum)

	for i := 0; i < jobsNum; i++ {
		go func(classifyDone chan<- struct{}, classifyJob <-chan string) {
			for file := range classifyJob {
				classify(file, target, copyMode, fullHashMode, classifyMode)
			}

			classifyDone <- struct{}{}
		}(classifyDone, classifyJob)
	}

	filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
		if source == path {
			return nil
		}

		if info.IsDir() {
			return filepath.SkipDir
		}

		extName := strings.ToLower(filepath.Ext(path))
		if extName != ".jpg" && extName != ".cr2" && extName != ".mp4" && extName != ".mov" && extName != ".3gp" {
			return nil
		}

		classifyJob <- path

		return nil
	})

	close(classifyJob)

	for i := 0; i < jobsNum; i++ {
		<-classifyDone
	}
}