gubbins-fs 0.1.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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE +289 -0
  4. data/README.md +128 -0
  5. data/lib/fs/atomic_write.rb +121 -0
  6. data/lib/fs/concerns/argument_validation.rb +140 -0
  7. data/lib/fs/concerns/file_dsl_methods.rb +471 -0
  8. data/lib/fs/concerns/required_arguments.rb +141 -0
  9. data/lib/fs/directory/accessibility_check.rb +89 -0
  10. data/lib/fs/directory.rb +497 -0
  11. data/lib/fs/errors.rb +491 -0
  12. data/lib/fs/file.rb +1524 -0
  13. data/lib/fs/file_variant_builder.rb +269 -0
  14. data/lib/fs/file_variant_proxy.rb +253 -0
  15. data/lib/fs/files/csv.rb +26 -0
  16. data/lib/fs/files/gif.rb +10 -0
  17. data/lib/fs/files/image.rb +390 -0
  18. data/lib/fs/files/jpg.rb +36 -0
  19. data/lib/fs/files/json.rb +24 -0
  20. data/lib/fs/files/pdf.rb +231 -0
  21. data/lib/fs/files/pdf_collection.rb +64 -0
  22. data/lib/fs/files/png.rb +10 -0
  23. data/lib/fs/files/text.rb +13 -0
  24. data/lib/fs/files/tiff.rb +24 -0
  25. data/lib/fs/files/zip.rb +123 -0
  26. data/lib/fs/lockfile_sweeper.rb +70 -0
  27. data/lib/fs/tempfile_sweeper.rb +79 -0
  28. data/lib/fs/testing/mock/fs/configurator.rb +223 -0
  29. data/lib/fs/testing/mock/fs/dirs/directory.rb +41 -0
  30. data/lib/fs/testing/mock/fs/dsl.rb +81 -0
  31. data/lib/fs/testing/mock/fs/file.rb +60 -0
  32. data/lib/fs/testing/mock/fs/file_klass_mock.rb +350 -0
  33. data/lib/fs/testing/mock/fs/files/jpg.rb +14 -0
  34. data/lib/fs/testing/mock/fs/files/pdf.rb +19 -0
  35. data/lib/fs/testing/mock/fs/files/png.rb +12 -0
  36. data/lib/fs/testing/mock/fs/files/tiff.rb +12 -0
  37. data/lib/fs/testing/mock/fs/files/zip.rb +23 -0
  38. data/lib/fs/testing.rb +17 -0
  39. data/lib/fs/version.rb +3 -0
  40. data/lib/fs.rb +26 -0
  41. data/lib/gubbins/fs.rb +4 -0
  42. metadata +124 -0
