Skip to content

Commit

Permalink
Merge branch 'release/2.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jonghyeons committed Aug 31, 2022
2 parents 03c33bb + 8e1ba7e commit 9809baf
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2022, jonghyeons
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ JPG, RAW file sorter program in terminal written golang


### How to install

```bash
go get github.com/jonghyeons/photopic
```
OR
```bash
$ git clone https://github.com/jonghyeons/photopic.git
$ cd photopic
Expand All @@ -17,11 +20,14 @@ $ go install
### Usage

```bash
# Show help
$ photopic help

# Run in current filepath
$ photopic
$ photopic run

# Run another filepath
$ photopic YOURFILEPATH
$ photopic run YOUR_FILE_PATH
```


Expand Down
20 changes: 20 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "photopic",
Short: "Photopic is JPG, RAW file sorting program in terminal",
Long: `Photopic is JPG, RAW file sorting program in terminal`,
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
133 changes: 133 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package cmd

import (
"fmt"
"github.com/jonghyeons/photopic/utils"
"github.com/rwcarlsen/goexif/exif"
"github.com/spf13/cobra"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
)

var (
wideAngleLensCnt = 0
standardLensCnt = 0
telephotoLensCnt = 0
errCnt = 0
)

var runCmd = &cobra.Command{
Use: "run",
Short: "Classify JPEG and RAW files.",
Long: `Classify JPEG and RAW files.
Usage:
photopic run [filepath]`,
Run: func(cmd *cobra.Command, args []string) {
dir := ""
if len(args) != 0 {
dir = args[0]
} else {
var err error
dir, err = os.Getwd()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}

err := utils.MakeDir(dir)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if len(files) == 0 {
fmt.Println("empty directory")
os.Exit(1)
}

AnalysisExifAndMoveFile(dir, files)

fmt.Println("## It's done!")
fmt.Println("## Photopic Report")
fmt.Println("1. Lens Angle")
fmt.Println("# This data is analyzed only in the raw files.")
fmt.Println("wide-angle ", wideAngleLensCnt, "shot")
fmt.Println("standard ", standardLensCnt, "shot")
fmt.Println("telephoto ", telephotoLensCnt, "shot")
fmt.Println("analysis failure: ", errCnt)
},
}

func init() {
rootCmd.AddCommand(runCmd)
rootCmd.Flags().BoolP("directory", "d", false, "Specifies the directory. (default: current file path)")
}

func AnalysisExifAndMoveFile(dir string, files []fs.FileInfo) {
rawTypes := []string{"RAF", "CRW", "CR2", "CR3", "NEF", "NRW", "PEF", "DNG", "SRW", "ORF", "SRF", "SR2", "ARW", "RW2", "3FR", "DCR", "KDC", "MRW", "RWL", "DNG", "MOS", "X3F", "GPR"}

for _, file := range files {
fn := strings.Split(file.Name(), ".")
fileType := fn[len(fn)-1]

if fileType == "JPG" {
err := os.Rename(filepath.Join(dir, "/", file.Name()), filepath.Join(dir, "/jpg/", file.Name()))
if err != nil {
fmt.Println(file.Name(), err.Error())
}
} else if utils.Contains(rawTypes, fileType) {
err := SetLensAngleCnt(dir + "/" + file.Name())
if err != nil {
fmt.Println(file.Name(), err.Error())
}
err = os.Rename(filepath.Join(dir, "/", file.Name()), filepath.Join(dir, "/raw/", file.Name()))
if err != nil {
fmt.Println(file.Name(), err.Error())
}
}
}
}

func SetLensAngleCnt(fnameWithPath string) error {
f, err := os.Open(fnameWithPath)
if err != nil {
return err
}

exif.RegisterParsers()
x, err := exif.Decode(f)
if err != nil {
return err
}

focal, err := x.Get(exif.FocalLength)
if err != nil {
errCnt++
return err
}

numer, denom, err := focal.Rat2(0)
if err != nil {
errCnt++
return err
}

if numer/denom < 35 {
wideAngleLensCnt++
} else if numer/denom >= 35 || numer/denom < 85 {
standardLensCnt++
} else {
telephotoLensCnt++
}

return nil
}
70 changes: 70 additions & 0 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"fmt"
"github.com/jonghyeons/photopic/utils"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

// The image file must be located in the path(/sample).
func TestAnalysisExifAndMoveFile(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Errorf(err.Error())
}

dir = filepath.Join(dir, "/../sample")
err = utils.MakeDir(dir)
if err != nil {
t.Errorf(err.Error())
}

files, err := ioutil.ReadDir(dir)
if err != nil {
t.Errorf(err.Error())
}
if len(files) == 0 {
fmt.Println("empty directory")
t.Error()
}

type args struct {
dir string
files []fs.FileInfo
}
tests := []struct {
name string
args args
}{
{
name: "exif analysis test",
args: args{
dir: dir,
files: files,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AnalysisExifAndMoveFile(tt.args.dir, tt.args.files)
})
}

if wideAngleLensCnt > 0 || standardLensCnt > 0 || telephotoLensCnt > 0 || errCnt > 0 {
fmt.Println("## It's done!")
fmt.Println("## Photopic Report")
fmt.Println("1. Lens Angle")
fmt.Println("# This data is analyzed only in the raw files.")
fmt.Println("wide-angle ", wideAngleLensCnt, "shot")
fmt.Println("standard ", standardLensCnt, "shot")
fmt.Println("telephoto ", telephotoLensCnt, "shot")
fmt.Println("analysis failure: ", errCnt)
} else {
t.Error()
}
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/jonghyeons/photopic

go 1.12
go 1.13

require github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
require (
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
github.com/spf13/cobra v1.4.0
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Loading

0 comments on commit 9809baf

Please sign in to comment.