Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
Add PhotoWalker
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Chen committed Apr 21, 2019
1 parent e6fd538 commit 806802d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
37 changes: 37 additions & 0 deletions PhotoWalker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Photo Walker

A simple photo(file) walker written in go.

## Usage

First download

```shell
$ go get -u github.com/iochen/GoSimpleProjects/tree/master/PhotoWalker
```

or download `main.go`

```shell
$ go run main.go
```



```
Usage of PhotoWalker:
-d string
The directory you want to walk. (default "./")
-e string
The extension(s) you want to match. (default "jpg,jpeg,png,raw")
-o string
The directory you want to output. (default "./output/")
-r If you want to remove same file in output directory.
```



## LICENSE

**MIT LICENSE**

107 changes: 107 additions & 0 deletions PhotoWalker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"crypto/sha1"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)

const sep = string(filepath.Separator)

func main() {
walkDir := flag.String("d", "."+sep, "The directory you want to walk.")
outDir := flag.String("o", "."+sep+"output"+sep, "The directory you want to output.")
ext := flag.String("e", "jpg,jpeg,png,raw", "The extension(s) you want to match.")
rmSame := flag.Bool("r", false, "If you want to remove same file in output directory.")
flag.Parse()

if (*outDir)[len(*outDir)-1] != filepath.Separator {
*outDir += sep
}

extExist := ExtSet(*ext)
WalkDir(*walkDir, *outDir, extExist)
if *rmSame {
RemoveSameFile(*outDir)
}
}

func ExtSet(ext string) map[string]bool {
extExist := make(map[string]bool)
extList := strings.Split(ext, ",")
for _, ext := range extList {
extExist["."+ext] = true
}
return extExist
}

func WalkDir(walkDir, outDir string, extExist map[string]bool) {
var pathList []string
err := filepath.Walk(walkDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
ext := filepath.Ext(path)
if !info.IsDir() && extExist[ext] {
pathList = append(pathList, path)
}
return nil
})
checkErr(err)
CopyFile(pathList, outDir)
}

func CopyFile(pathList []string, outDir string) {
if len(pathList) == 0 {
return
}
if _, err := os.Stat(outDir); os.IsNotExist(err) {
err = os.MkdirAll(outDir, 0755)
checkErr(err)
}
for _, path := range pathList {
fileName := filepath.Base(path)
bytes, err := ioutil.ReadFile(path)
checkErr(err)
err = ioutil.WriteFile(outDir+fileName, bytes, 0644)
checkErr(err)
}
}

func RemoveSameFile(workDir string) {
SHA1List := make(map[string]bool)
makeSHA1 := func(bytes *[]byte) string {
h := sha1.New()
_, err := io.WriteString(h, string(*bytes))
checkErr(err)
return fmt.Sprintf("%x", h.Sum(nil))
}
err := filepath.Walk(workDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
bytes, err := ioutil.ReadFile(path)
checkErr(err)
hash := makeSHA1(&bytes)
if SHA1List[hash] {
err := os.Remove(path)
checkErr(err)
} else {
SHA1List[hash] = true
}
return nil
})
checkErr(err)
}

func checkErr(err error) {
if err != nil {
log.Println(err)
}
}
2 changes: 1 addition & 1 deletion SimpleDownloader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A simple downloader written in go.

Use command:
```bash
$ go get -u iochen.com/simpledownloader
$ go get -u github.com/iochen/GoSimpleProjects/tree/master/SimpleDownloader
```
(or download from [releases](https://github.com/iochen/SimpleDownloader/releases))

Expand Down

0 comments on commit 806802d

Please sign in to comment.