some improvements

This commit is contained in:
ston1th 2019-08-26 19:58:31 +02:00
commit 58a0dc36d5
25 changed files with 233 additions and 105 deletions

View file

@ -3,24 +3,52 @@
package scan
import (
"bytes"
"net/http"
"os"
"path/filepath"
"strings"
)
const (
pdfContentType = "application/pdf"
maxSniffSize = 512
pdfT = "application/pdf"
jpegT = "image/jpeg"
pngT = "image/png"
bmpT = "image/bmp"
gifT = "image/gif"
webpT = "image/webp"
pnmT = "image/x-portable-anymap"
jp2T = "image/jp2"
tiffT = "image/tiff"
maxSniffSize = 512
)
func isPDF(path string) bool {
// TODO
// return checkContentType(path) || checkExtension(path)
return checkContentType(path)
type extendedSig struct {
sig []byte
ct string
}
func checkContentType(path string) bool {
var extendedSigs = []extendedSig{
{[]byte("P1\x0A"), pnmT},
{[]byte("P2\x0A"), pnmT},
{[]byte("P3\x0A"), pnmT},
{[]byte("P4\x0A"), pnmT},
{[]byte("P5\x0A"), pnmT},
{[]byte("P6\x0A"), pnmT},
{[]byte("P7\x0A"), pnmT},
{[]byte("\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A\x00\x00\x00\x14\x66\x74\x79\x70\x6A\x70\x32"), jp2T},
{[]byte("\x4d\x4d\x00\x2a"), tiffT},
{[]byte("\x49\x49\x2a\x00"), tiffT},
}
func extendedContentType(buf []byte) string {
for _, sig := range extendedSigs {
if bytes.HasPrefix(buf, sig.sig) {
return sig.ct
}
}
return "application/octet-stream"
}
func isPDF(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
@ -31,9 +59,28 @@ func checkContentType(path string) bool {
if err != nil {
return false
}
return http.DetectContentType(buf) == pdfContentType
return http.DetectContentType(buf) == pdfT
}
func checkExtension(path string) bool {
return strings.ToLower(filepath.Ext(path)) == pdfExt
func isImage(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
buf := make([]byte, maxSniffSize)
_, err = f.Read(buf)
if err != nil {
return false
}
switch http.DetectContentType(buf) {
case jpegT, pngT, bmpT, gifT, webpT:
return true
default:
switch extendedContentType(buf) {
case pnmT, jp2T, tiffT:
return true
}
}
return false
}

View file

@ -106,6 +106,10 @@ func (s *Scanner) Scan(file string) (filename, text string, err error) {
var pdffile string
pdf := isPDF(scanfile)
if !pdf {
if !isImage(scanfile) {
err = errors.New("error: input file is not an image")
return
}
var (
pdf []byte
f *os.File