Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexability in temporary folder in StreamWriter files #2024

Open
eliyaoo32 opened this issue Nov 10, 2024 · 4 comments
Open

Flexability in temporary folder in StreamWriter files #2024

eliyaoo32 opened this issue Nov 10, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@eliyaoo32
Copy link

eliyaoo32 commented Nov 10, 2024

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
Copy link
Member

xuri commented Nov 11, 2024

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
Copy link
Author

eliyaoo32 commented Nov 11, 2024

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
Copy link
Member

xuri commented Nov 11, 2024

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

@eliyaoo32
Copy link
Author

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

@xuri xuri added the enhancement New feature or request label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants