comicbook 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/comic_book/cb.rb CHANGED
@@ -9,28 +9,28 @@ class ComicBook
9
9
  Archiver.new(path).archive options
10
10
  end
11
11
 
12
- def info
13
- xml_path = File.join path, 'ComicInfo.xml'
14
- return nil unless File.exist? xml_path
15
-
16
- ComicInfo.load xml_path
17
- end
18
-
19
12
  def extract _options = {}
20
13
  Extractor.new(path).extract
21
14
  end
22
15
 
23
- def pages
24
- pattern = ComicBook::IMAGE_GLOB_PATTERN
25
- search_path = File.join path, '**', pattern
26
- image_files = Dir.glob search_path, File::FNM_CASEFOLD
27
-
28
- image_files.sort.map do |file|
29
- relative_path = Pathname.new(file).relative_path_from(Pathname.new(path)).to_s
30
- basename = File.basename file
16
+ # Every file in the folder, as Entries with folder-relative paths
17
+ # (hidden files and macOS metadata are left out)
18
+ def entries
19
+ files = Dir.glob(File.join(path, '**', '*')).reject { File.directory? it }
31
20
 
32
- ComicBook::Page.new relative_path, basename
21
+ entries = files.map do |file|
22
+ relative = Pathname.new(file).relative_path_from(Pathname.new(path)).to_s
23
+ ComicBook::Entry.new relative
33
24
  end
25
+
26
+ entries.reject(&:macos_metadata?)
27
+ end
28
+
29
+ def info
30
+ xml_path = File.join path, 'ComicInfo.xml'
31
+ return nil unless File.exist? xml_path
32
+
33
+ ComicInfo.load xml_path
34
34
  end
35
35
  end
36
36
  end
@@ -8,9 +8,12 @@ class ComicBook
8
8
  def archive options = {}
9
9
  extension = options.fetch :extension, :cb7
10
10
  delete_original = options.fetch :delete_original, false
11
+ contents = options.fetch :contents, :all
11
12
 
12
13
  output_path = options[:to] || determine_output_path(extension)
13
- create_archive output_path
14
+ entries = ComicBook.new(source_folder).files(type: contents)
15
+
16
+ create_archive output_path, entries
14
17
  cleanup_source_folder if delete_original
15
18
 
16
19
  output_path
@@ -27,27 +30,16 @@ class ComicBook
27
30
  File.expand_path File.join(dir_name, "#{base_name}.#{extension}")
28
31
  end
29
32
 
30
- def create_archive output_path
33
+ def create_archive output_path, entries
31
34
  File.open(output_path, 'wb') do |file|
32
35
  SevenZipRuby::Writer.open(file) do |writer|
33
- find_image_files.each do |image_file|
34
- add_file writer, image_file
35
- end
36
+ entries.each { add_entry writer, it }
36
37
  end
37
38
  end
38
39
  end
39
40
 
40
- def find_image_files
41
- pattern = File.join(source_folder, '**', ComicBook::IMAGE_GLOB_PATTERN)
42
- Dir.glob(pattern, File::FNM_CASEFOLD).sort
43
- end
44
-
45
- def add_file writer, file
46
- file_path = Pathname.new file
47
- source_path = Pathname.new source_folder
48
- relative_path = file_path.relative_path_from source_path
49
-
50
- writer.add_file file, as: relative_path.to_s
41
+ def add_entry writer, entry
42
+ writer.add_file File.join(source_folder, entry.path), as: entry.path
51
43
  end
52
44
 
53
45
  def cleanup_source_folder
@@ -63,6 +63,7 @@ class ComicBook
63
63
  def extract_files destination, options, seven_zip_reader
64
64
  seven_zip_reader.entries.each do |entry|
65
65
  next unless entry.file?
66
+ next if ComicBook::Entry.new(entry.path).macos_metadata?
66
67
  next if options[:images_only] && !image_file?(entry.path)
67
68
 
68
69
  extract_single_file entry, destination, seven_zip_reader
@@ -14,49 +14,33 @@ class ComicBook
14
14
  Extractor.new(path).extract options
