libis-format 1.2.9 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/data/types.yml +1 -1
  4. data/lib/libis/format/command_line.rb +2 -3
  5. data/lib/libis/format/config.rb +18 -19
  6. data/lib/libis/format/converter/base.rb +9 -16
  7. data/lib/libis/format/converter/chain.rb +36 -28
  8. data/lib/libis/format/converter/email_converter.rb +5 -8
  9. data/lib/libis/format/converter/fop_pdf_converter.rb +4 -6
  10. data/lib/libis/format/converter/image_converter.rb +51 -58
  11. data/lib/libis/format/converter/jp2_converter.rb +33 -35
  12. data/lib/libis/format/converter/office_converter.rb +19 -23
  13. data/lib/libis/format/converter/pdf_converter.rb +22 -28
  14. data/lib/libis/format/converter/repository.rb +7 -13
  15. data/lib/libis/format/converter/spreadsheet_converter.rb +7 -11
  16. data/lib/libis/format/converter/video_converter.rb +41 -55
  17. data/lib/libis/format/converter/xslt_converter.rb +14 -13
  18. data/lib/libis/format/converter.rb +1 -1
  19. data/lib/libis/format/identifier.rb +41 -43
  20. data/lib/libis/format/tool/droid.rb +29 -30
  21. data/lib/libis/format/tool/ff_mpeg.rb +11 -13
  22. data/lib/libis/format/tool/fido.rb +1 -1
  23. data/lib/libis/format/tool/pdf_copy.rb +17 -18
  24. data/lib/libis/format/tool/pdf_merge.rb +17 -17
  25. data/lib/libis/format/tool/pdf_optimizer.rb +18 -20
  26. data/lib/libis/format/tool/spreadsheet_to_ods.rb +23 -20
  27. data/lib/libis/format/tool.rb +2 -2
  28. data/lib/libis/format/type_database.rb +51 -28
  29. data/lib/libis/format/type_database_impl.rb +57 -24
  30. data/lib/libis/format/version.rb +1 -1
  31. data/lib/libis/format.rb +3 -2
  32. data/lib/libis-format.rb +2 -0
  33. metadata +3 -3
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'os'
2
4
 
3
5
  require 'libis/tools/extend/string'
@@ -9,46 +11,42 @@ require 'libis/format/config'
9
11
  module Libis
10
12
  module Format
11
13
  module Tool
12
-
13
14
  class PdfOptimizer
14
15
  include ::Libis::Tools::Logger
15
16
 
16
17
  def self.installed?
17
- result = Libis::Tools::Command.run(Libis::Format::Config[:ghostscript_cmd], "--version")
18
- result[:status] == 0
18
+ result = Libis::Tools::Command.run(Libis::Format::Config[:ghostscript_cmd], '--version')
19
+ (result[:status]).zero?
19
20
  end
20
21
 
21
22
  def self.run(source, target, quality)
22
- self.new.run source, target, quality
23
+ new.run source, target, quality
23
24
  end
24
25
 
25
26
  def run(source, target, quality)
26
-
27
27
  timeout = Libis::Format::Config[:timeouts][:pdf_optimizer]
28
28
  result = Libis::Tools::Command.run(
29
- Libis::Format::Config[:ghostscript_cmd],
30
- '-sDEVICE=pdfwrite',
31
- '-dCompatibilityLevel=1.4',
32
- "-dPDFSETTINGS=/#{quality}",
33
- '-dNOPAUSE',
34
- '-dBATCH',
35
- "-sOutputFile=#{target}",
36
- "#{source}",
37
- timeout: timeout,
38
- kill_after: timeout * 2
29
+ Libis::Format::Config[:ghostscript_cmd],
30
+ '-sDEVICE=pdfwrite',
31
+ '-dCompatibilityLevel=1.4',
32
+ "-dPDFSETTINGS=/#{quality}",
33
+ '-dNOPAUSE',
34
+ '-dBATCH',
35
+ "-sOutputFile=#{target}",
36
+ source.to_s,
37
+ timeout:,
38
+ kill_after: timeout * 2
39
39
  )
40
40
 
41
- raise RuntimeError, "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]
42
- raise RuntimeError, "#{self.class} errors: #{result[:err].join("\n")}" unless result[:status] == 0
41
+ raise "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]
42
+ raise "#{self.class} errors: #{result[:err].join("\n")}" unless (result[:status]).zero?
43
43
 
