forked from kevin2li/PDF-Guru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
70 lines (65 loc) · 1.88 KB
/
encrypt.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
package main
import "os"
func (a *App) EncryptPDF(inFile string, outFile string, upw string, opw string, perm []string) error {
logger.Printf("inFile: %s, outFile: %s, upw: %s, opw: %s, perm: %v\n", inFile, outFile, upw, opw, perm)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"encrypt"}
if len(perm) > 0 {
args = append(args, "--perm")
args = append(args, perm...)
}
if upw != "" {
args = append(args, "--user_password", upw)
}
if opw != "" {
args = append(args, "--owner_password", opw)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) DecryptPDF(inFile string, outFile string, passwd string) error {
logger.Printf("inFile: %s, outFile: %s, passwd: %s\n", inFile, outFile, passwd)
if _, err := os.Stat(inFile); os.IsNotExist(err) {
logger.Errorln(err)
return err
}
args := []string{"decrypt"}
if passwd != "" {
args = append(args, "--password", passwd)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}
func (a *App) ChangePasswordPDF(inFile string, outFile string, oldUpw string, upw string, oldOpw string, opw string) error {
logger.Printf("inFile: %s, outFile: %s, oldUpw: %s, upw: %s, oldOpw: %s, opw: %s\n", inFile, outFile, oldUpw, upw, oldOpw, opw)
args := []string{"change_password"}
if oldUpw != "" {
args = append(args, "--old_user_password", oldUpw)
}
if upw != "" {
args = append(args, "--user_password", upw)
}
if oldOpw != "" {
args = append(args, "--old_owner_password", oldOpw)
}
if opw != "" {
args = append(args, "--owner_password", opw)
}
if outFile != "" {
args = append(args, "-o", outFile)
}
args = append(args, inFile)
logger.Println(args)
return a.cmdRunner(args, "pdf")
}