15
15
  end
16
16
 
17
- def info
18
- xml = nil
17
+ def entries
18
+ names = []
19
19
 
20
20
  File.open(path, 'rb') do |file|
21
21
  SevenZipRuby::Reader.open(file) do |reader|
22
- entry = reader.entries.find { it.path == 'ComicInfo.xml' }
23
- xml = reader.extract_data(entry.index) if entry
22
+ reader.entries.each do |entry|
23
+ names << entry.path if entry.file?
24
+ end
24
25
  end
25
26
  end
26
27
 
27
- return nil unless xml
28
-
29
- ComicInfo.load xml
28
+ names.map { ComicBook::Entry.new it }.reject(&:macos_metadata?)
30
29
  end
31
30
 
32
- def pages
33
- entries = []
31
+ def info
32
+ xml = nil
34
33
 
35
34
  File.open(path, 'rb') do |file|
36
35
  SevenZipRuby::Reader.open(file) do |reader|
37
- reader.entries.each do |entry|
38
- entries << entry.path if entry.file?
39
- end
36
+ entry = reader.entries.find { it.path == 'ComicInfo.xml' }
37
+ xml = reader.extract_data(entry.index) if entry
40
38
  end
41
39
  end
42
40
 
43
- entries.select { image_file? it }
44
- .map { create_page_from_entry it }
45
- .sort_by(&:name)
46
- end
47
-
48
- private
49
-
50
- def create_page_from_entry entry
51
- basename = File.basename entry
52
-
53
- ComicBook::Page.new entry, basename
54
- end
55
-
56
- def image_file? filename
57
- extension = File.extname filename.downcase
41
+ return nil unless xml
58
42
 
59
- ComicBook::IMAGE_EXTENSIONS.include? extension
43
+ ComicInfo.load xml
60
44
  end
61
45
  end
62
46
  end
@@ -57,9 +57,21 @@ class ComicBook
57
57
 
58
58
  def extract_files destination, options
59
59
  CLIHelpers.unar_extract archive_path, destination
60
+ delete_macos_metadata destination
60
61
  delete_non_images destination if options[:images_only]
61
62
  end
62
63
 
64
+ # unar extracts the whole archive, so what macOS added is removed afterwards.
65
+ def delete_macos_metadata destination
66
+ archive_entries = CLIHelpers.lsar_list archive_path
67
+
68
+ archive_entries.each do |entry|
69
+ next unless ComicBook::Entry.new(entry).macos_metadata?
70
+
71
+ FileUtils.rm_rf File.join(destination, entry)
72
+ end
73
+ end
74
+
63
75
  def delete_non_images destination
64
76
  archive_entries = CLIHelpers.lsar_list archive_path
65
77
 
@@ -13,6 +13,10 @@ class ComicBook
13
13
  Extractor.new(path).extract options
14
14
  end
15
15
 
16
+ def entries
17
+ CLIHelpers.lsar_list(path).map { ComicBook::Entry.new it }.reject(&:macos_metadata?)
18
+ end
19
+
16
20
  def info
17
21
  entries = CLIHelpers.lsar_list path
18
22
  return nil unless entries.include? 'ComicInfo.xml'
@@ -25,26 +29,5 @@ class ComicBook
25
29
  ComicInfo.load xml_path
26
30
  end
27
31
  end
28
-
29
- def pages
30
- CLIHelpers.lsar_list(path)
31
- .select { image_file? it }
32
- .map { create_page_from_entry it }
33
- .sort_by(&:name)
34
- end
35
-
36
- private
37
-
38
- def create_page_from_entry entry
39
- basename = File.basename entry
40
-
41
- ComicBook::Page.new entry, basename
42
- end
43
-
44
- def image_file? filename
45
- extension = File.extname filename.downcase
46
-
47
- ComicBook::IMAGE_EXTENSIONS.include? extension
48
- end
49
32
  end
50
33
  end
@@ -8,9 +8,12 @@ class ComicBook
8
8
  def archive options = {}
9
9
  extension = options.fetch :extension, :cbt
