Skip to content

Commit

Permalink
Change LocalFile to embed *os.File.
Browse files Browse the repository at this point in the history
This is to support ConvertDoc when falls back to Docx (and so needs to Seek back to the beginning of the file).
  • Loading branch information
dhowden committed Aug 9, 2015
1 parent e1b0968 commit b4319ff
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions local.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"os"
)

// LocalFile is a type which wraps an *os.File. See NewLocalFile for more details.
type LocalFile struct {
name string
*os.File

unlink bool
}

Expand All @@ -19,7 +21,7 @@ type LocalFile struct {
func NewLocalFile(r io.Reader, dir, prefix string) (*LocalFile, error) {
if f, ok := r.(*os.File); ok {
return &LocalFile{
name: f.Name(),
File: f,
}, nil
}

Expand All @@ -28,25 +30,21 @@ func NewLocalFile(r io.Reader, dir, prefix string) (*LocalFile, error) {
return nil, fmt.Errorf("error creating temporary file: %v", err)
}
_, err = io.Copy(f, r)
f.Close()
if err != nil {
f.Close()
os.Remove(f.Name())
return nil, fmt.Errorf("error copying data into temporary file: %v", err)
}

return &LocalFile{
name: f.Name(),
File: f,
unlink: true,
}, nil
}

// Name returns the path to the file.
func (l *LocalFile) Name() string {
return l.name
}

// Done cleans up all resources.
func (l *LocalFile) Done() {
l.Close()
if l.unlink {
os.Remove(l.Name())
}
Expand Down

0 comments on commit b4319ff

Please sign in to comment.