summaryrefslogtreecommitdiff
path: root/cmd/rescribe/main.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2024-02-06 08:17:24 +0000
committerNick White <git@njw.name>2024-02-06 15:11:22 +0000
commitd2399a7ce0af49098a065c48409d1512f37c7c21 (patch)
treebe8d15fd3631ca3a0fe6e1f76e8146abadc933ac /cmd/rescribe/main.go
parentf9199fe32b894757bce1065ac99dec6a8ce65f89 (diff)
Fix selecting a custom training file in flatpak
This is done by copying any training to the temporary tesseract directory, and always using that as the TESSDIR. This works as it's writeable (unlike the /app/share directory that flatpak would otherwise work)
Diffstat (limited to 'cmd/rescribe/main.go')
-rw-r--r--cmd/rescribe/main.go32
1 files changed, 27 insertions, 5 deletions
diff --git a/cmd/rescribe/main.go b/cmd/rescribe/main.go
index b33a66c..f532678 100644
--- a/cmd/rescribe/main.go
+++ b/cmd/rescribe/main.go
@@ -241,11 +241,33 @@ These training files are included in rescribe, and are always available:
}
}
- // if trainingPath doesn't exist, set it to the embedded training instead
- _, err = os.Stat(trainingPath)
- if err != nil && !os.IsExist(err) {
- trainingPath = filepath.Base(trainingPath)
- trainingPath = filepath.Join(tessdatadir, trainingPath)
+ // copy training path to the tessdir directory, so that we can keep that a
+ // writeable space, which is needed opening other trainings in sandboxes
+ // like flatpak
+ in, err := os.Open(trainingPath)
+ trainingPath = filepath.Join(tessdatadir, filepath.Base(trainingPath))
+ if err != nil {
+ in, err = os.Open(trainingPath)
+ if err != nil {
+ log.Fatalf("Error opening training file %s: %v", trainingPath, err)
+ }
+ }
+ defer in.Close()
+ newPath := trainingPath + ".new"
+ out, err := os.Create(newPath)
+ if err != nil {
+ log.Fatalf("Error creating training file %s: %v", newPath, err)
+ }
+ defer out.Close()
+ _, err = io.Copy(out, in)
+ if err != nil {
+ log.Fatalf("Error copying training file to %s: %v", newPath, err)
+ }
+ in.Close()
+ out.Close()
+ err = os.Rename(newPath, trainingPath)
+ if err != nil {
+ log.Fatalf("Error moving new training file to %s: %v", trainingPath, err)
}
abstraining, err := filepath.Abs(trainingPath)