@@ -0,0 +1,390 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/enumerable"
5
+ require "active_support/core_ext/object/blank"
6
+ require "active_support/core_ext/string/inflections"
7
+ require "shell/convert"
8
+ require "shell/file"
9
+ require "shell/identify"
10
+ require "shell/img2_pdf"
11
+ require "shell/magick"
12
+ require "shell/tesseract"
13
+
14
+ module Fs
15
+ module Files
16
+ class Image < Fs::File
17
+ # The black/white ratio range in which we consider a page
18
+ # to be "not empty"
19
+ #
20
+ # @return [Range]
21
+ BW_RATIO_RANGE = 1.5..98.5
22
+
23
+ CONVERT_FORMAT =
24
+ Proc.new do
25
+ get_path_from do |klass:|
26
+ path.sub_ext("." + klass.extensions.first)
27
+ end
28
+
29
+ create_with do |klass:, path:, content: nil, create_block: nil, **kargs|
30
+ if klass.is_a?(Fs::Files::PDF)
31
+ Shell::Img2PDF.convert(input: self.path, output: path, **kargs).raise_failures
32
+ else
33
+ Shell::Convert.exec(input: self.path, output: path, **kargs).raise_failures
34
+ end
35
+
36
+ klass.load(path: path)
37
+ end
38
+ end
39
+
40
+ encoding "binary"
41
+
42
+ variant(:png, "Fs::Files::PNG", &CONVERT_FORMAT)
43
+ variant(:jpg, "Fs::Files::JPG", &CONVERT_FORMAT)
44
+ variant(:tiff, "Fs::Files::TIFF", &CONVERT_FORMAT)
45
+ variant(:pdf, "Fs::Files::PDF", &CONVERT_FORMAT)
46
+
47
+ # Find the appropriate image subclass for `path`
48
+ #
49
+ # @param path [Pathname, Fs::File, String]
50
+ # @raise [Errors::Fs::Validation::UnsupportedMimeType]
51
+ # @return [Fs::Files::JPG, Fs::Files::PNG, Fs::Files::TIFF]
52
+ def self.klass_for(path)
53
+ mime_type =
54
+ Shell::File.mime_type(path)
55
+ .raise_failures
56
+ .value
57
+
58
+ klass =
59
+ %w(JPG GIF PNG TIFF).find do |type|
60
+ "Fs::Files::#{type}".constantize
61
+ .then do |klass|
62
+ klass.accepts?(mime_type: mime_type) ? klass : false
63
+ end
64
+ end
65
+
66
+ unless klass
67
+ Errors::Fs::Validation::UnsupportedMimeType.raise(label: Fs::File.normalize_path_input(path).to_s, subject: mime_type)
68
+ end
69
+
70
+ "Fs::Files::#{klass}".constantize
71
+ end
72
+
73
+ # Wraps `path` in the appropriate image subclass for either
74
+ # JPG, PNG or TIFF
75
+ #
76
+ # @param path [Pathname, Fs::File, String]
77
+ # @param skip_extension_validation [Boolean]
78
+ # @raise [Errors::Fs::Validation::UnsupportedMimeType]
79
+ # @return [Fs::Files::JPG, Fs::Files::PNG, Fs::Files::TIFF]
80
+ def self.for(path, skip_extension_validation: false)
81
+ klass_for(path).load(path: path, skip_extension_validation: skip_extension_validation)
82
+ end
83
+
84
+ # Create a new `Fs::File` object by copying an existing file from `source`; convert
85
+ # it to our own format if it is in a different one
86
+ #
87
+ # The source file must be valid according to the current `Fs::File` subclass.
88
+ #
89
+ # Validation of the source file can be disabled either completely through
90
+ # `validate_source: false`, or selectively through the three `skip_` arguments.
91
+ #
92
+ # Provide the same `**kargs` you would provide to {.load}:
93
+ #
94
+ # @example
95
+ # # `Fs::Files::Team::Logo` is a PNG subclass
96
+ # Fs::Files::Team::Logo.copy_and_convert_from("/some/file.jpg", team: Team.first)
97
+ #
98
+ # @param source [Fs::File, Pathname, String]
99
+ # @param overwrite [Boolean] overwrite an existing destination file
100
+ # @see #initialize
101
+ # @raise [Errors::Fs::ReadWrite]
102
+ # @return [Fs::Files::Image]
103
+ def self.copy_and_convert_from(source, overwrite: false, **kargs)
104
+ with_existing_file(source, overwrite: overwrite, validate_source: false, **kargs) do |source_path, instance|
105
+ if klass_for(source) <=> self
106
+ FileUtils.cp(source_path, instance.path)
107
+ else
108
+ Shell::Convert.exec(input: source_path, output: instance.path).raise_failures
109
+ end
110
+ end
111
+ end
112
+
113
+ # Is this image blank?
114
+ #
115
+ # @param ratio_range [Range<Integer>] the range in which
116
+ # we consider the page to be NOT blank, eg. everything
117
+ # from 10% to 90% black pixels
118
+ # @return [Boolean]
119
+ def blank?(ratio_range: BW_RATIO_RANGE)
120
+ ratio_range.exclude?(bw_ratio[:black])
121
+ end
122
+
123
+ # Compare the similarity of the first `percentage` percent
124
+ # of us with the first `percentage` percent of `file`
125
+ #
126
+ # @param file [Pathname, Fs::File, String]
127
+ # @param percentage [Integer] 0-100
128
+ # @param metric see {Shell::Magick.compare}
129
+ # @param ratio_range [Range<Integer>] the range in which
130
+ # we consider the page to be NOT blank, eg. everything
131
+ # from 10% to 90% black pixels
132
+ # @return [Float, nil] 0-100; `nil` if one of the tops is blank
133
+ def compare_top_with(file, percentage: 15, metric: "SSIM", ratio_range: BW_RATIO_RANGE)
134
+ Fs::Files::Image.create_temp do |our_top|
135
+ Shell::Magick.top(input: self, output: our_top, percentage: percentage)
136
+ .raise_failures
137
+
138
+ return nil if our_top.blank?
139
+
140
+ Fs::Files::Image.create_temp do |their_top|
141
+ Shell::Magick.top(input: file, output: their_top, percentage: percentage)
142
+ .raise_failures
143
+
144
+ return nil if their_top.blank?
145
+
146
+ Shell::Magick.compare(our_top, their_top, metric: metric)
147
+ .raise_failures
148
+ .value
149
+ end
150
+ end
151
+ end
152
+
153
+ # Compare the similarity of the last `percentage` percent
154
+ # of us with the last `percentage` percent of `file`
155
+ #
156
+ # @param file [Pathname, Fs::File, String]
157
+ # @param percentage [Integer] 0-100
158
+ # @param metric see {Shell::Magick.compare}
159
+ # @param ratio_range [Range<Integer>] the range in which
160
+ # we consider the page to be NOT blank, eg. everything
161
+ # from 10% to 90% black pixels
162
+ # @return [Float, nil] 0-100; `nil` if one of the bottoms is blank
163
+ def compare_bottom_with(file, percentage: 10, metric: "SSIM", ratio_range: BW_RATIO_RANGE)
164
+ Fs::Files::Image.create_temp do |our_bottom|
165
+ Shell::Magick.bottom(input: self, output: our_bottom, percentage: percentage)
166
+ .raise_failures
167
+
168
+ return nil if our_bottom.blank?
169
+
170
+ Fs::Files::Image.create_temp do |their_bottom|
171
+ Shell::Magick.bottom(input: file, output: their_bottom, percentage: percentage)
172
+ .raise_failures
173
+
174
+ return nil if their_bottom.blank?
175
+
176
+ Shell::Magick.compare(our_bottom, their_bottom, metric: metric)
177
+ .raise_failures
178
+ .value
179
+ end
180
+ end
181
+ end
182
+
183
+ # Compare the similarity of our top and bottom parts with `file`
184
+ #
185
+ # @param file [Pathname, Fs::File, String]
186
+ # @param top [Integer] 0-100
187
+ # @param bottom [Integer] 0-100
188
+ # @param metric see {Shell::Magick.compare}
189
+ # @param ratio_range [Range<Integer>] the range in which
190
+ # we consider the page to be NOT blank, eg. everything
191
+ # from 10% to 90% black pixels
192
+ # @return [Hash{Symbol=>Float,nil}] 0-100; `nil` if a crop - ours or theirs - is blank
193
+ def compare_edges_with(file, top: 15, bottom: 10, metric: "SSIM", ratio_range: BW_RATIO_RANGE)
194
+ {
195
+ top: compare_top_with(file, percentage: top, metric: metric, ratio_range: ratio_range),
196
+ bottom: compare_bottom_with(file, percentage: bottom, metric: metric, ratio_range: ratio_range)
197
+ }
198
+ end
199
+
200
+ # Runs `identify` and returns verbose output as a nested Hash
201
+ #
202
+ # @return [Shell::Command::Result]
203
+ def identify
204
+ @identify ||=
205
+ Shell::Identify.info(path)
206
+ .raise_failures
207
+ .value
208
+ end
209
+
210
+ # Returns an images dimensions and area
211
+ #
212
+ # @return [Shell::Command::Result]
213
+ # `Result#value` [Struct] a Struct with `:x`, `:y` and `:area` attributes
214
+ def dimensions
215
+ @dimensions ||=
216
+ Shell::Identify.dimensions(path)
217
+ .raise_failures
218
+ .value
219
+ end
220
+
221
+ # The number of images contained inside this one
222
+ #
223
+ # Useful for types with that capability, such as
224
+ # GIFs or TIFFs
225
+ #
226
+ # @return [Integer]
227
+ def page_count
228
+ @page_count ||=
229
+ Shell::Identify.image_count(path)
230
+ .raise_failures
231
+ .value
232
+ end
233
+
234
+ # Returns an images dimensions and area after
235
+ # removing surrounding empty space
236
+ #
237
+ # @return [Shell::Command::Result]
238
+ # `Result#value` [Struct] a Struct with `:x`, `:y` and `:area` attributes
239
+ def autocropped_dimensions
240
+ @autocropped_dimensions ||=
241
+ create_temp do |tempfile|
242
+ tempfile.deskew
243
+ # Stretching contrast makes autocrop more effective by
244
+ # pushing the background towards uniform white and the
245
+ # content towards uniform black
246
+ tempfile.enhance_contrast
247
+ tempfile.autocrop
248
+ tempfile.pad
249
+ tempfile.dimensions
250
+ end
251
+ end
252
+
253
+ def ocr(**kargs)
254
+ Shell::Tesseract.ocr(input: self, **kargs)
255
+ .raise_failures
256
+ .value
257
+ end
258
+
259
+ # Apply the EXIF orientation to the image pixels and strip the tag
260
+ #
261
+ # @return [void]
262
+ def auto_orient
263
+ Shell::Convert.auto_orient(input: self)
264
+ .raise_failures
265
+ .tap { forget_dimensions }
266
+ end
267
+
268
+ # Can images of this format carry an alpha channel?
269
+ #
270
+ # @return [Boolean]
271
+ def alpha_capable?
272
+ true
273
+ end
274
+
275
+ # Remove the alpha channel, blending transparent areas onto white
276
+ #
277
+ # Formats without transparency support, such as PDF, cannot take
278
+ # an image with an alpha channel as-is, even if it is fully opaque.
279
+ #
280
+ # @return [Shell::Command::Result]
281
+ def remove_alpha
282
+ Shell::Convert.remove_alpha(input: self)
283
+ .raise_failures
284
+ end
285
+
286
+ # Automatically crop the image by removing surrounding empty space
287
+ #
288
+ # @return [Shell::Command::Result]
289
+ def autocrop
290
+ Shell::Convert.autocrop(input: self)
291
+ .raise_failures
292
+ .tap { forget_dimensions }
293
+ end
294
+
295
+ def bw_ratio(**kargs)
296
+ Shell::Convert.bw_ratio(input: self, **kargs)
297
+ .raise_failures
298
+ .value
299
+ end
300
+
301
+ # Automatically corrects slight rotation/skew
302
+ #
303
+ # @return [Shell::Command::Result]
304
+ def deskew(**kargs)
305
+ Shell::Convert.deskew(input: self, **kargs)
306
+ .raise_failures
307
+ .tap { forget_dimensions }
308
+ end
309
+
310
+ # Increases contrast to improve readability of low-contrast
311
+ # printouts, eg. faded thermal receipts
312
+ #
313
+ # @return [Shell::Command::Result]
314
+ def enhance_contrast(**kargs)
315
+ Shell::Convert.enhance_contrast(input: self, **kargs)
316
+ .raise_failures
317
+ end
318
+
319
+ # Adds a solid-colored border around the image
320
+ #
321
+ # @return [Shell::Command::Result]
322
+ def pad(**kargs)
323
+ Shell::Convert.pad(input: self, **kargs)
324
+ .raise_failures
325
+ .tap { forget_dimensions }
326
+ end
327
+
328
+ # Detected clockwise rotation in degress
329
+ #
330
+ # Only works on images containing text, because it relies
331
+ # on Tesseract
332
+ #
333
+ # @see Shell::Tesseract#detect_rotation
334
+ # @options **kargs
335
+ # @return [Integer] 0 - 270
336
+ def rotation(**kargs)
337
+ Shell::Tesseract.detect_rotation(input: self, **kargs)
338
+ .raise_failures
339
+ .value
340
+ end
341
+
342
+ # Rotate the image clockwise by `:degrees`
343
+ #
344
+ # @param degrees [Integer] 0 - 270
345
+ # @return [void]
346
+ def rotate(degrees:, **kargs)
347
+ Shell::Convert.rotate(input: self, degrees: degrees, **kargs)
348
+ .raise_failures
349
+ .tap { forget_dimensions }
350
+ end
351
+
352
+ # Resize the image in-place to fit into an area of `area` pixels
353
+ #
354
+ # Note that in practice, the resulting image will be slightly smaller
355
+ # due to rounding.
356
+ #
357
+ # @param area [Integer] number of pixels the image should at most have
358
+ # @return [Shell::Command::Result]
359
+ # Command result of Shell::Convert
360
+ def fit_area!(area)
361
+ return if dimensions.area < area
362
+
363
+ # factor to shrink by, eg. 0.9
364
+ factor = area.to_f / dimensions.area
365
+ new_x = (Math.sqrt(factor) * dimensions.x).to_i
366
+ new_y = (Math.sqrt(factor) * dimensions.y).to_i
367
+
368
+ Shell::Convert.resize(input: path, x: new_x, y: new_y)
369
+ .raise_failures
370
+ .tap { forget_dimensions }
371
+ end
372
+
373
+ # Are we a valid image?
374
+ #
375
+ # @return [Boolean]
376
+ def valid_image?
377
+ Shell::Identify.valid?(self)
378
+ end
379
+
380
+ private
381
+
382
+ # Our dimensions are memoized, and what changes them has to let go of them
383
+ #
384
+ # @return [void]
385
+ def forget_dimensions
386
+ @dimensions = nil
387
+ end
388
+ end
389
+ end
390
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "shell/convert"
4
+
5
+ module Fs
6
+ module Files
7
+ class JPG < Image
8
+ extension :jpg, :jpeg, :jp2
9
+ mime_type "image/jpeg", "image/jp2"
10
+
11
+ # JPEG has no alpha channel. JPEG 2000 has, but img2pdf embeds it as-is
12
+ # and so never trips over it.
13
+ #
14
+ # @return [Boolean]
15
+ def alpha_capable?
16
+ false
17
+ end
18
+
19
+ variant(:thumbnail, Fs::Files::JPG) do
20
+ get_path_from do |size: "x300", **kargs|
21
+ dirname/"#{basename}_thumbnail_#{size}.#{extension}"
22
+ end
23
+
24
+ load_with do |klass:, path:, **kargs|
25
+ klass.load(path: path)
26
+ end
27
+
28
+ create_with do |klass:, path:, size: "x300", content: nil, create_block: nil, **kargs|
29
+ Shell::Convert.thumbnail(input: self.path, output: path, size: size, **kargs)
30
+
31
+ klass.load(path: path)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fs
4
+ module Files
5
+ class JSON < Fs::File
6
+ extension :json
7
+ mime_type "application/json", /^text\//
8
+ encoding "utf-8", "us-ascii", "iso-8859-1", "iso-8859-15", "cp1252", "unknown-8bit"
9
+
10
+ # @return [String]
11
+ def raw_content
12
+ @raw_content ||=
13
+ ::File.read(path)
14
+ .then do |string|
15
+ string.encode(Encoding::UTF_8)
16
+ end
17
+ end
18
+
19
+ def content(**parser_options)
20
+ ::JSON.load(raw_content)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,231 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/object/blank"
5
+ require "shell"
6
+ require "shell/ghostscript"
7
+ require "shell/pdfimages"
8
+ require "shell/pdfinfo"
9
+ require "shell/pdftotext"
10
+
11
+ module Fs
12
+ module Files
13
+ class PDF < Fs::File
14
+ extension :pdf
15
+ mime_type "application/pdf"
16
+ encoding "binary", "us-ascii", "unknown-8bit", "iso-8859-1"
17
+
18
+ # The page image variants take the arguments of {Shell::Ghostscript.pdf_to_image} which
19
+ # matter here: the `page`, a `resolution` in DPI (Ghostscript's default if left out) and
20
+ # `max_pixels`, which lowers the resolution as far as a large page needs to stay within it.
21
+ variant(:text, Fs::Files::Text) do
22
+ create_with_content_from { text }
23
+ end
24
+
25
+ variant(:single_page_pdf, self) do
26
+ get_path_from do |klass:, page:|
27
+ path.dirname / "#{basename}_#{page}.#{klass.extensions.first}"
28
+ end
29
+
30
+ create_with do |klass:, page:, path: nil|
31
+ Shell::Ghostscript.extract_pdf(input: self.path, output: path, page: page)
32
+ .raise_failures
33
+
34
+ klass.load(path: path)
35
+ end
36
+ end
37
+
38
+ variant(:tiff, Fs::Files::TIFF) do
39
+ get_path_from do |klass:, page:, **|
40
+ path.dirname / "#{basename}_#{page}.#{klass.extensions.first}"
41
+ end
42
+
43
+ create_with do |klass:, page:, path:, resolution: nil, **kargs|
44
+ Shell::Ghostscript.pdf_to_image(input: self.path, output: path, page: page, format: :tiff,
45
+ **{ resolution: resolution }.compact)
46
+ .raise_failures
47
+
48
+ klass.load(path: path)
49
+ end
50
+ end
51
+
52
+ variant(:jpg, Fs::Files::JPG) do
53
+ get_path_from do |klass:, page:, **|
54
+ path.dirname / "#{basename}_#{page}.#{klass.extensions.first}"
55
+ end
56
+
57
+ create_with do |klass:, page:, path:, resolution: nil, max_pixels: nil, **kargs|
58
+ Shell::Ghostscript.pdf_to_image(input: self.path, output: path, page: page, format: :jpg,
59
+ max_pixels: max_pixels, **{ resolution: resolution }.compact)
60
+ .raise_failures
61
+
62
+ klass.load(path: path)
63
+ end
64
+ end
65
+
66
+ variant(:png, Fs::Files::PNG) do
67
+ get_path_from do |klass:, page:, **|
68
+ path.dirname / "#{basename}_#{page}.#{klass.extensions.first}"
69
+ end
70
+
71
+ create_with do |klass:, page:, path:, resolution: nil, max_pixels: nil, **kargs|
72
+ Shell::Ghostscript.pdf_to_image(input: self.path, output: path, page: page, format: :png,
73
+ max_pixels: max_pixels, **{ resolution: resolution }.compact)
74
+ .raise_failures
75
+
76
+ klass.load(path: path)
77
+ end
78
+ end
79
+
80
+ # @return [Boolean]
81
+ def parseable?
82
+ text && page_count && well_formed?
83
+ rescue Shell::Errors::CommandUnsuccessful
84
+ false
85
+ end
86
+
87
+ # Does Ghostscript consider this PDF well-formed, ie. is it processable
88
+ # without Ghostscript having to repair or ignore part of it?
89
+ #
90
+ # @return [Boolean]
91
+ def well_formed?
92
+ Shell::Ghostscript.well_formed?(path)
93
+ .raise_failures
94
+ .value
95
+ end
96
+
97
+ # @raise [Errors::Parser::FacturX::InvalidFile]
98
+ # @raise [Errors::Parser::FacturX::InvalidExtensionSchema]
99
+ # @raise [Errors::Parser::FacturX::InvalidInvoice]
100
+ # @return [Boolean]
101
+ def electronic_invoice?
102
+ electronic_invoice.present?
103
+ end
104
+
105
+ # @raise [Errors::Parser::FacturX::InvalidFile]
106
+ # @raise [Errors::Parser::FacturX::InvalidExtensionSchema]
107
+ # @raise [Errors::Parser::FacturX::InvalidInvoice]
108
+ # @return [Parser::XML::FacturX::CII::Invoice, nil]
109
+ def electronic_invoice
110
+ @electronic_invoice ||= Parser::PDF::FacturX.try_parse!(self)
111
+ end
112
+
113
+ # The combined text layer contents of all PDF pages
114
+ #
115
+ # @return [String]
116
+ def text
117
+ Shell::Pdftotext.text(path)
118
+ .raise_failures
119
+ .value
120
+ end
121
+
122
+ # The text layer content on page `:page`
123
+ #
124
+ # @param page [Integer] the 1-based page number
125
+ # @return [String]
126
+ def text_on(page:)
127
+ Shell::Pdftotext.page(path, page: page)
128
+ .raise_failures
129
+ .value
130
+ end
131
+
132
+ # @return [Integer]
133
+ def page_count
134
+ Shell::Pdfinfo.page_count(path)
135
+ .raise_failures
136
+ .value
137
+ end
138
+
139
+ # Does this PDF have embedded machine-readable text?
140
+ #
141
+ # @return [Boolean]
142
+ def text_layer?
143
+ text.size.positive?
144
+ end
145
+
146
+ # Is this PDF encrypted in such a manner that prevents us
147
+ # from reading its contents?
148
+ #
149
+ # PDF user restrictions weirdly allow "encrypted" files
150
+ # which can nonetheless be read, but prohibit you from
151
+ # extracting contents or changing them.
152
+ #
153
+ # For our use cases, we only care about access for OCR
154
+ # purposes.
155
+ #
156
+ # @return [Boolean]
157
+ def encrypted?
158
+ Shell::Pdfinfo.pdf_reading_prohibited?(path)
159
+ .raise_failures
160
+ .value
161
+ end
162
+
163
+ # The average and median pixels per inch over all pages
164
+ #
165
+ # @example
166
+ # some_pdf.resolution
167
+ # # => #<OpenStruct x_average=157, y_average=183, x_median=155, y_median=175>
168
+ #
169
+ # @return [OpenStruct] a struct with `#x_average`, `#y_average`, `#x_median` and `#y_median`
170
+ def resolution
171
+ Shell::Pdfimages.resolution(path)
172
+ .raise_failures
173
+ .value
174
+ end
175
+
176
+ # Replaces self with a Ghostscript-reencoded version
177
+ #
178
+ # This is useful for malformed files which Ghostscript
179
+ # might still be able to process.
180
+ #
181
+ # @option **kargs see Shell::Ghostscript.regenerate
182
+ # @return [void]
183
+ def reencode!(**kargs)
184
+ # Ghostscript does not seem to do in-place, thus the tempfile
185
+ create_temp do |tempfile|
186
+ Shell::Ghostscript.regenerate(input: self, output: tempfile, **kargs)
187
+ .raise_failures
188
+
189
+ delete
190
+
191
+ FileUtils.mv(tempfile.path, path)
192
+ end
193
+ end
194
+
195
+ # Does this PDF contain a /Collection dictionary, making it a
196
+ # PDF portfolio/collection rather than a regular document?
197
+ #
198
+ # @return [Boolean]
199
+ def collection?
200
+ target = "/Collection"
201
+
202
+ # We carry the last (target.bytesize - 1) bytes of each chunk into the
203
+ # next iteration so that a match straddling a chunk boundary is not missed.
204
+ overlap = target.bytesize - 1
205
+ tail = "".b
206
+
207
+ ::File.open(path.to_s, "rb") do |f|
208
+ loop do
209
+ chunk = f.read(65536)
210
+ break false if chunk.nil?
211
+
212
+ # Prepend the tail from the previous chunk before searching,
213
+ # then update the tail for the next iteration.
214
+ combined = tail + chunk
215
+ break true if combined.b.include?(target)
216
+ tail = combined.last(overlap)
217
+ end
218
+ end
219
+ end
220
+
221
+ private
222
+
223
+ # @raise [Errors::Fs::Validation::FileUnreadable]
224
+ # @return [void]
225
+ def custom_validations
226
+ raise Errors::Fs::Validation::FileUnreadable,
227
+ "#{path} is a PDF collection and cannot be processed as a regular PDF" if collection?
228
+ end
229
+ end
230
+ end
231
+ end