Safe filenames

This commit is contained in:
Nemo 2020-03-29 19:34:51 +05:30
parent 11194d648b
commit d3a603a209
5 changed files with 32 additions and 15 deletions

View File

@ -7,15 +7,12 @@ TODO
## Usage ## Usage
``` ```
muse-dl --help Usage: muse-dl [--flags] URL
--no-cleanup Don't cleanup temporary files
USAGE: muse-dl [FLAGS] URL --tmp-dir PATH Temporary Directory to use
--output FILE Output Filename
FLAGS: --no-bookmarks Don't add bookmarks in the PDF
--no-bookmarks: Don't add bookmarks -h, --help Show this help
--no-cleanup: Don't cleanup temporary files
--tmp-dir /path: Use /path as temporary directory
--output file.pdf: Use file.pdf as output filename
``` ```
## License ## License

View File

@ -3,22 +3,27 @@ require "./pdftk.cr"
require "./fetch.cr" require "./fetch.cr"
require "./book.cr" require "./book.cr"
require "./journal.cr" require "./journal.cr"
require "./util.cr"
# TODO: Write documentation for `Muse::Dl`
module Muse::Dl module Muse::Dl
VERSION = "0.1.0" VERSION = "0.1.0"
# TODO: Put your code here
class Main class Main
def self.run(args : Array(String)) def self.run(args : Array(String))
parser = Parser.new(args) parser = Parser.new(args)
thing = Fetch.get_info(parser.url) thing = Fetch.get_info(parser.url)
if thing.is_a? Muse::Dl::Book 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| thing.chapters.each do |chapter|
Fetch.save_chapter(parser.tmp, chapter[0], chapter[1], parser.bookmarks) Fetch.save_chapter(parser.tmp, chapter[0], chapter[1], parser.bookmarks)
end end
chapter_ids = thing.chapters.map { |c| c[0] } chapter_ids = thing.chapters.map { |c| c[0] }
# Stitch the PDFs together
pdf_builder = Pdftk.new(parser.tmp) pdf_builder = Pdftk.new(parser.tmp)
temp_stitched_file = pdf_builder.stitch chapter_ids temp_stitched_file = pdf_builder.stitch chapter_ids
pdf_builder.add_metadata(temp_stitched_file, parser.output, thing) pdf_builder.add_metadata(temp_stitched_file, parser.output, thing)

View File

@ -6,11 +6,18 @@ module Muse::Dl
@bookmarks = true @bookmarks = true
@tmp : String @tmp : String
@cleanup = true @cleanup = true
@output = "tempfilename.pdf" @output = DEFAULT_FILE_NAME
@url = "INVALID_URL" @url = "INVALID_URL"
DEFAULT_FILE_NAME = "tempfilename.pdf"
getter :bookmarks, :tmp, :cleanup, :output, :url 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) def find_next(arg : Array(String), flag : String, default)
search = arg.index flag search = arg.index flag
if search if search

View File

@ -61,19 +61,19 @@ module Muse::Dl
InfoValue: Project MUSE (https://muse.jhu.edu/) InfoValue: Project MUSE (https://muse.jhu.edu/)
InfoBegin InfoBegin
InfoKey: Producer InfoKey: Producer
InfoValue: Muse-DL InfoValue: Muse-DL/#{Muse::Dl::VERSION}
InfoBegin InfoBegin
InfoKey: Title InfoKey: Title
InfoValue: #{book.title} InfoValue: #{book.title}
InfoBegin InfoBegin
InfoKey: Keywords InfoKey: Keywords
InfoValue: Publisher: #{book.publisher}, Published #{book.date} InfoValue: Publisher:#{book.publisher}, Published:#{book.date}
InfoBegin InfoBegin
InfoKey: Author InfoKey: Author
InfoValue: #{book.author} InfoValue: #{book.author}
InfoBegin InfoBegin
InfoKey: Subject InfoKey: Subject
InfoValue: #{book.summary.gsub("\n", " ")} InfoValue: #{book.summary.gsub(/\n\s+/, " ")}
EOT EOT
# Known Info keys, if they are present # Known Info keys, if they are present

8
src/util.cr Normal file
View File

@ -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