10
10
  delete_original = options.fetch :delete_original, false
11
+ contents = options.fetch :contents, :all
11
12
 
12
13
  output_path = options[:to] || determine_output_path(extension)
13
- create_archive output_path
14
+ entries = ComicBook.new(source_folder).files(type: contents)
15
+
16
+ create_archive output_path, entries
14
17
  cleanup_source_folder if delete_original
15
18
 
16
19
  output_path
@@ -27,31 +30,19 @@ class ComicBook
27
30
  File.expand_path File.join(dir_name, "#{base_name}.#{extension}")
28
31
  end
29
32
 
30
- def create_archive output_path
33
+ def create_archive output_path, entries
31
34
  File.open(output_path, 'wb') do |file|
32
35
  Gem::Package::TarWriter.new(file) do |writer|
33
- find_image_files.each do |image_file|
34
- add_file writer, image_file
35
- end
36
+ entries.each { add_entry writer, it }
36
37
  end
37
38
  end
38
39
  end
39
40
 
40
- def find_image_files
41
- pattern = File.join(source_folder, '**', ComicBook::IMAGE_GLOB_PATTERN)
42
- Dir.glob(pattern, File::FNM_CASEFOLD).sort
43
- end
44
-
45
- def add_file writer, file
46
- file_path = Pathname.new file
47
- source_path = Pathname.new source_folder
48
- relative_path = file_path.relative_path_from source_path
41
+ def add_entry writer, entry
42
+ absolute = File.join source_folder, entry.path
49
43
 
50
- stat = File.stat file
51
- writer.add_file(relative_path.to_s, stat.mode) do |io|
52
- File.open(file, 'rb') do |f|
53
- io.write f.read
54
- end
44
+ writer.add_file(entry.path, File.stat(absolute).mode) do |io|
45
+ io.write File.binread(absolute)
55
46
  end
56
47
  end
57
48
 
@@ -61,6 +61,7 @@ class ComicBook
61
61
  def extract_files destination, options, tar
62
62
  tar.each do |entry|
63
63
  next unless entry.file?
64
+ next if ComicBook::Entry.new(entry.full_name).macos_metadata?
64
65
  next if options[:images_only] && !image_file?(entry.full_name)
65
66
 
66
67
  extract_single_file entry, destination
@@ -14,53 +14,37 @@ class ComicBook
14
14
  Extractor.new(path).extract options
15
15
  end
16
16
 
17
- def info
18
- xml = nil
17
+ def entries
18
+ names = []
19
19
 
20
20
  File.open(path, 'rb') do |file|
21
21
  Gem::Package::TarReader.new(file) do |reader|
22
22
  reader.each do |entry|
23
- next unless entry.full_name == 'ComicInfo.xml'
24
-
25
- xml = entry.read
26
- break
23
+ names << entry.full_name if entry.file?
27
24
  end
28
25
  end
29
26
  end
30
27
 
31
- return nil unless xml
32
-
33
- ComicInfo.load xml
28
+ names.map { ComicBook::Entry.new it }.reject(&:macos_metadata?)
34
29
  end
35
30
 
36
- def pages
37
- entries = []
31
+ def info
32
+ xml = nil
38
33
 
39
34
  File.open(path, 'rb') do |file|
40
35
  Gem::Package::TarReader.new(file) do |reader|
41
36
  reader.each do |entry|
42
- entries << entry.full_name if entry.file?
37
+ next unless entry.full_name == 'ComicInfo.xml'
38
+
39
+ xml = entry.read
40
+ break
43
41
  end
44
42
  end
45
43
  end
46
44
 
47
- entries.select { image_file? it }
48
- .map { create_page_from_entry it }
49
- .sort_by(&:name)
50
- end
51
-
52
- private
53
-
54
- def create_page_from_entry entry
55
- basename = File.basename entry
56
-
57
- ComicBook::Page.new entry, basename
58
- end
59
-
60
- def image_file? filename
61
- extension = File.extname filename.downcase
45
+ return nil unless xml
62
46
 
63
- ComicBook::IMAGE_EXTENSIONS.include? extension
47
+ ComicInfo.load xml
64
48
  end
