xdamman / mediumexporter

Export your stories published on medium.com to markdown for easy import
MIT License
235 stars 32 forks source link

File naming issue on Windows - invalid characters in downloaded filenames #22

Closed ChristianWilkie closed 5 years ago

ChristianWilkie commented 5 years ago

I tried running the CLI to download one of my medium posts via the following command: node ./index.js https://medium.com/@ChristianWilkie/setting-up-a-free-ghost-blog-on-google-cloud-platform-and-cloudflare-c9bc79861a0e

I got an error:

PS C:\Users\chris\Desktop\medium\mediumexporter> node ./index.js https
://medium.com/@ChristianWilkie/setting-up-a-free-ghost-blog-on-google-cl
oud-platform-and-cloudflare-c9bc79861a0e
image https://cdn-images-1.medium.com/max/2000/1*4zonAsqpNEPQwT8XGNIKQQ.png 1*4zonAsqpNEPQwT8XGNIKQQ.png
something went wrong
{ Error: ENOENT: no such file or directory, open 'content\setting-up-a-free-ghost-blog-on-google-cloud-platform-and-cloudflare\images/1*4zonAsqpNEPQwT8XGNIKQQ.png'
    at Object.openSync (fs.js:438:3)
    at Object.writeFileSync (fs.js:1189:35)
    at Object.downloadImages (C:\Users\chris\Desktop\medium\mediumexporter\lib\utils.js:20:8)
    at process._tickCallback (internal/process/next_tick.js:68:7)
  errno: -4058,
  syscall: 'open',
  code: 'ENOENT',
  path:
   'content\\setting-up-a-free-ghost-blog-on-google-cloud-platform-and-cloudflare\\images/1*4zonAsqpNEPQwT8XGNIKQQ.png' }

It seems related to running it on windows and the '' in the filename of one of the images: https://cdn-images-1.medium.com/max/800/14zonAsqpNEPQwT8XGNIKQQ.png

In Windows it seems like the character isn't allowed in filenames. I noticed when I try to save it in chrome it replaces the with _

ex: 1*4zonAsqpNEPQwT8XGNIKQQ.png -> 1_4zonAsqpNEPQwT8XGNIKQQ.png

Here's a list of forbidden filename characters on Windows: https://docs.microsoft.com/en-us/windows/desktop/msi/filename

not really sure of the best solution, maybe you could use something like this: https://github.com/parshap/node-sanitize-filename

when I tried hacking it in it seemed to work:

"C:\Program Files\nodejs\node.exe" C:\Users\chris\Desktop\medium\mediumexporter\index.js https://medium.com/@ChristianWilkie/setting-up-a-free-ghost-blog-on-google-cloud-platform-and-cloudflare-c9bc79861a0e
image https://cdn-images-1.medium.com/max/2000/1*4zonAsqpNEPQwT8XGNIKQQ.png 14zonAsqpNEPQwT8XGNIKQQ.png
image https://cdn-images-1.medium.com/max/2000/1*6QfOFjGYVjI5JzaNh6Sf7Q.png 16QfOFjGYVjI5JzaNh6Sf7Q.png
image https://cdn-images-1.medium.com/max/2000/1*FftUAGHFGHTZLjDEXUpbGg.png 1FftUAGHFGHTZLjDEXUpbGg.png
image https://cdn-images-1.medium.com/max/2000/1*3oWKRejAS3HC3K_vu6j9uQ.png 13oWKRejAS3HC3K_vu6j9uQ.png
image https://cdn-images-1.medium.com/max/2000/1*yY558iqcpryitUVP2-oq-Q.png 1yY558iqcpryitUVP2-oq-Q.png
image https://cdn-images-1.medium.com/max/2360/1*-5FNUDRUkrGmfc896lVXzg.png 1-5FNUDRUkrGmfc896lVXzg.png
image https://cdn-images-1.medium.com/max/2000/1*xhHwY1Q6q27XLbxC1Vaowg.png 1xhHwY1Q6q27XLbxC1Vaowg.png
image https://cdn-images-1.medium.com/max/2236/1*F7IKa0s5r8zu47v7c08E2A.png 1F7IKa0s5r8zu47v7c08E2A.png
image https://cdn-images-1.medium.com/max/2104/1*VKNsP8qFpms0mx3YlcH3EQ.png 1VKNsP8qFpms0mx3YlcH3EQ.png
image https://cdn-images-1.medium.com/max/2082/1*gaGGKaWZx5_zmvac5w9UNg.png 1gaGGKaWZx5_zmvac5w9UNg.png
image https://cdn-images-1.medium.com/max/2000/1*AfNNmjD4lDP1ZNbZQVFnyw.png 1AfNNmjD4lDP1ZNbZQVFnyw.png
image https://cdn-images-1.medium.com/max/2000/1*26nHtpOvLVJo0PuCQLYaTQ.png 126nHtpOvLVJo0PuCQLYaTQ.png
image https://cdn-images-1.medium.com/max/2000/1*XRzEgDPazTjabVTjobPKNg.png 1XRzEgDPazTjabVTjobPKNg.png

I can add a PR with the module added in case it's helpful/for your review. I don't really write much nodejs at all so I dunno if what I wrote is terrible/buggy but it seemed to work when I tried it.

Thanks for your time! :)

ChristianWilkie commented 5 years ago

merged in! thanks :D