-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscantopl.go
137 lines (121 loc) · 3.2 KB
/
scantopl.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
package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/jnovack/flag"
log "github.com/sirupsen/logrus"
)
var (
Version = "v1.0.0"
)
func FilenameWithoutExtension(fn string) string {
return strings.TrimSuffix(path.Base(fn), path.Ext(fn))
}
func TitleFromFileName(fn string) string {
return strings.TrimPrefix(FilenameWithoutExtension(fn), "pl_")
}
func createForm(form map[string]string) (string, io.Reader, error) {
body := new(bytes.Buffer)
mp := multipart.NewWriter(body)
defer mp.Close()
for key, val := range form {
if strings.HasPrefix(val, "@") {
val = val[1:]
file, err := os.Open(val)
if err != nil {
return "", nil, err
}
defer file.Close()
part, err := mp.CreateFormFile(key, val)
if err != nil {
return "", nil, err
}
io.Copy(part, file)
} else {
mp.WriteField(key, val)
}
}
return mp.FormDataContentType(), body, nil
}
func uploadFile(document, plurl, pltoken string) {
form := map[string]string{"document": "@" + document, "title": TitleFromFileName(document)}
ct, body, err := createForm(form)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest("POST", plurl+"/api/documents/post_document/", body)
req.Header.Set("Content-Type", ct)
req.Header.Set("Authorization", "Token "+pltoken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
log.Warn("Upload failed with response code: ", resp.StatusCode)
} else {
log.Info("Upload succesfully, remove file")
err := os.Remove(document)
if err != nil {
log.Warn(err)
}
}
}
func main() {
// OPTs
flag.String(flag.DefaultConfigFlagname, "", "path to config file")
scandir := flag.String("scandir", "/home/scanservjs/output", "Scanserjs ouput directory")
plurl := flag.String("plurl", "http://localhost:8080", "The paperless instance URL without trailing /")
pltoken := flag.String("pltoken", "xxxxxxxxxxxxxxxxxx", "Paperless auth token , generated through admin")
showversion := flag.Bool("version", false, "Show version and exit")
flag.Parse()
if *showversion {
fmt.Println("version", Version)
os.Exit(0)
}
// test if it's a directory
log.Println("Start watching:", *scandir)
// The watch loop
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
//log.Println("event:", event)
if event.Has(fsnotify.Create) && strings.HasPrefix(path.Base(event.Name), "pl_") {
//little pause to ensure the write operation is finished
time.Sleep(1 * time.Second)
log.Info("New file to upload:", event.Name)
uploadFile(event.Name, *plurl, *pltoken)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Warn("error:", err)
}
}
}()
//err = watcher.Add("/home/docker_data/scanservjs/output")
err = watcher.Add(*scandir)
if err != nil {
log.Fatal(err)
}
<-done
}