44
44
  {
45
45
  command: result,
46
- files: [ target ]
46
+ files: [target]
47
47
  }
48
-
49
48
  end
50
49
  end
51
-
52
50
  end
53
51
  end
54
52
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
 
3
5
  require 'libis/tools/extend/string'
@@ -9,61 +11,62 @@ require 'libis/format/config'
9
11
  module Libis
10
12
  module Format
11
13
  module Tool
12
-
13
14
  class SpreadsheetToOds
14
15
  include ::Libis::Tools::Logger
15
16
 
16
17
  def self.installed?
17
- result = Libis::Tools::Command.run(Libis::Format::Config[:soffice_cmd], "--version")
18
- result == 0
18
+ result = Libis::Tools::Command.run(Libis::Format::Config[:soffice_cmd], '--version')
19
+ result.zero?
19
20
  end
20
21
 
21
22
  def self.run(source, target, options = {})
22
- self.new.run source, target, options
23
+ new.run source, target, options
23
24
  end
24
25
 
25
26
  def run(source, target, options = {})
26
27
  workdir = '/...'
27
28
  workdir = Dir.tmpdir unless Dir.exist? workdir
28
29
 
29
- workdir = File.join(workdir, rand(1000000).to_s)
30
+ workdir = File.join(workdir, rand(1_000_000).to_s)
30
31
  FileUtils.mkpath(workdir)
31
32
 
32
33
  src_file = File.join(workdir, File.basename(source))
33
34
  FileUtils.symlink source, src_file
34
35
 
35
- tgt_file = File.join(workdir, File.basename(source, '.*') + '.ods')
36
+ tgt_file = File.join(workdir, "#{File.basename(source, '.*')}.ods")
36
37
 
37
38
  export_filter = options[:export_filter] || 'ods'
38
39
 
39
40
  timeout = Libis::Format::Config[:timeouts][:spreadsheet_to_ods] ||
40
- Libis::Format::Config[:timeouts][:office_to_pdf]
41
+ Libis::Format::Config[:timeouts][:office_to_pdf]
41
42
  result = Libis::Tools::Command.run(
42
- Libis::Format::Config[:soffice_cmd], '--headless',
43
- "-env:UserInstallation=file://#{workdir}",
44
- '--convert-to', export_filter,
45
- '--outdir', workdir, src_file,
46
- timeout: timeout,
47
- kill_after: timeout * 2
43
+ Libis::Format::Config[:soffice_cmd], '--headless',
44
+ "-env:UserInstallation=file://#{workdir}",
45
+ '--convert-to', export_filter,
46
+ '--outdir', workdir, src_file,
47
+ timeout:,
48
+ kill_after: timeout * 2
48
49
  )
49
50
 
50
- raise RuntimeError, "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]
51
+ raise "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]
52
+
51
53
  warn "OfficeToPdf conversion messages: \n\t#{result[:err].join("\n\t")}" unless result[:err].empty?
52
- raise RuntimeError, "#{self.class} failed to generate target file #{tgt_file}" unless File.exist?(tgt_file)
54
+ raise "#{self.class} failed to generate target file #{tgt_file}" unless File.exist?(tgt_file)
53
55
 
54
56
  FileUtils.copy tgt_file, target, preserve: true
55
57
 
56
58
  {
57
59
  command: result,
58
- files: [ target ]
60
+ files: [target]
59
61
  }
60
-
61
62
  ensure
62
- FileUtils.rmtree workdir rescue nil
63
-
63
+ begin
64
+ FileUtils.rmtree workdir
65
+ rescue StandardError
66
+ nil
67
+ end
64
68
  end
65
69
  end
66
-
67
70
  end
68
71
  end
69
72
  end
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # code utf-8
2
4
 
3
5
  module Libis
4
6
  module Format
5
7
  module Tool
6
-
7
8
  autoload :Droid, 'libis/format/tool/droid'
8
9
  autoload :ExtensionIdentification, 'libis/format/tool/extension_identification'
9
10
  autoload :Fido, 'libis/format/tool/fido'
@@ -19,7 +20,6 @@ module Libis
19
20
  autoload :PdfToPdfa, 'libis/format/tool/pdf_to_pdfa'
20
21
  autoload :PdfaValidator, 'libis/format/tool/pdfa_validator'
