From a000b5d069e4fdf781c5d4fa3f6b8a29653b5684 Mon Sep 17 00:00:00 2001 From: guangwu Date: Wed, 13 Sep 2023 12:33:58 +0800 Subject: [PATCH] all: remove refs to deprecated io/ioutil (#140) --- doc.go | 3 +-- docconv.go | 3 +-- docx.go | 3 +-- html_appengine.go | 4 ++-- html_test/html_test.go | 4 ++-- local.go | 3 +-- odt.go | 3 +-- pages.go | 5 ++--- pdf_ocr.go | 3 +-- pptx.go | 3 +-- rtf_test/rtf_test.go | 4 ++-- snappy/snappy_test.go | 9 ++++----- tidy.go | 3 +-- 13 files changed, 20 insertions(+), 30 deletions(-) diff --git a/doc.go b/doc.go index 9b8b137..d9bce28 100644 --- a/doc.go +++ b/doc.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -77,7 +76,7 @@ func ConvertDoc(r io.Reader) (string, map[string]string, error) { // Save output to a file var buf bytes.Buffer - outputFile, err := ioutil.TempFile("/tmp", "sajari-convert-") + outputFile, err := os.CreateTemp("/tmp", "sajari-convert-") if err != nil { // TODO: Remove this. log.Println("TempFile Out:", err) diff --git a/docconv.go b/docconv.go index eab82b2..bd30dae 100755 --- a/docconv.go +++ b/docconv.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "path" "strings" @@ -98,7 +97,7 @@ func Convert(r io.Reader, mimeType string, readability bool) (*Response, error) case "text/plain": var b []byte - b, err = ioutil.ReadAll(r) + b, err = io.ReadAll(r) body = string(b) } diff --git a/docx.go b/docx.go index f9f276a..9e96107 100644 --- a/docx.go +++ b/docx.go @@ -6,7 +6,6 @@ import ( "encoding/xml" "fmt" "io" - "io/ioutil" "os" "time" ) @@ -40,7 +39,7 @@ func ConvertDocx(r io.Reader) (string, map[string]string, error) { size = si.Size() ra = f } else { - b, err := ioutil.ReadAll(io.LimitReader(r, maxBytes)) + b, err := io.ReadAll(io.LimitReader(r, maxBytes)) if err != nil { return "", nil, nil } diff --git a/html_appengine.go b/html_appengine.go index c6fb19a..1f2337d 100644 --- a/html_appengine.go +++ b/html_appengine.go @@ -1,15 +1,15 @@ +//go:build appengine // +build appengine package docconv import ( "io" - "io/ioutil" "log" ) func HTMLReadability(r io.Reader) []byte { - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { log.Printf("HTMLReadability: %v", err) return nil diff --git a/html_test/html_test.go b/html_test/html_test.go index 33a9755..f381ce7 100644 --- a/html_test/html_test.go +++ b/html_test/html_test.go @@ -3,7 +3,7 @@ package html_test import ( "bytes" "fmt" - "io/ioutil" + "os" "testing" "github.com/google/go-cmp/cmp" @@ -39,7 +39,7 @@ This is a full sentence. }) docconv.HTMLReadabilityOptionsValues.ReadabilityUseClasses = tt.readabilityUseClasses - data, err := ioutil.ReadFile("testdata/test.html") + data, err := os.ReadFile("testdata/test.html") must(t, err) got, _, err := docconv.ConvertHTML(bytes.NewReader(data), true) must(t, err) diff --git a/local.go b/local.go index 739115e..02e6a62 100644 --- a/local.go +++ b/local.go @@ -3,7 +3,6 @@ package docconv import ( "fmt" "io" - "io/ioutil" "os" ) @@ -25,7 +24,7 @@ func NewLocalFile(r io.Reader) (*LocalFile, error) { }, nil } - f, err := ioutil.TempFile(os.TempDir(), "docconv") + f, err := os.CreateTemp(os.TempDir(), "docconv") if err != nil { return nil, fmt.Errorf("error creating temporary file: %v", err) } diff --git a/odt.go b/odt.go index ad93d07..a45fbe7 100644 --- a/odt.go +++ b/odt.go @@ -5,7 +5,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "time" ) @@ -14,7 +13,7 @@ func ConvertODT(r io.Reader) (string, map[string]string, error) { meta := make(map[string]string) var textBody string - b, err := ioutil.ReadAll(io.LimitReader(r, maxBytes)) + b, err := io.ReadAll(io.LimitReader(r, maxBytes)) if err != nil { return "", nil, err } diff --git a/pages.go b/pages.go index 54a2b03..d5cfbd8 100644 --- a/pages.go +++ b/pages.go @@ -7,7 +7,6 @@ import ( "encoding/binary" "fmt" "io" - "io/ioutil" "strings" "google.golang.org/protobuf/proto" @@ -21,7 +20,7 @@ func ConvertPages(r io.Reader) (string, map[string]string, error) { meta := make(map[string]string) var textBody string - b, err := ioutil.ReadAll(io.LimitReader(r, maxBytes)) + b, err := io.ReadAll(io.LimitReader(r, maxBytes)) if err != nil { return "", nil, fmt.Errorf("error reading data: %v", err) } @@ -57,7 +56,7 @@ func ConvertPages(r io.Reader) (string, map[string]string, error) { // Ignore error. // NOTE: This error was unchecked. Need to revisit this to see if it // should be acted on. - archiveInfoData, _ := ioutil.ReadAll(io.LimitReader(bReader, archiveLength)) + archiveInfoData, _ := io.ReadAll(io.LimitReader(bReader, archiveLength)) archiveInfo := &TSP.ArchiveInfo{} err = proto.Unmarshal(archiveInfoData, archiveInfo) diff --git a/pdf_ocr.go b/pdf_ocr.go index a05c943..6fcd5ea 100644 --- a/pdf_ocr.go +++ b/pdf_ocr.go @@ -6,7 +6,6 @@ package docconv import ( "fmt" "io" - "io/ioutil" "log" "os" "os/exec" @@ -39,7 +38,7 @@ func cleanupTemp(tmpDir string) { func ConvertPDFImages(path string) (BodyResult, error) { bodyResult := BodyResult{} - tmp, err := ioutil.TempDir(os.TempDir(), "tmp-imgs-") + tmp, err := os.MkdirTemp(os.TempDir(), "tmp-imgs-") if err != nil { bodyResult.err = err return bodyResult, err diff --git a/pptx.go b/pptx.go index a0cb839..c79979c 100644 --- a/pptx.go +++ b/pptx.go @@ -5,7 +5,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "strings" ) @@ -28,7 +27,7 @@ func ConvertPptx(r io.Reader) (string, map[string]string, error) { size = si.Size() ra = f } else { - b, err := ioutil.ReadAll(r) + b, err := io.ReadAll(r) if err != nil { return "", nil, nil } diff --git a/rtf_test/rtf_test.go b/rtf_test/rtf_test.go index 59858d1..7985f56 100644 --- a/rtf_test/rtf_test.go +++ b/rtf_test/rtf_test.go @@ -2,7 +2,7 @@ package rtf_test import ( "bytes" - "io/ioutil" + "os" "strings" "testing" @@ -10,7 +10,7 @@ import ( ) func TestConvertRTF(t *testing.T) { - data, err := ioutil.ReadFile("testdata/test.rtf") + data, err := os.ReadFile("testdata/test.rtf") if err != nil { t.Fatalf("got error %v, want nil", err) } diff --git a/snappy/snappy_test.go b/snappy/snappy_test.go index 940f79a..923bab7 100644 --- a/snappy/snappy_test.go +++ b/snappy/snappy_test.go @@ -9,7 +9,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "math/rand" "net/http" "os" @@ -119,7 +118,7 @@ func TestFramingFormat(t *testing.T) { if _, err := NewWriter(buf).Write(src); err != nil { t.Fatalf("Write: encoding: %v", err) } - dst, err := ioutil.ReadAll(NewReader(buf)) + dst, err := io.ReadAll(NewReader(buf)) if err != nil { t.Fatalf("ReadAll: decoding: %v", err) } @@ -149,7 +148,7 @@ func TestReaderReset(t *testing.T) { continue } r.Reset(strings.NewReader(s)) - got, err := ioutil.ReadAll(r) + got, err := io.ReadAll(r) switch s { case encoded: if err != nil { @@ -186,7 +185,7 @@ func TestWriterReset(t *testing.T) { failed = true continue } - got, err := ioutil.ReadAll(NewReader(buf)) + got, err := io.ReadAll(NewReader(buf)) if err != nil { t.Errorf("#%d: ReadAll: %v", i, err) failed = true @@ -229,7 +228,7 @@ func benchEncode(b *testing.B, src []byte) { } func readFile(b testing.TB, filename string) []byte { - src, err := ioutil.ReadFile(filename) + src, err := os.ReadFile(filename) if err != nil { b.Fatalf("failed reading %s: %s", filename, err) } diff --git a/tidy.go b/tidy.go index c4475dc..e0f23a7 100644 --- a/tidy.go +++ b/tidy.go @@ -2,7 +2,6 @@ package docconv import ( "io" - "io/ioutil" "os" "os/exec" ) @@ -11,7 +10,7 @@ import ( // Errors & warnings are deliberately suppressed as underlying tools // throw warnings very easily. func Tidy(r io.Reader, xmlIn bool) ([]byte, error) { - f, err := ioutil.TempFile(os.TempDir(), "docconv") + f, err := os.CreateTemp(os.TempDir(), "docconv") if err != nil { return nil, err }