Skip to content

Instantly share code, notes, and snippets.

@avelican
Created October 12, 2024 14:25
Show Gist options
  • Save avelican/8602b417e810f8dd4e31e8e3fbbb41f0 to your computer and use it in GitHub Desktop.
Save avelican/8602b417e810f8dd4e31e8e3fbbb41f0 to your computer and use it in GitHub Desktop.
qpa mod
// edit: only convert if not already txt...
if (filepath.Ext(inputPath)) != ".txt" {
// Create a temporary file for the intermediate output
tmpFile, err := os.CreateTemp("", "ebook-convert-*.txt")
if err != nil {
return nil, fmt.Errorf("failed to create temporary file: %v", err)
}
defer tmpFile.Close()
c := color.New(color.Bold, color.FgMagenta)
c.Println("Converting " + filepath.Base(inputPath) + " to the proper intermediary text format...")
cmd := exec.Command("ebook-convert", inputPath, tmpFile.Name())
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to convert ebook: %s\nOutput: %s", err, string(output))
}
return tmpFile, nil
} else {
// return inputPath, nil
inputFile, err := os.Open(inputPath)
if err != nil {
return nil, fmt.Errorf("failed to open input file")
}
return inputFile, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment