techprimate / TPPDF

TPPDF is a simple-to-use PDF builder for iOS and macOS written in Swift
https://techprimate.github.io/TPPDF/
MIT License
741 stars 125 forks source link

Allow generateURL() to sanitize the file name with replacement character #389

Closed icarus31 closed 2 months ago

icarus31 commented 2 months ago

How should the feature work?

When calling the generateURL() function, an extra parameter could be used to convert invalid character to that one. A default character (ex: "-") will be used if not provided.

What should it look like?

I guess a better solution would probably be to check for all invalid characters and replace them with the separator.

public func generateURL(filename: String, info: PDFInfo?, separator: String = "-") {
    let filenameUpdated = filename.replacingOccurrences(of: "/", with: separator)
    let url = FileManager.generateTemporaryOutputURL(for: updatedFilename)
    try generate(to: url, info: info)
    return url
}

For example, if the file name has a slash "/" character, "myfile/name" would be replaced by "myfile-name" if nothing has been provided.

What happened instead?

If nothing to replace, the current code stays the same

This is just a suggestion. I will now protect my self to avoid the crash.

philprime commented 2 months ago

Hi @icarus31, thanks for opening up this issue.

IIRC the filename is a parameter provided by the developer using the framework (in this case is you). Is there a specific reason why the sanitization of the file name should not happen outside of the framework, and become a responsibility of TPPDF?

IMHO using a filename without the directory separator / is general best practice and not a particular job of TPPDF, but I am open for discussion

icarus31 commented 1 month ago

You are right, please ignore that enhancement. Regards