MCPcopy
hub / github.com/sajari/docconv / NewLocalFile

Function NewLocalFile

local.go:20–42  ·  view source on GitHub ↗

NewLocalFile ensures that there is a file which contains the data provided by r. If r is actually an instance of *os.File then this file is used, otherwise a temporary file is created and the data from r copied into it. Callers must call Done() when the LocalFile is no longer needed to ensure all

(r io.Reader)

Source from the content-addressed store, hash-verified

18// created and the data from r copied into it. Callers must call Done() when
19// the LocalFile is no longer needed to ensure all resources are cleaned up.
20func NewLocalFile(r io.Reader) (*LocalFile, error) {
21 if f, ok := r.(*os.File); ok {
22 return &LocalFile{
23 File: f,
24 }, nil
25 }
26
27 f, err := os.CreateTemp(os.TempDir(), "docconv")
28 if err != nil {
29 return nil, fmt.Errorf("error creating temporary file: %v", err)
30 }
31 _, err = io.Copy(f, r)
32 if err != nil {
33 f.Close()
34 os.Remove(f.Name())
35 return nil, fmt.Errorf("error copying data into temporary file: %v", err)
36 }
37
38 return &LocalFile{
39 File: f,
40 unlink: true,
41 }, nil
42}
43
44// Done cleans up all resources.
45func (l *LocalFile) Done() {

Callers 6

ConvertPDFFunction · 0.85
ConvertDocFunction · 0.85
ConvertPDFFunction · 0.85
ConvertRTFFunction · 0.85
TestNewLocalFileFunction · 0.85
ConvertImageFunction · 0.85

Calls 1

CloseMethod · 0.80

Tested by 1

TestNewLocalFileFunction · 0.68