// 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 { return checkContentType(path) || checkExtension(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 }