65
49
  end
66
50
  end
@@ -8,9 +8,12 @@ class ComicBook
8
8
  def archive options = {}
9
9
  extension = options.fetch :extension, :cbz
10
10
  delete_original = options.fetch :delete_original, false
11
+ contents = options.fetch :contents, :all
11
12
 
12
13
  output_path = options[:to] || determine_output_path(extension)
13
- create_archive output_path
14
+ entries = ComicBook.new(source_folder).files(type: contents)
15
+
16
+ create_archive output_path, entries
14
17
  cleanup_source_folder if delete_original
15
18
 
16
19
  output_path
@@ -27,25 +30,14 @@ class ComicBook
27
30
  File.expand_path File.join(dir_name, "#{base_name}.#{extension}")
28
31
  end
29
32
 
30
- def create_archive output_path
33
+ def create_archive output_path, entries
31
34
  Zip::File.open(output_path, create: true) do |writer|
32
- find_image_files.each do |file|
33
- add_file writer, file
34
- end
35
+ entries.each { add_entry writer, it }
35
36
  end
36
37
  end
37
38
 
38
- def find_image_files
39
- pattern = File.join(source_folder, '**', ComicBook::IMAGE_GLOB_PATTERN)
40
- Dir.glob(pattern, File::FNM_CASEFOLD).sort
41
- end
42
-
43
- def add_file writer, file
44
- file_path = Pathname.new file
45
- source_path = Pathname.new source_folder
46
- relative_path = file_path.relative_path_from source_path
47
-
48
- writer.add relative_path.to_s, file
39
+ def add_entry writer, entry
40
+ writer.add entry.path, File.join(source_folder, entry.path)
49
41
  end
50
42
 
51
43
  def cleanup_source_folder
@@ -63,6 +63,7 @@ class ComicBook
63
63
  Zip::File.open(archive_path) do |zipfile|
64
64
  zipfile.each do |entry|
65
65
  next if entry.directory?
66
+ next if ComicBook::Entry.new(entry.name).macos_metadata?
66
67
  next if options[:images_only] && !image_file?(entry.name)
67
68
 
68
69
  extract_single_file entry, destination_basename
@@ -14,43 +14,27 @@ class ComicBook
14
14
  Extractor.new(path).extract options
15
15
  end
16
16
 
17
- def info
18
- xml = nil
17
+ def entries
18
+ names = []
19
19
 
20
20
  Zip::File.open(path) do |zipfile|
21
- entry = zipfile.find_entry('ComicInfo.xml')
22
- xml = entry&.get_input_stream&.read
21
+ zipfile.each { |entry| names << entry.name if entry.file? }
23
22
  end
24
23
 
25
- return nil unless xml
26
-
27
- ComicInfo.load xml
24
+ names.map { ComicBook::Entry.new it }.reject(&:macos_metadata?)
28
25
  end
29
26
 
30
- def pages
31
- entries = []
27
+ def info
28
+ xml = nil
32
29
 
33
30
  Zip::File.open(path) do |zipfile|
34
- zipfile.each { |entry| entries << entry.name }
31
+ entry = zipfile.find_entry('ComicInfo.xml')
32
+ xml = entry&.get_input_stream&.read
35
33
  end
36
34
 
37
- entries.select { image_file? it }
38
- .map { create_page_from_entry it }
39
- .sort_by(&:name)
40
- end
41
-
42
- private
43
-
44
- def create_page_from_entry entry
45
- basename = File.basename entry
46
-
47
- ComicBook::Page.new entry, basename
48
- end
49
-
50
- def image_file? filename
51
- extension = File.extname filename.downcase
35
+ return nil unless xml
52
36
 
53
- ComicBook::IMAGE_EXTENSIONS.include? extension
37
+ ComicInfo.load xml
54
38
  end
55
39
  end
56
40
  end
