qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
18.43k stars 1.73k forks source link

Flexability in temporary folder in StreamWriter files #2024

Open eliyaoo32 opened 2 weeks ago

eliyaoo32 commented 2 weeks ago

The StreamWriter feature in Excelize is great for handling large XLSX file generation. Currently, temporary files are created using Golang's os.CreateTemp, as seen in stream.go#L740.

In some cases, relying on os.CreateTemp is not flexible enough. Specifically, there are scenarios where it's preferable to direct these temporary files to different or dynamically determined storage locations based on the request context. This restriction can limit the usage of StreamWriter in more complex environments.

I propose adding an option in StreamWriter to allow users to provide a custom temporary file creation method. This would follow the options pattern, maintaining the current behavior as the default. The user would be able to specify their own TempFileFactory, allowing for dynamic control over the temporary file path.

For example:

type TempFileFactory func() (*os.File, error)

var DefaultTempFileFactory TempFileFactory = func() {
    return os.CreateTemp(os.TempDir(), "excelize-")
}

// Line 740:
bw.tmp, err = newTempFile();

I have no problem to contribue this solution for the library if there is an agreement on a solution.

xuri commented 2 weeks ago

Thanks for your suggestion. The temporary file not only used in stream writer, but also in stream reader (rows iterator), you can set TMPDIR environment variable for excelize application in the runtime to changed temporary file location, does this make sense? If we decide expose this for the user, add new fields TempDir in the Options data type would be better.

eliyaoo32 commented 2 weeks ago

Using the TMPDIR environment makes sense if it's fine to have all the same temporary file under the same folder. But sometimes there are needs to put the temporary files under different places for different context. For example, use different mount/harddisk for different clients or utillizing different disks based on their free-space at the moment. Management of this logic is impossible throguh env variable since the variable is shared between threads.

Adding a new field TempDir as optional data is a great option as well. (Less flexable but easier)

xuri commented 2 weeks ago

I suggest check the optional value TempDir if exist when open or create workbook, contribution are welcome.

eliyaoo32 commented 2 weeks ago

Sure, I'll work on a contribution for it.