-
Notifications
You must be signed in to change notification settings - Fork 14
/
source.go
120 lines (96 loc) · 2.47 KB
/
source.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/labstack/gommon/log"
"github.com/unidoc/unidoc/pdf/creator"
)
type Mergeable interface {
MergeTo(c *creator.Creator) error
}
type source struct {
path, sourceType, mime, ext string
pages []int
}
// Initiate new source file from input argument
func NewSource(input string) Mergeable {
fileInputParts := strings.Split(input, "~")
path := fileInputParts[0]
var inputSource Mergeable
info, err := os.Stat(path)
if err != nil {
log.Fatal("Error:", err.Error())
}
switch mode := info.Mode(); {
case mode.IsDir():
inputSource = getMergeableDir(path)
case mode.IsRegular():
pages := []int{}
if len(fileInputParts) > 1 {
pages = parsePageNums(fileInputParts[1])
}
inputSource = getMergeableFile(path, pages)
}
return inputSource
}
func getMergeableDir(path string) Mergeable {
dir := DirSource{path: path}
dir.scanMergeables()
return dir
}
func getMergeableFile(path string, pages []int) Mergeable {
f, err := os.Open(path)
if err != nil {
log.Fatal("Cannot read source file:", path)
}
defer f.Close()
ext := filepath.Ext(f.Name())
mime, err := getMimeType(f)
if err != nil {
log.Fatal("Error in getting mime type of file:", path)
}
sourceType, err := getFileType(mime, ext)
if err != nil {
log.Fatal("Error : %s (%s)", err.Error(), path)
}
source := source{path, sourceType, mime, ext, pages}
var m Mergeable
switch sourceType {
case "image":
m = ImgSource{source}
case "pdf":
m = PDFSource{source}
}
return m
}
func getFileType(mime, ext string) (string, error) {
pdfExts := []string{".pdf", ".PDF"}
imgExts := []string{".jpg", ".jpeg", ".gif", ".png", ".tiff", ".tif", ".JPG", ".JPEG", ".GIF", ".PNG", ".TIFF", ".TIF"}
switch {
case mime == "application/pdf":
return "pdf", nil
case mime[:6] == "image/":
return "image", nil
case mime == "application/octet-stream" && in_array(ext, pdfExts):
return "pdf", nil
case mime == "application/octet-stream" && in_array(ext, imgExts):
return "image", nil
}
return "error", errors.New("File type not acceptable. ")
}
func parsePageNums(pagesInput string) []int {
pages := []int{}
for _, e := range strings.Split(pagesInput, ",") {
pageNo, err := strconv.Atoi(strings.Trim(e, " \n"))
if err != nil {
fmt.Errorf("Invalid format! Example of a file input with page numbers: path/to/abc.pdf~1,2,3,5,6")
os.Exit(1)
}
pages = append(pages, pageNo)
}
return pages
}