@@ -0,0 +1,29 @@
1
+ class ComicBook
2
+ # A single member of a comic book: an image, an info file
3
+ # (ComicInfo.xml / MetronInfo.xml), or anything else.
4
+ #
5
+ # `path` locates it within the comic:
6
+ # a relative path in a folder,
7
+ # or an entry name in an archive
8
+ # `name` is its basename.
9
+ class Entry
10
+ attr_reader :path, :name
11
+
12
+ def initialize path
13
+ @path = path
14
+ @name = File.basename path
15
+ end
16
+
17
+ def image? = ComicBook::IMAGE_EXTENSIONS.include?(File.extname(name).downcase)
18
+ def info? = ComicBook::INFO_FILENAMES.include?(name)
19
+
20
+ # True if macOS added this to an archive, so it is not part of the comic:
21
+ # an AppleDouble member (`._page1.jpg`, which tar writes beside a file
22
+ # that has extended attributes),
23
+ # or anything in a `__MACOSX` folder (where Finder's Compress puts the same data).
24
+ # Such members are never listed, counted as pages, or extracted.
25
+ def macos_metadata?
26
+ path.split('/').any? { it == '__MACOSX' || it.start_with?('._') }
27
+ end
28
+ end
29
+ end
@@ -1,10 +1,10 @@
1
1
  class ComicBook
2
2
  class Page
3
- attr_reader :path, :name
3
+ attr_reader :name, :path
4
4
 
5
5
  def initialize path, name
6
- @path = path
7
6
  @name = name
7
+ @path = path
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,95 @@
1
+ require 'comicinfo'
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+
5
+ class ComicBook
6
+ # Makes a folder's page images exactly the given files, in the given order.
7
+ #
8
+ # See ComicBook#arrange_pages, which this does the work for.
9
+ class PageArranger
10
+ # An issue with no details, for filling in a template when the folder has no ComicInfo.xml.
11
+ BLANK_ISSUE_XML = '<ComicInfo/>'.freeze
12
+
13
+ def initialize folder
14
+ @folder = File.realpath folder
15
+ end
16
+
17
+ def arrange sources, template:
18
+ previous_pages = comic.pages.map(&:path)
19
+ resolved = sources.map { resolve it, previous_pages }
20
+ files = resolved.map { it[:path] }
21
+ order = resolved.map { it[:previous_index] }
22
+
23
+ Dir.mktmpdir 'comicbook-arrange' do |staging|
24
+ staged = stage files, staging, template
25
+ replace_pages previous_pages, staged
26
+ end
27
+
28
+ update_info order
29
+ comic.pages
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :folder
35
+
36
+ # Delete the pages the folder had, and move the staged ones into where they were.
37
+ def replace_pages previous_pages, staged
38
+ previous_pages.each { FileUtils.rm File.join(folder, it) }
39
+
40
+ pages_directory = File.join folder, shared_directory(previous_pages)
41
+ FileUtils.mkdir_p pages_directory
42
+ staged.each { FileUtils.mv it, File.join(pages_directory, File.basename(it)) }
43
+ end
44
+
45
+ def comic = ComicBook.new(folder)
46
+
47
+ def info_path = File.join(folder, 'ComicInfo.xml')
48
+
49
+ # Where a source is on disk, and which page it was before (nil for a file new to the comic).
50
+ def resolve source, previous_pages
51
+ path = source.start_with?('/') ? source : File.join(folder, source)
52
+
53
+ raise Error, "Path does not exist: #{path}" unless File.exist? path
54
+ raise Error, "Unsupported file type: #{File.extname(path)}" unless Entry.new(path).image?
55
+
56
+ path = File.realpath path
57
+ folder_prefix = "#{folder}/"
58
+ previous_index = previous_pages.index(path.delete_prefix(folder_prefix)) if path.start_with? folder_prefix
59
+
60
+ { path:, previous_index: }
61
+ end
62
+
63
+ # Copy the files into the staging folder under their new page names, in order.
64
+ def stage files, staging, template
65
+ issue = File.exist?(info_path) ? ComicInfo.load(info_path) : ComicInfo.load(BLANK_ISSUE_XML)
66
+
67
+ files.each_with_index.map do |file, index|
68
+ name = issue.page_filename template:, page: index + 1, of: files.length
69
+ destination = File.join staging, "#{name}#{File.extname(file)}"
70
+
71
+ FileUtils.cp file, destination
72
+ destination
73
+ end
74
+ end
75
+
76
+ # The deepest directory that contains every one of the paths, relative like them ("" for the top).
77
+ def shared_directory paths
78
+ directories = paths.map { it.split('/')[0...-1] }
79
+ shared = directories.first || []
80
+
81
+ directories.drop(1).each do |directory|
82
+ shared = shared.zip(directory).take_while { |ours, theirs| ours == theirs }.map(&:first)
83
+ end
84
+
85
+ shared.join '/'
86
+ end
87
+
88
+ # Bring the folder's ComicInfo.xml (if any) in step with the rearranged pages.
89
+ def update_info order
90
+ return unless File.exist? info_path
91
+
92
+ ComicInfo.load(info_path).rearranging_pages(order).save info_path
93
+ end
94
+ end
95
+ end
@@ -3,10 +3,6 @@ require_relative 'pdf/extractor'
3
3
 