21
22
  autoload :MsgToPdf, 'libis/format/tool/msg_to_pdf'
22
-
23
23
  end
24
24
  end
25
25
  end
@@ -1,11 +1,10 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
4
  require 'libis/tools/extend/hash'
5
5
 
6
6
  module Libis
7
7
  module Format
8
-
9
8
  # noinspection RubyClassVariableUsageInspection
10
9
  class TypeDatabase
11
10
  @implementation = Libis::Format::TypeDatabaseImpl.instance
@@ -16,16 +15,25 @@ module Libis
16
15
 
17
16
  def self.enrich(info, map_keys = {})
18
17
  return {} unless info.is_a? Hash
19
- mapper = Hash.new {|hash,key| hash[key] = key}
18
+
19
+ mapper = Hash.new { |hash, key| hash[key] = key }
20
20
  mapper.merge! map_keys
21
21
  unless (puid = info[mapper[:PUID]]).blank?
22
- info[mapper[:TYPE]] ||= puid_infos(puid).first[:TYPE] rescue nil
22
+ info[mapper[:TYPE]] ||= begin
23
+ puid_infos(puid).first[:TYPE]
24
+ rescue StandardError
25
+ nil
26
+ end
23
27
  end
24
28
  unless (mime = info[mapper[:MIME]]).blank?
25
- info[mapper[:TYPE]] ||= mime_infos(mime).first[:TYPE] rescue nil
29
+ info[mapper[:TYPE]] ||= begin
30
+ mime_infos(mime).first[:TYPE]
31
+ rescue StandardError
32
+ nil
33
+ end
26
34
  end
27
35
  unless (type_name = info[mapper[:TYPE]]).nil?
28
- mapper.keys.each do |key|
36
+ mapper.each_key do |key|
29
37
  info[mapper[key]] = get(type_name, key) || info[mapper[key]]
30
38
  end
31
39
  info[mapper[:GROUP]] = type_group(type_name)
@@ -35,13 +43,22 @@ module Libis
35
43
 
36
44
  def self.normalize(info, map_keys = {})
37
45
  return {} unless info.is_a? Hash
38
- mapper = Hash.new {|hash,key| hash[key] = key}
46
+
47
+ mapper = Hash.new { |hash, key| hash[key] = key }
39
48
  mapper.merge! map_keys
40
49
  unless (puid = info[mapper[:PUID]]).blank?
41
- info[mapper[:TYPE]] ||= puid_infos(puid).first[:TYPE] rescue nil
50
+ info[mapper[:TYPE]] ||= begin
51
+ puid_infos(puid).first[:TYPE]
52
+ rescue StandardError
53
+ nil
54
+ end
42
55
  end
43
56
  unless (mime = info[mapper[:MIME]]).blank?
44
- info[mapper[:TYPE]] ||= mime_infos(mime).first[:TYPE] rescue nil
57
+ info[mapper[:TYPE]] ||= begin
58
+ mime_infos(mime).first[:TYPE]
59
+ rescue StandardError
60
+ nil
61
+ end
45
62
  end
46
63
  unless (type_name = info[mapper[:TYPE]]).nil?
47
64
  info[mapper[:MIME]] = type_mimetypes(type_name).first if type_mimetypes(type_name).first
@@ -52,35 +69,35 @@ module Libis
52
69
 
53
70
  def self.get(type_name, key)
54
71
  case key
55
- when :MIME
56
- type_mimetypes(type_name).first
57
- when :PUID
58
- type_puids(type_name).first
59
- when :EXTENSION
60
- type_extentions(type_name).first
61
- else
62
- typeinfo(type_name)[key]
72
+ when :MIME
73
+ type_mimetypes(type_name).first
74
+ when :PUID
75
+ type_puids(type_name).first
76
+ when :EXTENSION
77
+ type_extentions(type_name).first
78
+ else
79
+ typeinfo(type_name)[key]
63
80
  end
64
81
  end
65
82
 
66
- def self.type_group(t)
67
- typeinfo(t)[:GROUP]
83
+ def self.type_group(ftype)
84
+ typeinfo(ftype)[:GROUP]
68
85
  end
69
86
 
70
- def self.type_mimetypes(t)
71
- typeinfo(t)[:MIME] || []
87
+ def self.type_mimetypes(ftype)
88
+ typeinfo(ftype)[:MIME] || []
72
89
  end
