Lofter1/anyflip-downloader

"The system cannot find the path specified" and flag errors

Closed this issue · 3 comments

Description

  • The script fails to convert downloaded data into pdf when the title of the anyflip's book contains an apostrophe (f.ex "Wayfinder's"). When the error occurs the script prints the line stating:
    date time open Wayfinder's.pdf: The system cannot find the path specified."

  • The error doesn't occur when the title doesn't contain an apostrophe and pdf is successfully generated from the downloaded data.

  • Setting the title manually with the -title flag doesn't seem to work. I attempted to set a different title with this flag, but it never worked for me for some reason. My input commands were:
    anyflip-downloader url -title MM5
    or
    anyflip-downloader url -title SKT

  • Similarly, -temp-download-folder flag didn't work for me neither. My input command was:
    anyflip-downloader url -temp-download-folder Wayfinder

OS: Windows 10 22H2

To Reproduce
Install, using the PowerShell command provided in the code.
Input the command line, such as:
anyflip-downloader https://anyflip.com/ybvc/hgch -title MM5
or
anyflip-downloader https://anyflip.com/ybvc/zlfd/ -temp-download-folder Eberron

Expected behavior
Conversion of downloaded data into a pdf even if the title of the downloaded book in anyflip contains an apostrophe.
Title is changed to the one specified by the flag.
Temporary folder name is changed to the one specified by the flag.

Hi! @Lofter1 I don't really know how to contribute, but you need to sanitize the name of the PDF when converting in main.go, since it has some issues with special characters like ' and . If you add a function that deletes those characters (and any others that might give you problems when converting to PDF), and then sanitize the name of the output file, it works like a charm:

func createPDF(outputFile string, imageDir string) error {
    pdf := itopdf.NewInstance()
    err := pdf.WalkDir(imageDir, nil)
    if err != nil {
        return err
    }

    // Sanitize the output file name
    outputFile = sanitizeFileName(outputFile)

    err = pdf.Save(outputFile)
    if err != nil {
        return err
    }
    return nil
}

func sanitizeFileName(fileName string) string {
    // Replace invalid characters in file names
    fileName = strings.ReplaceAll(fileName, "'", "")
    fileName = strings.ReplaceAll(fileName, "\\", "")

    return fileName
}

I added sanitisation to the convertPDF method now.

Concerning the title not being set: I made a mistake when writing the README. Gos standard flag library cares for the position of flags. They should come before the first positional argument (so the book URL in this case). Move the URL to the end of the command and it should work. I'll probably should add a better usage message that describes the usage correctly, too.

Thank you for the quick fix!