docstore/pkg/scan/helper.go
2019-08-25 12:34:10 +02:00

39 lines
682 B
Go

// Copyright (C) 2019 Marius Schellenberger
package scan
import (
"net/http"
"os"
"path/filepath"
"strings"
)
const (
pdfContentType = "application/pdf"
maxSniffSize = 512
)
func isPDF(path string) bool {
// TODO
// return checkContentType(path) || checkExtension(path)
return checkContentType(path)
}
func checkContentType(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
}
return http.DetectContentType(buf) == pdfContentType
}
func checkExtension(path string) bool {
return strings.ToLower(filepath.Ext(path)) == pdfExt
}