Skip to content

Commit 55e152f

Browse files
authored
This closes #2024, add TempDir field in the Options data type (#2024) (#2163)
1 parent d434acd commit 55e152f

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

excelize.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,16 @@ type charsetTranscoderFn func(charset string, input io.Reader) (rdr io.Reader, e
9898
//
9999
// CultureInfo specifies the country code for applying built-in language number
100100
// format code these effect by the system's local language settings.
101+
//
102+
// TmpDir specifies the temporary directory for creating temporary files, if the
103+
// value is empty, the system default temporary directory will be used.
101104
type Options struct {
102105
MaxCalcIterations uint
103106
Password string
104107
RawCellValue bool
105108
UnzipSizeLimit int64
106109
UnzipXMLSizeLimit int64
110+
TmpDir string
107111
ShortDatePattern string
108112
LongDatePattern string
109113
LongTimePattern string

lib.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (f *File) ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) {
8080
// unzipToTemp unzip the zip entity to the system temporary directory and
8181
// returned the unzipped file path.
8282
func (f *File) unzipToTemp(zipFile *zip.File) (string, error) {
83-
tmp, err := os.CreateTemp("", "excelize-")
83+
tmp, err := os.CreateTemp(f.options.TmpDir, "excelize-")
8484
if err != nil {
8585
return "", err
8686
}

rows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func (f *File) getFromStringItem(index int) string {
371371
}()
372372
}
373373
f.sharedStringItem = [][]uint{}
374-
f.sharedStringTemp, _ = os.CreateTemp("", "excelize-")
374+
f.sharedStringTemp, _ = os.CreateTemp(f.options.TmpDir, "excelize-")
375375
f.tempFiles.Store(defaultTempFileSST, f.sharedStringTemp.Name())
376376
var (
377377
inElement string

stream.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error) {
123123
file: f,
124124
Sheet: sheet,
125125
SheetID: sheetID,
126+
rawData: bufferedWriter{tmpDir: f.options.TmpDir},
126127
}
127128
var err error
128129
sw.worksheet, err = f.workSheetReader(sheet)
@@ -737,8 +738,9 @@ func bulkAppendFields(w io.Writer, ws *xlsxWorksheet, from, to int) {
737738
// is written to the temp file with Sync, which may return an error.
738739
// Therefore, Sync should be periodically called and the error checked.
739740
type bufferedWriter struct {
740-
tmp *os.File
741-
buf bytes.Buffer
741+
tmpDir string
742+
tmp *os.File
743+
buf bytes.Buffer
742744
}
743745

744746
// Write to the in-memory buffer. The error is always nil.
@@ -775,7 +777,7 @@ func (bw *bufferedWriter) Sync() (err error) {
775777
return nil
776778
}
777779
if bw.tmp == nil {
778-
bw.tmp, err = os.CreateTemp("", "excelize-")
780+
bw.tmp, err = os.CreateTemp(bw.tmpDir, "excelize-")
779781
if err != nil {
780782
// can not use local storage
781783
return nil

stream_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestStreamWriter(t *testing.T) {
9797
assert.NoError(t, file.Close())
9898

9999
// Test close temporary file error
100-
file = NewFile()
100+
file = NewFile(Options{TmpDir: os.TempDir()})
101101
streamWriter, err = file.NewStreamWriter("Sheet1")
102102
assert.NoError(t, err)
103103
for rowID := 10; rowID <= 25600; rowID++ {

0 commit comments

Comments
 (0)