-
Notifications
You must be signed in to change notification settings - Fork 8
/
reformatdir.go
141 lines (127 loc) · 3.24 KB
/
reformatdir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package convertutil
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/grokify/simplego/image/imageutil"
"github.com/grokify/simplego/io/ioutilmore"
"github.com/pkg/errors"
)
const (
PDFSpecs = "1950pxw300dpi"
KindleSpecs = "600pxw300dpi"
)
type CopyType int
const (
PDFFormat CopyType = iota // 0 convert cli value
KindleFormat // 1 convert cli value
)
// ReformatImages converts images in one dir to another using default
// formats for Kindle and PDF.
func ReformatImages(baseSrcDir, baseOutDir string, copyType CopyType, rewrite bool) error {
var err error
baseSrcDir, err = filepath.Abs(strings.TrimSpace(baseSrcDir))
if err != nil {
return err
}
baseOutDir, err = filepath.Abs(strings.TrimSpace(baseOutDir))
if err != nil {
return err
}
return reformatImagesSubdir(baseSrcDir, baseOutDir, "", copyType, rewrite)
}
func reformatImagesSubdir(baseSrcDir, baseOutDir, dirPart string, copyType CopyType, rewrite bool) error {
thisSrcDir := baseSrcDir
thisOutDir := baseOutDir
dirPart = strings.TrimSpace(dirPart)
if len(dirPart) > 0 {
thisSrcDir = filepath.Join(thisSrcDir, dirPart)
thisOutDir = filepath.Join(thisOutDir, dirPart)
isDir, err := ioutilmore.IsDir(thisSrcDir)
if err != nil {
return err
}
if !isDir {
return fmt.Errorf("Need Dir [%s]", thisSrcDir)
}
}
if err := os.MkdirAll(thisOutDir, 0755); err != nil {
return err
}
sdirs, files, err := ioutilmore.ReadDirSplit(thisSrcDir, false)
if err != nil {
return err
}
for _, file := range files {
err := reformatImagesSubdirFile(
filepath.Join(thisSrcDir, file.Name()),
filepath.Join(thisOutDir, file.Name()),
copyType, rewrite)
if err != nil {
return err
}
/*
thisSrcFile := filepath.Join(thisSrcDir, file.Name())
thisOutFile := filepath.Join(thisOutDir, file.Name())
if !imageutil.IsImageExt(thisSrcFile) {
continue
}
if !rewrite {
isFile, err := ioutilmore.IsFile(thisOutFile)
if err == nil && isFile {
continue
}
}
switch copyType {
case PDFFormat:
_, _, err := ConvertToPDF(thisSrcFile, thisOutFile)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("ConvertToPDF failed for [%s]", thisSrcFile))
}
case KindleFormat:
_, stderr, err := ConvertToKindle(thisSrcFile, thisOutFile)
err = CheckError(err, stderr)
if err != nil {
return err
}
}
*/
}
for _, sdir := range sdirs {
subDir := sdir.Name()
if len(dirPart) > 0 {
subDir = filepath.Join(dirPart, subDir)
}
err := reformatImagesSubdir(baseSrcDir, baseOutDir, subDir, copyType, rewrite)
if err != nil {
return err
}
}
return nil
}
func reformatImagesSubdirFile(thisSrcFile, thisOutFile string, copyType CopyType, rewrite bool) error {
if !imageutil.IsImageExt(thisSrcFile) {
return nil
}
if !rewrite {
isFile, err := ioutilmore.IsFile(thisOutFile)
if err == nil && isFile {
return nil
}
}
switch copyType {
case PDFFormat:
_, _, err := ConvertToPDF(thisSrcFile, thisOutFile)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("ConvertToPDF failed for [%s]", thisSrcFile))
}
case KindleFormat:
_, stderr, err := ConvertToKindle(thisSrcFile, thisOutFile)
err = CheckError(err, stderr)
if err != nil {
return err
}
}
return nil
}