73
90
 
74
- def self.type_puids(t)
75
- typeinfo(t)[:PUID] || []
91
+ def self.type_puids(ftype)
92
+ typeinfo(ftype)[:PUID] || []
76
93
  end
77
94
 
78
- def self.type_extentions(t)
79
- typeinfo(t)[:EXTENSIONS] || []
95
+ def self.type_extentions(ftype)
96
+ typeinfo(ftype)[:EXTENSIONS] || []
80
97
  end
81
98
 
82
- def self.typeinfo(t)
83
- @implementation.typeinfo(t)
99
+ def self.typeinfo(ftype)
100
+ @implementation.typeinfo(ftype)
84
101
  end
85
102
 
86
103
  def self.group_types(group)
@@ -127,7 +144,13 @@ module Libis
127
144
  @implementation.known_mime?(mime)
128
145
  end
129
146
 
130
- end
147
+ def self.groups
148
+ @implementation.groups
149
+ end
131
150
 
151
+ def self.export_csv(filename, **options)
152
+ @implementation.export_csv(filename, **options)
153
+ end
154
+ end
132
155
  end
133
156
  end
@@ -1,21 +1,22 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'singleton'
4
4
  require 'yaml'
5
+ require 'csv'
5
6
 
6
7
  require 'libis/tools/logger'
7
8
  require 'libis/tools/extend/hash'
8
9
  require 'libis/tools/extend/string'
10
+ require 'libis/tools/extend/symbol'
9
11
 
10
12
  module Libis
11
13
  module Format
12
-
13
14
  class TypeDatabaseImpl
14
15
  include Singleton
15
16
  include ::Libis::Tools::Logger
16
17
 
17
- def typeinfo(t)
18
- @types[t.to_sym] || {}
18
+ def typeinfo(ftype)
19
+ @types[ftype.to_sym] || {}
19
20
  end
20
21
 
21
22
  def group_types(group)
@@ -26,58 +27,84 @@ module Libis
26
27
 
27
28
  def puid_infos(puid)
28
29
  @types.select do |_, v|
29
- v[:PUID].include? puid rescue false
30
+ v[:PUID].include? puid
31
+ rescue StandardError
32
+ false
30
33
  end.values
31
34
  end
32
35
 
33
36
  def puid_types(puid)
34
37
  @types.select do |_, v|
35
- v[:PUID].include? puid rescue false
38
+ v[:PUID].include? puid
39
+ rescue StandardError
40
+ false
36
41
  end.keys
37
42
  end
38
43
 
39
44
  def mime_infos(mime)
40
45
  @types.select do |_, v|
41
- v[:MIME].include? mime rescue false
46
+ v[:MIME].include? mime
47
+ rescue StandardError
48
+ false
42
49
  end.values
43
50
  end
44
51
 
45
52
  def mime_types(mime)
46
53
  @types.select do |_, v|
47
- v[:MIME].include? mime rescue false
54
+ v[:MIME].include? mime
55
+ rescue StandardError
56
+ false
48
57
  end.keys
49
58
  end
50
59
 
51
60
  def ext_infos(ext)
52
- ext = ext.gsub /^\./, ''
61
+ ext = ext.gsub(/^\./, '')
53
62
  @types.select do |_, v|
54
- v[:EXTENSIONS].include?(ext) rescue false
63
+ v[:EXTENSIONS].include?(ext)
64
+ rescue StandardError
65
+ false
55
66
  end.values
56
67
  end
57
68
 
58
69
  def ext_types(ext)
59
- ext = ext.gsub /^\./, ''
70
+ ext = ext.gsub(/^\./, '')
60
71
  @types.select do |_, v|
61
- v[:EXTENSIONS].include?(ext) rescue false
72
+ v[:EXTENSIONS].include?(ext)
73
+ rescue StandardError
74
+ false
62
75
  end.keys
63
76
  end
64
77
 
65
78
  def puid_typeinfo(puid)
66
79
  @types.each do |_, v|
67
- return v if v[:PUID] and v[:PUID].include?(puid)
80
+ return v if v[:PUID]&.include?(puid)
68
81
  end
69
82
  nil
70
83
  end
71
84
 
72
85
  def known_mime?(mime)
73
86
  @types.each do |_, v|