4
4
  class ComicBook
5
5
  class PDF < Adapter
6
- def info
7
- nil
8
- end
9
-
10
6
  def archive _options = {}
11
7
  raise Error, 'PDF archiving not supported (use extract to convert PDF pages to images)'
12
8
  end
@@ -16,6 +12,8 @@ class ComicBook
16
12
  Extractor.new(path).extract options
17
13
  end
18
14
 
15
+ def info = nil
16
+
19
17
  def pages
20
18
  require_vips!
21
19
  image = Vips::Image.new_from_file path
@@ -0,0 +1,60 @@
1
+ class ComicBook
2
+ # Tells what kind of file a comic is from the bytes it starts with.
3
+ #
4
+ # Comic files are often mislabelled (a RAR named `.cbz`),
5
+ # so what the file is matters more than what it is called.
6
+ class Signature
7
+ # How many leading bytes are enough to recognize any format
8
+ # (TAR's marker ends at byte 262).
9
+ LENGTH = 262
10
+
11
+ ZIP_FILE_HEADER = "PK\x03\x04".b.freeze
12
+ ZIP_END_RECORD = "PK\x05\x06".b.freeze # an empty archive starts with this
13
+ RAR = "Rar!\x1A\x07".b.freeze # versions 4 and 5
14
+ SEVEN_ZIP = "7z\xBC\xAF\x27\x1C".b.freeze
15
+ PDF = '%PDF-'.b.freeze
16
+ ACE = '**ACE**'.b.freeze
17
+ ACE_OFFSET = 7
18
+ TAR = 'ustar'.b.freeze
19
+ TAR_OFFSET = 257
20
+
21
+ def initialize path
22
+ @path = path
23
+ end
24
+
25
+ # The type the file's contents say it is (:cbz, :cbr, :cb7, :pdf, :cba, :cbt).
26
+ #
27
+ # Nil when the file can't be read or starts like none of the formats;
28
+ # an old TAR with no `ustar` marker is one such file.
29
+ def type
30
+ return :cbz if zip?
31
+ return :cbr if starts_with? RAR
32
+ return :cb7 if starts_with? SEVEN_ZIP
33
+ return :pdf if starts_with? PDF
34
+ return :cba if marker? ACE, at: ACE_OFFSET
35
+ return :cbt if marker? TAR, at: TAR_OFFSET
36
+
37
+ nil
38
+ end
39
+
40
+ private
41
+
42
+ attr_reader :path
43
+
44
+ def zip? = starts_with?(ZIP_FILE_HEADER) || starts_with?(ZIP_END_RECORD)
45
+
46
+ def starts_with? marker
47
+ marker? marker, at: 0
48
+ end
49
+
50
+ def marker? marker, at:
51
+ bytes.byteslice(at, marker.bytesize) == marker
52
+ end
53
+
54
+ def bytes
55
+ @bytes ||= File.binread(path, LENGTH) || ''.b
56
+ rescue SystemCallError
57
+ @bytes = ''.b
58
+ end
59
+ end
60
+ end