Skip to content

Commit

Permalink
clean up empty file on PDF receive fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Evidlo committed Feb 5, 2025
1 parent 196ee37 commit a3ca613
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func handleRequest(conn net.Conn) {
line, err := reader.ReadString('\n')
if err == io.EOF {
fmt.Println("Couldn't find PDF start")
// Clean up
os.Remove(pdf_path)
os.Exit(1)
}
check(err)
Expand All @@ -161,24 +163,57 @@ func handleRequest(conn net.Conn) {
break
}
}
last := ""
// Read until end of PDF


// ----------
// line: 116

// linelen: 5
// err: <nil>
// ----------
// line: %%EOF

// linelen: 7
// err: <nil>
// ----------
// line:
// linelen: 0
// err: EOF
// Couldn't find PDF end


// ----------
// line: 924148

// linelen: 7
// err: <nil>
// ----------
// line: %%EOF
// linelen: 5
// err: EOF
// Saving metadata to /tmp/49ea3977-acfa-4789-908e-fae620f6c617.metadata
// Saving content file to /tmp/49ea3977-acfa-4789-908e-fae620f6c617.content


last := ""
for {
line, err := reader.ReadString('\n')
// end of pdf file
if (err == io.EOF) {
if strings.HasPrefix(last, "%%EOF") {
if strings.HasPrefix(line, "%%EOF") || strings.HasPrefix(last, "%%EOF") {
_, err = f.WriteString(line)
f.Close()
break
} else {
debug("Couldn't find PDF end")
os.Remove(pdf_path)
os.Exit(1)
}
}
check(err)
_, err = f.WriteString(line)
if (len(line) > 0) {
if len(line) > 0 {
last = line
}
}
Expand Down

0 comments on commit a3ca613

Please sign in to comment.