74
- return true if v[:MIME].include? mime
87
+ return true if v[:MIME]&.include? mime
75
88
  end
76
89
  false
77
90
  end
78
91
 
92
+ def groups
93
+ @types.values.map(&:dig.call(:GROUP)).uniq
94
+ end
95
+
96
+ def export_csv(filename, **options)
97
+ headers = @types.values.each_with_object(Set.new) { |v, s| v.each_key { |k| s << k.to_s } }
98
+ options[:headers] = headers.to_a
99
+ CSV.open(filename, 'w', **options) do |csv|
100
+ @types.each_value do |v|
101
+ csv << CSV::Row.new(v.keys, v.values.map { |x| x.is_a?(Array) ? x.join(', ') : x })
102
+ end
103
+ end
104
+ end
105
+
79
106
  def load_types(file_or_hash = {}, append = true)
80
- hash = file_or_hash.is_a?(Hash) ? file_or_hash : YAML::load_file(file_or_hash)
107
+ hash = file_or_hash.is_a?(Hash) ? file_or_hash : YAML.load_file(file_or_hash)
81
108
  # noinspection RubyResolve
82
109
  hash.each do |group, type_info|
83
110
  type_info.each do |type_name, info|
@@ -85,12 +112,20 @@ module Libis
85
112
  info.symbolize_keys!
86
113
  info[:TYPE] = type_key
87
114
  info[:GROUP] = group.to_sym
88
- info[:MIME] = info[:MIME].strip.split(/[\s,]+/).map(&:strip) rescue []
89
- info[:EXTENSIONS] = info[:EXTENSIONS].strip.split(/[\s,]+/).map { |v| v.strip } rescue []
90
- info[:PUID] = info[:PUID].strip.split(/[\s,]+/).map { |v| v.strip } if info[:PUID]
91
- if @types.has_key?(type_key)
92
- warn 'Type %s already defined; merging with info from %s.', type_name.to_s, file_or_hash
93
- info.merge!(@types[type_key]) do |_,v_new,v_old|
115
+ info[:MIME] = begin
116
+ info[:MIME].strip.split(/[\s,]+/).map(&:strip)
117
+ rescue StandardError
118
+ []
119
+ end
120
+ info[:EXTENSIONS] = begin
121
+ info[:EXTENSIONS].strip.split(/[\s,]+/).map(&:strip)
122
+ rescue StandardError
123
+ []
124
+ end
125
+ info[:PUID] = info[:PUID].strip.split(/[\s,]+/).map(&:strip) if info[:PUID]
126
+ if @types.key?(type_key)
127
+ warn 'Type %s already defined; merging with info from %s.', type_name, file_or_hash
128
+ info.merge!(@types[type_key]) do |_, v_new, v_old|
94
129
  case v_old
95
130
  when Array
96
131
  append ? v_old + v_new : v_new + v_old
@@ -109,12 +144,10 @@ module Libis
109
144
  protected
110
145
 
111
146
  def initialize
112
- @types = Hash.new
147
+ @types = {}
113
148
  type_database = Libis::Format::Config[:type_database]
114
149
  load_types(type_database)
115
150
  end
116
-
117
151
  end
118
-
119
152
  end
120
153
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Libis
4
4
  module Format
5
- VERSION = '1.2.9'
5
+ VERSION = '1.3.0'
6
6
  end
7
7
  end
data/lib/libis/format.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'libis/format/version'
2
4
 
3
5
  module Libis
@@ -13,6 +15,5 @@ module Libis
13
15
  ROOT_DIR = File.absolute_path(File.join(File.dirname(__FILE__), '..', '..'))
14
16
  DATA_DIR = File.join(ROOT_DIR, 'data')
15
17
  TOOL_DIR = File.join(ROOT_DIR, 'tools')
16
-
17
18
  end
18
- end
19
+ end
data/lib/libis-format.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'libis/format'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libis-format
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.9
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kris Dekeyser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-27 00:00:00.000000000 Z
11
+ date: 2024-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chromaprint
@@ -367,7 +367,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
367
367
  - !ruby/object:Gem::Version
368
368
  version: '0'
369
369
  requirements: []
370
- rubygems_version: 3.4.10
370
+ rubygems_version: 3.5.1
371
371
  signing_key:
372
372
  specification_version: 4
373
373
  summary: LIBIS File format format services.