Skip to content

Commit 3f30e91

Browse files
committed
Fixed linux multiple user permission issue for reading xlsx files.
1 parent 835de4b commit 3f30e91

File tree

1 file changed

+20
-0
lines changed
  • dataframe-excel/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io

1 file changed

+20
-0
lines changed

dataframe-excel/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/xlsx.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import org.apache.poi.ss.usermodel.Sheet
1313
import org.apache.poi.ss.usermodel.Workbook
1414
import org.apache.poi.ss.usermodel.WorkbookFactory
1515
import org.apache.poi.ss.util.CellReference
16+
import org.apache.poi.util.DefaultTempFileCreationStrategy
1617
import org.apache.poi.util.LocaleUtil
1718
import org.apache.poi.util.LocaleUtil.getUserTimeZone
19+
import org.apache.poi.util.TempFile
1820
import org.apache.poi.xssf.usermodel.XSSFWorkbook
1921
import org.jetbrains.kotlinx.dataframe.AnyFrame
2022
import org.jetbrains.kotlinx.dataframe.AnyRow
@@ -31,6 +33,7 @@ import java.io.File
3133
import java.io.InputStream
3234
import java.io.OutputStream
3335
import java.net.URL
36+
import java.nio.file.Files
3437
import java.time.LocalDate
3538
import java.time.LocalDateTime
3639
import java.util.Calendar
@@ -56,6 +59,20 @@ internal class DefaultReadExcelMethod(path: String?) : AbstractDefaultReadMethod
5659

5760
private const val readExcel = "readExcel"
5861

62+
/**
63+
* To prevent [Issue #402](https://github.com/Kotlin/dataframe/issues/402):
64+
*
65+
* Creates new temp directory instead of the default `/tmp/poifiles` which would
66+
* cause permission issues for multiple users.
67+
*/
68+
private fun setWorkbookTempDirectory() {
69+
val tempDir = Files.createTempDirectory("dataframe-excel").toFile()
70+
.also { it.deleteOnExit() }
71+
TempFile.setTempFileCreationStrategy(
72+
DefaultTempFileCreationStrategy(tempDir)
73+
)
74+
}
75+
5976
/**
6077
* @param sheetName sheet to read. By default, the first sheet in the document
6178
* @param columns comma separated list of Excel column letters and column ranges (e.g. “A:E” or “A,C,E:F”)
@@ -72,6 +89,7 @@ public fun DataFrame.Companion.readExcel(
7289
rowsCount: Int? = null,
7390
nameRepairStrategy: NameRepairStrategy = NameRepairStrategy.CHECK_UNIQUE,
7491
): AnyFrame {
92+
setWorkbookTempDirectory()
7593
val wb = WorkbookFactory.create(url.openStream())
7694
return wb.use { readExcel(wb, sheetName, skipRows, columns, rowsCount, nameRepairStrategy) }
7795
}
@@ -92,6 +110,7 @@ public fun DataFrame.Companion.readExcel(
92110
rowsCount: Int? = null,
93111
nameRepairStrategy: NameRepairStrategy = NameRepairStrategy.CHECK_UNIQUE,
94112
): AnyFrame {
113+
setWorkbookTempDirectory()
95114
val wb = WorkbookFactory.create(file)
96115
return wb.use { readExcel(it, sheetName, skipRows, columns, rowsCount, nameRepairStrategy) }
97116
}
@@ -129,6 +148,7 @@ public fun DataFrame.Companion.readExcel(
129148
rowsCount: Int? = null,
130149
nameRepairStrategy: NameRepairStrategy = NameRepairStrategy.CHECK_UNIQUE,
131150
): AnyFrame {
151+
setWorkbookTempDirectory()
132152
val wb = WorkbookFactory.create(inputStream)
133153
return wb.use { readExcel(it, sheetName, skipRows, columns, rowsCount, nameRepairStrategy) }
134154
}

0 commit comments

Comments
 (0)