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)
| 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. |
| 20 | func 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. |
| 45 | func (l *LocalFile) Done() { |