From d3a603a209af58b44909a1aa238b12206545d798 Mon Sep 17 00:00:00 2001 From: Nemo Date: Sun, 29 Mar 2020 19:34:51 +0530 Subject: [PATCH] Safe filenames --- README.md | 15 ++++++--------- src/muse-dl.cr | 9 +++++++-- src/parser.cr | 9 ++++++++- src/pdftk.cr | 6 +++--- src/util.cr | 8 ++++++++ 5 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/util.cr diff --git a/README.md b/README.md index f9a9ee3..b77aa0d 100644 --- a/README.md +++ b/README.md @@ -7,15 +7,12 @@ TODO ## Usage ``` -muse-dl --help - -USAGE: muse-dl [FLAGS] URL - -FLAGS: - --no-bookmarks: Don't add bookmarks - --no-cleanup: Don't cleanup temporary files - --tmp-dir /path: Use /path as temporary directory - --output file.pdf: Use file.pdf as output filename +Usage: muse-dl [--flags] URL + --no-cleanup Don't cleanup temporary files + --tmp-dir PATH Temporary Directory to use + --output FILE Output Filename + --no-bookmarks Don't add bookmarks in the PDF + -h, --help Show this help ``` ## License diff --git a/src/muse-dl.cr b/src/muse-dl.cr index 05d4ca7..764fcd1 100644 --- a/src/muse-dl.cr +++ b/src/muse-dl.cr @@ -3,22 +3,27 @@ require "./pdftk.cr" require "./fetch.cr" require "./book.cr" require "./journal.cr" +require "./util.cr" -# TODO: Write documentation for `Muse::Dl` module Muse::Dl VERSION = "0.1.0" - # TODO: Put your code here class Main def self.run(args : Array(String)) parser = Parser.new(args) thing = Fetch.get_info(parser.url) if thing.is_a? Muse::Dl::Book + # Will have no effect if parser has a custom title + parser.output = Util.slug_filename "#{thing.title}.pdf" + + # Save each chapter thing.chapters.each do |chapter| Fetch.save_chapter(parser.tmp, chapter[0], chapter[1], parser.bookmarks) end chapter_ids = thing.chapters.map { |c| c[0] } + + # Stitch the PDFs together pdf_builder = Pdftk.new(parser.tmp) temp_stitched_file = pdf_builder.stitch chapter_ids pdf_builder.add_metadata(temp_stitched_file, parser.output, thing) diff --git a/src/parser.cr b/src/parser.cr index e447952..752ad32 100644 --- a/src/parser.cr +++ b/src/parser.cr @@ -6,11 +6,18 @@ module Muse::Dl @bookmarks = true @tmp : String @cleanup = true - @output = "tempfilename.pdf" + @output = DEFAULT_FILE_NAME @url = "INVALID_URL" + DEFAULT_FILE_NAME = "tempfilename.pdf" + getter :bookmarks, :tmp, :cleanup, :output, :url + # Update the output filename unless we have a custom one passed + def output=(output_file : String) + @output = output_file unless @output != DEFAULT_FILE_NAME + end + def find_next(arg : Array(String), flag : String, default) search = arg.index flag if search diff --git a/src/pdftk.cr b/src/pdftk.cr index 885cbb8..0cd0355 100644 --- a/src/pdftk.cr +++ b/src/pdftk.cr @@ -61,19 +61,19 @@ module Muse::Dl InfoValue: Project MUSE (https://muse.jhu.edu/) InfoBegin InfoKey: Producer - InfoValue: Muse-DL + InfoValue: Muse-DL/#{Muse::Dl::VERSION} InfoBegin InfoKey: Title InfoValue: #{book.title} InfoBegin InfoKey: Keywords - InfoValue: Publisher: #{book.publisher}, Published #{book.date} + InfoValue: Publisher:#{book.publisher}, Published:#{book.date} InfoBegin InfoKey: Author InfoValue: #{book.author} InfoBegin InfoKey: Subject - InfoValue: #{book.summary.gsub("\n", " ")} + InfoValue: #{book.summary.gsub(/\n\s+/, " ")} EOT # Known Info keys, if they are present diff --git a/src/util.cr b/src/util.cr new file mode 100644 index 0000000..5ed414a --- /dev/null +++ b/src/util.cr @@ -0,0 +1,8 @@ +module Muse::Dl + class Util + # Generates a safe filename + def self.slug_filename(input : String) + input.strip.tr("\u{202E}%$|:;/\t\r\n\\", "-") + end + end +end