aspera-cli 4.29.1 → 4.30.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 (264) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +4 -4
  3. data/CHANGELOG.md +119 -0
  4. data/CONTRIBUTING.md +8 -1
  5. data/README.md +2 -0
  6. data/TODO.md +4 -5
  7. data/bin/asession +2 -2
  8. data/docs/README.md +616 -86
  9. data/docs/test-mcp-with-ai.md +4 -0
  10. data/lib/aspera/agent/base.rb +62 -6
  11. data/lib/aspera/agent/factory.rb +127 -20
  12. data/lib/aspera/ascp/data_repository.rb +71 -0
  13. data/lib/aspera/ascp/installation.rb +16 -11
  14. data/lib/aspera/ascp/log_analyzer/lines.rb +4 -4
  15. data/lib/aspera/ascp/log_analyzer/log_file.rb +5 -3
  16. data/lib/aspera/ascp/log_analyzer/records.rb +11 -6
  17. data/lib/aspera/ascp/log_analyzer.rb +43 -18
  18. data/lib/aspera/ascp/log_locations.rb +2 -2
  19. data/lib/aspera/assert.rb +8 -0
  20. data/lib/aspera/bundle_registry.rb +528 -0
  21. data/lib/aspera/cli/argument.rb +110 -0
  22. data/lib/aspera/cli/bool_value.rb +41 -0
  23. data/lib/aspera/cli/bootstrapper.rb +31 -24
  24. data/lib/aspera/cli/command/mount.rb +53 -0
  25. data/lib/aspera/cli/command/registry.rb +434 -0
  26. data/lib/aspera/cli/command.rb +95 -0
  27. data/lib/aspera/cli/command_line.rb +91 -91
  28. data/lib/aspera/cli/complete.rb +22 -0
  29. data/lib/aspera/cli/completion_actions.rb +28 -26
  30. data/lib/aspera/cli/context.rb +29 -6
  31. data/lib/aspera/cli/error.rb +39 -0
  32. data/lib/aspera/cli/extended_value.rb +25 -4
  33. data/lib/aspera/cli/formatter.rb +8 -8
  34. data/lib/aspera/cli/hints.rb +17 -52
  35. data/lib/aspera/cli/http.rb +17 -12
  36. data/lib/aspera/cli/mailer.rb +7 -24
  37. data/lib/aspera/cli/mcp/tool.rb +10 -2
  38. data/lib/aspera/cli/option/declarator.rb +80 -0
  39. data/lib/aspera/cli/option/registry.rb +74 -0
  40. data/lib/aspera/cli/option/source.rb +38 -0
  41. data/lib/aspera/cli/option/type.rb +37 -0
  42. data/lib/aspera/cli/option/value.rb +376 -0
  43. data/lib/aspera/cli/option.rb +78 -0
  44. data/lib/aspera/cli/options.schema.yaml +28 -783
  45. data/lib/aspera/cli/parser.rb +73 -41
  46. data/lib/aspera/cli/plugins/base.rb +122 -42
  47. data/lib/aspera/cli/plugins/basic_auth.rb +13 -2
  48. data/lib/aspera/cli/plugins/config.rb +101 -167
  49. data/lib/aspera/cli/plugins/factory.rb +139 -43
  50. data/lib/aspera/cli/plugins/mcp.rb +61 -54
  51. data/lib/aspera/cli/plugins/oauth.rb +19 -6
  52. data/lib/aspera/cli/poll.rb +14 -20
  53. data/lib/aspera/cli/preset_actions.rb +11 -3
  54. data/lib/aspera/cli/preset_manager.rb +16 -6
  55. data/lib/aspera/cli/result.rb +2 -2
  56. data/lib/aspera/cli/runner.rb +71 -99
  57. data/lib/aspera/cli/secret_finder.rb +1 -1
  58. data/lib/aspera/cli/security.rb +102 -4
  59. data/lib/aspera/{preview/terminal.rb → cli/terminal_image.rb} +93 -94
  60. data/lib/aspera/cli/vault_manager.rb +3 -3
  61. data/lib/aspera/cli/version.rb +1 -1
  62. data/lib/aspera/cli/wizard.rb +9 -3
  63. data/lib/aspera/coverage.rb +1 -2
  64. data/lib/aspera/line_logger.rb +35 -6
  65. data/lib/aspera/log.rb +3 -2
  66. data/lib/aspera/oauth/client/base.rb +19 -1
  67. data/lib/aspera/oauth/client/factory.rb +1 -7
  68. data/lib/aspera/plugin.schema.yaml +178 -0
  69. data/lib/aspera/plugins/alee/api.rb +25 -0
  70. data/lib/aspera/{cli/plugins/alee.rb → plugins/alee/commands.rb} +10 -8
  71. data/lib/aspera/plugins/alee/plugin.yaml +3 -0
  72. data/lib/aspera/plugins/aoc/admin_resources.rb +148 -0
  73. data/lib/aspera/plugins/aoc/api.rb +783 -0
  74. data/lib/aspera/{cli/plugins/aoc.rb → plugins/aoc/commands.rb} +404 -330
  75. data/lib/aspera/plugins/aoc/graphql.rb +45 -0
  76. data/lib/aspera/plugins/aoc/plugin.yaml +31 -0
  77. data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud API-0.2.6-enhanced.yaml +15042 -7076
  78. data/lib/aspera/plugins/aoc/schemas/options.yaml +47 -0
  79. data/lib/aspera/plugins/ats/api.rb +51 -0
  80. data/lib/aspera/{cli/plugins/ats.rb → plugins/ats/commands.rb} +33 -33
  81. data/lib/aspera/plugins/ats/plugin.yaml +6 -0
  82. data/lib/aspera/plugins/connect/agent.rb +199 -0
  83. data/lib/aspera/plugins/connect/plugin.yaml +11 -0
  84. data/lib/aspera/plugins/connect/schemas/agent_options.yaml +6 -0
  85. data/lib/aspera/{cli/plugins/console.rb → plugins/console/commands.rb} +41 -33
  86. data/lib/aspera/plugins/console/plugin.yaml +8 -0
  87. data/lib/aspera/plugins/cos/api.rb +114 -0
  88. data/lib/aspera/{cli/plugins/cos.rb → plugins/cos/commands.rb} +19 -16
  89. data/lib/aspera/plugins/cos/plugin.yaml +9 -0
  90. data/lib/aspera/plugins/cos/schemas/options.yaml +30 -0
  91. data/lib/aspera/plugins/desktop/agent.rb +165 -0
  92. data/lib/aspera/plugins/desktop/plugin.yaml +11 -0
  93. data/lib/aspera/plugins/desktop/schemas/agent_options.yaml +6 -0
  94. data/lib/aspera/plugins/direct/agent.rb +518 -0
  95. data/lib/aspera/plugins/direct/plugin.yaml +11 -0
  96. data/lib/aspera/plugins/direct/schemas/agent_options.yaml +86 -0
  97. data/lib/aspera/plugins/faspex4/gateway_options.rb +18 -0
  98. data/lib/aspera/plugins/faspex4/gw_servlet.rb +122 -0
  99. data/lib/aspera/plugins/faspex4/plugin.yaml +7 -0
  100. data/lib/aspera/plugins/faspex4/schemas/options.yaml +57 -0
  101. data/lib/aspera/plugins/faspex5/api.rb +269 -0
  102. data/lib/aspera/{cli/plugins/faspex5.rb → plugins/faspex5/commands.rb} +152 -114
  103. data/lib/aspera/plugins/faspex5/faspex4_post_proc_servlet.rb +241 -0
  104. data/lib/aspera/plugins/faspex5/plugin.yaml +14 -0
  105. data/lib/aspera/{schema → plugins/faspex5/schemas}/IBM Aspera Faspex API-5.0-enhanced.yaml +279 -76
  106. data/lib/aspera/plugins/faspex5/schemas/options.yaml +87 -0
  107. data/lib/aspera/{cli/plugins/faspio.rb → plugins/faspio/commands.rb} +11 -7
  108. data/lib/aspera/plugins/faspio/plugin.yaml +8 -0
  109. data/lib/aspera/{schema → plugins/faspio/schemas}/IBM Aspera faspio Gateway API-1.0.0-enhanced.yaml +4 -2
  110. data/lib/aspera/plugins/httpgw/agent.rb +126 -0
  111. data/lib/aspera/plugins/httpgw/api.rb +411 -0
  112. data/lib/aspera/{cli/plugins/httpgw.rb → plugins/httpgw/commands.rb} +9 -9
  113. data/lib/aspera/plugins/httpgw/plugin.yaml +12 -0
  114. data/lib/aspera/plugins/httpgw/schemas/agent_options.yaml +24 -0
  115. data/lib/aspera/plugins/node/agent.rb +184 -0
  116. data/lib/aspera/plugins/node/api.rb +699 -0
  117. data/lib/aspera/{cli/plugins/node.rb → plugins/node/commands.rb} +166 -141
  118. data/lib/aspera/plugins/node/emulator.rb +774 -0
  119. data/lib/aspera/plugins/node/plugin.yaml +20 -0
  120. data/lib/aspera/plugins/node/schemas/agent_options.yaml +20 -0
  121. data/lib/aspera/plugins/node/schemas/options.yaml +190 -0
  122. data/lib/aspera/{cli/plugins/orchestrator.rb → plugins/orchestrator/commands.rb} +139 -110
  123. data/lib/aspera/plugins/orchestrator/plugin.yaml +34 -0
  124. data/lib/aspera/{schema → plugins/orchestrator/schemas}/IBM Aspera Orchestrator API-v1-enhanced.yaml +486 -16
  125. data/lib/aspera/plugins/orchestrator/schemas/options.yaml +143 -0
  126. data/lib/aspera/plugins/orchestrator/workflow_checker.rb +692 -0
  127. data/lib/aspera/{cli/plugins/preview.rb → plugins/preview/commands.rb} +53 -51
  128. data/lib/aspera/plugins/preview/file_types.rb +121 -0
  129. data/lib/aspera/plugins/preview/generator.rb +350 -0
  130. data/lib/aspera/plugins/preview/plugin.yaml +12 -0
  131. data/lib/aspera/{preview/options.schema.yaml → plugins/preview/schemas/options.yaml} +1 -1
  132. data/lib/aspera/plugins/preview/utils.rb +213 -0
  133. data/lib/aspera/plugins/server/ascmd.rb +374 -0
  134. data/lib/aspera/{cli/plugins/server.rb → plugins/server/commands.rb} +48 -32
  135. data/lib/aspera/plugins/server/plugin.yaml +9 -0
  136. data/lib/aspera/plugins/server/schemas/options.yaml +30 -0
  137. data/lib/aspera/{cli/plugins/shares.rb → plugins/shares/commands.rb} +29 -21
  138. data/lib/aspera/plugins/shares/plugin.yaml +10 -0
  139. data/lib/aspera/plugins/shares/shares_web.rb +97 -0
  140. data/lib/aspera/plugins/transferd/agent.rb +258 -0
  141. data/lib/aspera/plugins/transferd/api.rb +180 -0
  142. data/lib/aspera/plugins/transferd/client.rb +67 -0
  143. data/lib/aspera/{cli/plugins/transferd.rb → plugins/transferd/commands.rb} +43 -42
  144. data/lib/aspera/plugins/transferd/config_commands.rb +38 -0
  145. data/lib/aspera/plugins/transferd/plugin.yaml +27 -0
  146. data/lib/aspera/plugins/transferd/product.rb +146 -0
  147. data/lib/aspera/plugins/transferd/schemas/agent_options.yaml +31 -0
  148. data/lib/aspera/plugins/transferd/schemas/options.yaml +80 -0
  149. data/lib/aspera/plugins/transferd/service.rb +617 -0
  150. data/lib/aspera/plugins/xfer/ascp_actions.rb +131 -0
  151. data/lib/aspera/plugins/xfer/ascp_logs_actions.rb +198 -0
  152. data/lib/aspera/plugins/xfer/async_transfer_store.rb +131 -0
  153. data/lib/aspera/plugins/xfer/config_commands.rb +119 -0
  154. data/lib/aspera/plugins/xfer/helper.rb +19 -0
  155. data/lib/aspera/plugins/xfer/plugin.yaml +42 -0
  156. data/lib/aspera/plugins/xfer/schemas/agent_extension.schema.yaml +26 -0
  157. data/lib/aspera/plugins/xfer/schemas/options.yaml +125 -0
  158. data/lib/aspera/plugins/xfer/sync_actions.rb +253 -0
  159. data/lib/aspera/plugins/xfer/transfer_actions.rb +176 -0
  160. data/lib/aspera/plugins/xfer/transfer_agent.rb +505 -0
  161. data/lib/aspera/products/cli_v3.rb +34 -0
  162. data/lib/aspera/products/connect.rb +1 -1
  163. data/lib/aspera/products/drive.rb +29 -0
  164. data/lib/aspera/products/hsts.rb +34 -0
  165. data/lib/aspera/products/transferd.rb +25 -3
  166. data/lib/aspera/products.rb +54 -0
  167. data/lib/aspera/prompt.rb +184 -0
  168. data/lib/aspera/proxy_auto_config.rb +3 -3
  169. data/lib/aspera/rest/aspera_errors.rb +4 -38
  170. data/lib/aspera/rest/error_analyzer.rb +6 -2
  171. data/lib/aspera/rest/list.rb +4 -5
  172. data/lib/aspera/schema/reader.rb +15 -0
  173. data/lib/aspera/schema/registry.rb +239 -45
  174. data/lib/aspera/schema/validator.rb +63 -17
  175. data/lib/aspera/sealed_hash.rb +4 -0
  176. data/lib/aspera/secret/hider.rb +159 -24
  177. data/lib/aspera/secret/reference.rb +8 -0
  178. data/lib/aspera/secret/resolver.rb +8 -4
  179. data/lib/aspera/secret/store/base.rb +10 -9
  180. data/lib/aspera/secret/store/encrypted_hash.rb +6 -6
  181. data/lib/aspera/secret/store/hashicorp_vault.rb +3 -3
  182. data/lib/aspera/secret/store/keeper.rb +2 -2
  183. data/lib/aspera/secret/store/macos_security.rb +2 -3
  184. data/lib/aspera/secret/store/one_password_api.rb +2 -2
  185. data/lib/aspera/secret/store/one_password_cli.rb +3 -3
  186. data/lib/aspera/secret/store/secret_service.rb +2 -2
  187. data/lib/aspera/ssl.rb +3 -3
  188. data/lib/aspera/standard_input.rb +20 -0
  189. data/lib/aspera/{schema → sync}/async_tables.yaml +1 -1
  190. data/lib/aspera/sync/database.rb +7 -0
  191. data/lib/aspera/sync/log_parser.rb +138 -0
  192. data/lib/aspera/sync/operations.rb +59 -34
  193. data/lib/aspera/transfer/command_line_builder.rb +248 -0
  194. data/lib/aspera/transfer/command_line_converter.rb +37 -0
  195. data/lib/aspera/transfer/faux_file.rb +2 -2
  196. data/lib/aspera/transfer/parameters.rb +9 -10
  197. data/lib/aspera/transfer/spec.rb +28 -1
  198. data/lib/aspera/transfer/spec.schema.yaml +30 -5
  199. data/lib/aspera/units/decimal.rb +1 -1
  200. data/lib/aspera/units/timestamp.rb +84 -0
  201. data/lib/aspera/uri_reader.rb +2 -2
  202. data/lib/aspera/web_auth/paste.rb +2 -2
  203. data/lib/aspera/web_server_simple.rb +10 -4
  204. data.tar.gz.sig +0 -0
  205. metadata +137 -80
  206. metadata.gz.sig +0 -0
  207. data/lib/aspera/agent/connect.rb +0 -188
  208. data/lib/aspera/agent/desktop.rb +0 -154
  209. data/lib/aspera/agent/direct.rb +0 -496
  210. data/lib/aspera/agent/httpgw.rb +0 -100
  211. data/lib/aspera/agent/node.rb +0 -166
  212. data/lib/aspera/agent/transferd.rb +0 -341
  213. data/lib/aspera/api/alee.rb +0 -19
  214. data/lib/aspera/api/aoc.rb +0 -776
  215. data/lib/aspera/api/ats.rb +0 -49
  216. data/lib/aspera/api/cos_node.rb +0 -107
  217. data/lib/aspera/api/faspex.rb +0 -258
  218. data/lib/aspera/api/httpgw.rb +0 -409
  219. data/lib/aspera/api/node.rb +0 -684
  220. data/lib/aspera/ascmd.rb +0 -248
  221. data/lib/aspera/cli/ascp_actions.rb +0 -113
  222. data/lib/aspera/cli/ascp_logs_actions.rb +0 -188
  223. data/lib/aspera/cli/async_transfer_store.rb +0 -129
  224. data/lib/aspera/cli/command_registry.rb +0 -371
  225. data/lib/aspera/cli/command_spec.rb +0 -273
  226. data/lib/aspera/cli/option_declarator.rb +0 -74
  227. data/lib/aspera/cli/option_registry.rb +0 -72
  228. data/lib/aspera/cli/option_types.rb +0 -120
  229. data/lib/aspera/cli/option_value.rb +0 -345
  230. data/lib/aspera/cli/prompt.rb +0 -186
  231. data/lib/aspera/cli/sync_actions.rb +0 -181
  232. data/lib/aspera/cli/transfer_actions.rb +0 -98
  233. data/lib/aspera/cli/transfer_agent.rb +0 -348
  234. data/lib/aspera/command_line_builder.rb +0 -244
  235. data/lib/aspera/command_line_converter.rb +0 -35
  236. data/lib/aspera/data_repository.rb +0 -69
  237. data/lib/aspera/faspex_gw.rb +0 -172
  238. data/lib/aspera/faspex_postproc.rb +0 -237
  239. data/lib/aspera/graphql.rb +0 -41
  240. data/lib/aspera/node_emulator.rb +0 -758
  241. data/lib/aspera/oauth/client/shares_web.rb +0 -94
  242. data/lib/aspera/orchestrator_workflow_checker.rb +0 -688
  243. data/lib/aspera/preview/file_types.rb +0 -119
  244. data/lib/aspera/preview/generator.rb +0 -345
  245. data/lib/aspera/preview/utils.rb +0 -211
  246. data/lib/aspera/products/other.rb +0 -92
  247. data/lib/aspera/products/transferd_service.rb +0 -610
  248. /data/lib/aspera/{data → ascp/data}/1 +0 -0
  249. /data/lib/aspera/{data → ascp/data}/2 +0 -0
  250. /data/lib/aspera/{data → ascp/data}/3 +0 -0
  251. /data/lib/aspera/{data → ascp/data}/4 +0 -0
  252. /data/lib/aspera/{data → ascp/data}/5 +0 -0
  253. /data/lib/aspera/{data → ascp/data}/6 +0 -0
  254. /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_account.graphql +0 -0
  255. /data/lib/aspera/{api → plugins/aoc}/queries/bss_subscription_usage.graphql +0 -0
  256. /data/lib/aspera/{schema → plugins/aoc/schemas}/IBM Aspera on Cloud Automation API-1.0.5-enhanced.yaml +0 -0
  257. /data/lib/aspera/{schema → plugins/console/schemas}/IBM Aspera Console-enhanced.yaml +0 -0
  258. /data/lib/aspera/{schema → plugins/node/schemas}/IBM Aspera Node API-4.4.6.yaml +0 -0
  259. /data/lib/aspera/{preview → plugins/preview}/image_error.png +0 -0
  260. /data/lib/aspera/{preview → plugins/preview}/video_error.png +0 -0
  261. /data/lib/aspera/{schema → plugins/shares/schemas}/IBM_Aspera_Shares.yaml +0 -0
  262. /data/lib/{transferd_pb.rb → aspera/plugins/transferd/grpc/transferd_pb.rb} +0 -0
  263. /data/lib/{transferd_services_pb.rb → aspera/plugins/transferd/grpc/transferd_services_pb.rb} +0 -0
  264. /data/lib/aspera/{schema → plugins/transferd/schemas}/transferd-config.schema.json +0 -0
@@ -0,0 +1,350 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ # ffmpeg options:
7
+ # spellchecker:ignore soffice pauseframes libx264 trunc bufsize muxer apng libmp3lame maxrate posterize movflags faststart
8
+ # spellchecker:ignore palettegen paletteuse pointsize bordercolor repage lanczos unoconv optipng reencode conv transframes
9
+
10
+ require_relative 'utils'
11
+ require_relative 'file_types'
12
+ require_relative '../../schema/registry'
13
+ require_relative '../../log'
14
+ require_relative '../../assert'
15
+ require 'fileutils'
16
+
17
+ module Aspera
18
+ module Plugins
19
+ module Preview
20
+ # Generates one preview file for one format for one file at a time.
21
+ class Generator
22
+ # Values for preview_format: output format.
23
+ PREVIEW_FORMATS = %i[png mp4].freeze
24
+
25
+ # List of valid ffmpeg option keys for reencode configuration.
26
+ FFMPEG_OPTIONS_LIST = %w[in out].freeze
27
+
28
+ # Key of the schema of the generator options in the bundle (`schemas/options.yaml`)
29
+ OPTIONS_SCHEMA = 'preview'
30
+
31
+ # Generator options, members defined in `schemas/options.yaml`.
32
+ Options = Struct.new(*Schema::Registry.instance.reader(OPTIONS_SCHEMA).current['properties'].keys.map(&:to_sym))
33
+
34
+ # CLI needs to know conversion type to know if need skip it.
35
+ # One of CONVERSION_TYPES.
36
+ attr_reader :conversion_type, :destination
37
+
38
+ # Node API MIME types are from: http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types.
39
+ # The resulting preview file type is taken from destination file extension.
40
+ # Conversion methods are provided by private methods: convert_<conversion_type>_to_<preview_format>.
41
+ # conversion_type: one of FileTypes::CONVERSION_TYPES.
42
+ # preview_format: one of Generator::PREVIEW_FORMATS.
43
+ # The conversion video->mp4 is implemented in methods: convert_video_to_mp4_using_<video_conversion>.
44
+ # conversion method: one of Generator::VIDEO_CONVERSION_METHODS.
45
+ # @param src [String] Source file path.
46
+ # @param dst [String] Destination file path.
47
+ # @param options [Options] All conversion options.
48
+ # @param main_temp_dir [String] Main temp folder, sub folder will be created for generation.
49
+ # @param mime [String, nil] Optional MIME type as provided by node api (or nil).
50
+ def initialize(src, dst, options, main_temp_dir, mime: nil)
51
+ # Source file path
52
+ @source = src
53
+ # Destination file path
54
+ @destination = dst
55
+ @options = options
56
+ # temp folder name based on source file
57
+ @temp_folder = File.join(main_temp_dir, @source.split('/').last.gsub(/\s/, '_').gsub(/\W/, ''))
58
+ # Extract preview format from extension of target file.
59
+ @preview_format_sym = File.extname(@destination).gsub(/^\./, '').to_sym
60
+ conversion_type = FileTypes.instance.conversion_type(@source, mime)
61
+ @processing_method = "convert_#{conversion_type}_to_#{@preview_format_sym}"
62
+ if conversion_type.eql?(:video)
63
+ case @preview_format_sym
64
+ when :mp4
65
+ @processing_method = "#{@processing_method}_using_#{@options.video_conversion}"
66
+ when :png
67
+ @processing_method = "#{@processing_method}_using_#{@options.video_png_conv}"
68
+ end
69
+ end
70
+ @processing_method = @processing_method.to_sym
71
+ Log.log.debug { "method: #{@processing_method}" }
72
+ Aspera.assert(respond_to?(@processing_method, true)) { "no processing known for #{conversion_type} -> #{@preview_format_sym}" }
73
+ command = [:magick] + %w[identify -list font]
74
+ magick_fonts = Utils.parse_magick_fonts(Utils.execute(*command, mode: :capture).first)
75
+ Aspera.assert(magick_fonts[:fonts].any? { |f| f[:name].eql?(@options.thumb_text_font) }, type: EnvironmentError) { "Missing font #{@options.thumb_text_font} in #{command}" }
76
+ end
77
+
78
+ # Creates preview as specified in constructor.
79
+ def generate
80
+ Log.log.debug { "#{@source}->#{@destination} (#{@processing_method})" }
81
+ begin
82
+ send(@processing_method)
83
+ # Check that generated size does not exceed maximum.
84
+ result_size = File.size(@destination)
85
+ Log.log.warn { "preview size exceeds maximum allowed #{result_size} > #{@options.max_size}" } if result_size > @options.max_size
86
+ ensure
87
+ FileUtils.rm_rf(@temp_folder)
88
+ end
89
+ end
90
+
91
+ # Path to error image corresponding to preview type.
92
+ # @return [String] The path to the error image.
93
+ def error_asset
94
+ File.expand_path(@preview_format_sym.eql?(:mp4) ? 'video_error.png' : 'image_error.png', File.dirname(__FILE__))
95
+ end
96
+
97
+ private
98
+
99
+ # Creates a unique temp folder for file.
100
+ # @return [String] The temporary folder path.
101
+ def this_tmpdir
102
+ FileUtils.mkdir_p(@temp_folder)
103
+ return @temp_folder
104
+ end
105
+
106
+ # Calculates offset in seconds for video frame extraction.
107
+ # @param duration [Float] Duration of video in seconds.
108
+ # @param start_offset [Numeric] Start offset of parts in seconds.
109
+ # @param total_count [Integer] Total count of parts.
110
+ # @param index [Integer] Index of part (starts at 1).
111
+ # @return [Float] Offset in seconds suitable for ffmpeg -ss option.
112
+ def get_offset(duration, start_offset, total_count, index)
113
+ Aspera.assert_type(duration, Float) { 'duration' }
114
+ return start_offset + ((index - 1) * (duration - start_offset) / total_count)
115
+ end
116
+
117
+ # Converts video to MP4 using blend method.
118
+ # Extracts key frames and blends them with transitions.
119
+ def convert_video_to_mp4_using_blend
120
+ p_duration = Utils.video_get_duration(@source)
121
+ p_start_offset = @options.video_start_sec.to_i
122
+ p_key_frame_count = @options.blend_keyframes.to_i
123
+ last_keyframe = nil
124
+ current_index = 1
125
+ frame_rate_hz = 30
126
+ 1.upto(p_key_frame_count) do |i|
127
+ Utils.video_dump_frame(
128
+ @source,
129
+ get_offset(p_duration, p_start_offset, p_key_frame_count, i),
130
+ @options.video_scale,
131
+ Utils.get_tmp_num_filepath(this_tmpdir, current_index)
132
+ )
133
+ Utils.video_dupe_frame(this_tmpdir, current_index, @options.blend_pauseframes)
134
+ Utils.video_blend_frames(this_tmpdir, last_keyframe, current_index) unless last_keyframe.nil?
135
+ # Go to last dupe frame.
136
+ last_keyframe = current_index + @options.blend_pauseframes
137
+ # Go after last dupe frame and keep space to blend.
138
+ current_index = last_keyframe + 1 + @options.blend_transframes
139
+ end
140
+ Utils.ffmpeg(
141
+ in: [
142
+ Utils.ffmpeg_fmt(this_tmpdir),
143
+ '-framerate', @options.blend_fps
144
+ ],
145
+ out: [
146
+ @destination,
147
+ '-filter:v', "scale='trunc(iw/2)*2:trunc(ih/2)*2'",
148
+ '-codec:v', 'libx264',
149
+ '-r', frame_rate_hz,
150
+ '-pix_fmt', 'yuv420p'
151
+ ]
152
+ )
153
+ end
154
+
155
+ # Converts video to MP4 using clips method.
156
+ # Generates n clips starting at offset and concatenates them.
157
+ def convert_video_to_mp4_using_clips
158
+ p_duration = Utils.video_get_duration(@source)
159
+ file_list_file = File.join(this_tmpdir, 'clip_files.txt')
160
+ File.open(file_list_file, 'w+') do |f|
161
+ 1.upto(@options.clips_count.to_i) do |i|
162
+ offset_seconds = get_offset(p_duration, @options.video_start_sec.to_i, @options.clips_count.to_i, i)
163
+ tmp_file_name = format('clip%04d.mp4', i)
164
+ Utils.ffmpeg(
165
+ in: [
166
+ @source,
167
+ '-ss', offset_seconds * 0.9
168
+ ],
169
+ out: [
170
+ File.join(this_tmpdir, tmp_file_name),
171
+ '-ss', offset_seconds * 0.1,
172
+ '-t', @options.clips_length,
173
+ '-filter:v', "scale='#{@options.video_scale}'",
174
+ '-codec:a', 'libmp3lame'
175
+ ]
176
+ )
177
+ f.puts("file '#{tmp_file_name}'")
178
+ end
179
+ end
180
+ # Concat clips.
181
+ Utils.ffmpeg(
182
+ in: [file_list_file, '-f', 'concat'],
183
+ out: [@destination, '-codec', 'copy']
184
+ )
185
+ File.delete(file_list_file)
186
+ end
187
+
188
+ # Converts video to MP4 using re-encoding method.
189
+ # Performs a simple re-encoding with configurable ffmpeg options.
190
+ def convert_video_to_mp4_using_reencode
191
+ options = @options.reencode_ffmpeg
192
+ Aspera.assert_type(options, Hash, type: ParameterError) { 'reencode_ffmpeg' }
193
+ options.each do |k, v|
194
+ Aspera.assert_values(k, FFMPEG_OPTIONS_LIST, type: ParameterError) { 'key' }
195
+ Aspera.assert_type(v, Array, type: ParameterError) { k }
196
+ end
197
+ codec = @options.video_codec || Utils.available_h264_encoder
198
+ in_opts = options['in'] || ['-ss', @options.video_start_sec.to_i * 0.9]
199
+ out_opts = options['out'] || [
200
+ '-t', 60,
201
+ '-codec:v', codec,
202
+ '-profile:v', 'high',
203
+ '-pix_fmt', 'yuv420p',
204
+ '-preset', 'slow',
205
+ '-b:v', '500k',
206
+ '-maxrate', '500k',
207
+ '-bufsize', '1000k',
208
+ '-filter:v', "scale='#{@options.video_scale}'",
209
+ '-threads', '0',
210
+ '-codec:a', 'libmp3lame',
211
+ '-ac', '2',
212
+ '-b:a', '128k',
213
+ '-movflags', 'faststart'
214
+ ]
215
+ Utils.ffmpeg(
216
+ in: [@source, *in_opts],
217
+ out: [@destination, *out_opts]
218
+ )
219
+ end
220
+
221
+ # Converts video to PNG using fixed frame method.
222
+ # Generates a static thumbnail at a specific time offset.
223
+ def convert_video_to_png_using_fixed
224
+ Utils.video_dump_frame(
225
+ @source,
226
+ Utils.video_get_duration(@source) * @options.thumb_vid_fraction,
227
+ @options.thumb_vid_scale,
228
+ @destination
229
+ )
230
+ end
231
+
232
+ # Converts video to animated PNG (APNG).
233
+ # Creates an animated thumbnail with looping.
234
+ # @see https://trac.ffmpeg.org/wiki/SponsoringPrograms/GSoC/2015#AnimatedPortableNetworkGraphicsAPNG
235
+ def convert_video_to_png_using_animated
236
+ p_duration = Utils.video_get_duration(@source)
237
+ p_start_offset = @options.video_start_sec.to_i
238
+ p_max_duration = @options.clips_length.to_i
239
+ # If video is shorter than start offset + duration, adjust to capture from start.
240
+ if p_duration <= (p_start_offset + p_max_duration)
241
+ p_start_offset = 0
242
+ p_max_duration = p_duration
243
+ end
244
+ Utils.ffmpeg(
245
+ in: [
246
+ @source,
247
+ '-ss', p_start_offset,
248
+ '-t', p_max_duration
249
+ ],
250
+ out: [
251
+ @destination,
252
+ '-vf', 'fps=5,scale=120:-1:flags=lanczos',
253
+ '-plays', 0, # Loop forever (0 = infinite loop for APNG).
254
+ '-f', 'apng'
255
+ ]
256
+ )
257
+ end
258
+
259
+ # Converts office document to PNG.
260
+ # First converts to PDF, then to PNG image.
261
+ def convert_office_to_png
262
+ tmp_pdf_file = File.join(this_tmpdir, "#{File.basename(@source, File.extname(@source))}.pdf")
263
+ case @options.office_conversion
264
+ when :unoconv
265
+ # `unoconv` is a python wrapper over `soffice`
266
+ Utils.silent_execute(
267
+ :unoconv,
268
+ '-f', 'pdf',
269
+ '-o', tmp_pdf_file,
270
+ @source
271
+ )
272
+ when :soffice
273
+ Utils.silent_execute(
274
+ :soffice,
275
+ '--headless',
276
+ '--invisible',
277
+ '--nocrashreport',
278
+ '--nodefault',
279
+ '--nofirststartwizard',
280
+ '--nologo',
281
+ '--norestore',
282
+ '--convert-to', 'pdf',
283
+ '--outdir', File.dirname(tmp_pdf_file),
284
+ @source
285
+ )
286
+ # soffice creates the file with the source name, so we need to rename it if needed.
287
+ generated_pdf = File.join(File.dirname(tmp_pdf_file), "#{File.basename(@source, File.extname(@source))}.pdf")
288
+ FileUtils.mv(generated_pdf, tmp_pdf_file) if generated_pdf != tmp_pdf_file
289
+ else Aspera.error_unexpected_value(@options.office_conversion) { 'office_conversion' }
290
+ end
291
+ convert_pdf_to_png(tmp_pdf_file)
292
+ end
293
+
294
+ # Converts PDF to PNG image.
295
+ # @param source_file_path [String, nil] Optional source file path, defaults to @source.
296
+ def convert_pdf_to_png(source_file_path = nil)
297
+ source_file_path ||= @source
298
+ Utils.silent_execute(
299
+ :magick,
300
+ 'convert',
301
+ '-size', "x#{@options.thumb_img_size}",
302
+ '-background', 'white',
303
+ '-flatten',
304
+ "#{source_file_path}[0]",
305
+ @destination
306
+ )
307
+ end
308
+
309
+ # Converts image to PNG thumbnail.
310
+ # Applies auto-orientation, resizing, and optimization.
311
+ def convert_image_to_png
312
+ Utils.silent_execute(
313
+ :magick,
314
+ 'convert',
315
+ '-auto-orient',
316
+ '-thumbnail', "#{@options.thumb_img_size}x#{@options.thumb_img_size}>",
317
+ '-quality', 95,
318
+ '+dither',
319
+ '-posterize', 40,
320
+ "#{@source}[0]",
321
+ @destination
322
+ )
323
+ Utils.silent_execute(:optipng, @destination)
324
+ end
325
+
326
+ # Converts plain text to PNG image.
327
+ # Renders first 100 lines of text file as an image.
328
+ def convert_plaintext_to_png
329
+ # Get 100 first lines of text file.
330
+ first_lines = File.foreach(@source).first(100).join
331
+ Utils.silent_execute(
332
+ :magick,
333
+ 'convert',
334
+ '-size', "#{@options.thumb_img_size}x#{@options.thumb_img_size}",
335
+ 'xc:white', # Define canvas with background color (xc, or canvas) of preceding size.
336
+ '-font', @options.thumb_text_font,
337
+ '-pointsize', 12,
338
+ '-fill', 'black', # Font color.
339
+ '-annotate', '+0+0', first_lines,
340
+ '-trim', # Avoid large blank regions.
341
+ '-bordercolor', 'white',
342
+ '-border', 8,
343
+ '+repage',
344
+ @destination
345
+ )
346
+ end
347
+ end
348
+ end
349
+ end
350
+ end
@@ -0,0 +1,12 @@
1
+ name: preview
2
+ title: Preview generation for Aspera access keys
3
+ depends:
4
+ - node
5
+ - xfer
6
+ gems:
7
+ - marcel
8
+ commands: true
9
+ schemas:
10
+ - key: preview
11
+ file: schemas/options.yaml
12
+ owned: true
@@ -1,5 +1,5 @@
1
1
  $schema: https://json-schema.org/draft/2020-12/schema
2
- $id: https://github.com/IBM/aspera-cli/tree/main/lib/aspera/preview/options.schema.yaml
2
+ $id: https://github.com/IBM/aspera-cli/tree/main/lib/aspera/plugins/preview/schemas/options.yaml
3
3
  title: Preview generator options
4
4
  description: >-
5
5
  Options of the preview generator: tools and parameters used to generate PNG thumbnails and MP4 video previews.
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SPDX-FileCopyrightText: IBM Corp.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ require_relative '../../log'
7
+ require_relative '../../assert'
8
+ require 'English'
9
+ require 'tmpdir'
10
+ require 'fileutils'
11
+ require 'open3'
12
+
13
+ module Aspera
14
+ module Plugins
15
+ module Preview
16
+ class Utils
17
+ # External binaries used
18
+ EXTERNAL_TOOLS = %i[ffmpeg ffprobe magick optipng unoconv soffice].freeze
19
+ # File name format for temporary files, used by both ffmpeg and ruby(Kernel.format)
20
+ TEMP_FORMAT = 'img%04d.jpg'
21
+ # default parameters for ffmpeg
22
+ FFMPEG_DEFAULT_PARAMS = [
23
+ '-y', # overwrite output without asking
24
+ '-loglevel', 'error' # show only errors and up
25
+ ].freeze
26
+ # Preferred H.264 encoders in order: best quality/compatibility first.
27
+ H264_ENCODER_PREFERENCE = %w[libx264 libopenh264 h264_nvenc h264_amf h264_qsv h264_vaapi h264_v4l2m2m].freeze
28
+ private_constant :EXTERNAL_TOOLS, :TEMP_FORMAT, :FFMPEG_DEFAULT_PARAMS, :H264_ENCODER_PREFERENCE
29
+
30
+ class << self
31
+ # @param tool [Symbol] either `unoconv` or `soffice`
32
+ attr_accessor :office_tool
33
+
34
+ # Return the first H.264 encoder available in the local ffmpeg installation.
35
+ # Result is memoized after the first call.
36
+ # @return [String] encoder name (e.g. 'libx264', 'libopenh264')
37
+ # @raise [EnvironmentError] if no supported H.264 encoder is found
38
+ def available_h264_encoder
39
+ return @available_h264_encoder if defined?(@available_h264_encoder)
40
+ stdout, = execute(:ffmpeg, '-encoders', mode: :capture, exception: false)
41
+ available = stdout.lines.grep(/h264/i).map { |l| l.split[1] }
42
+ @available_h264_encoder = H264_ENCODER_PREFERENCE.find { |enc| available.include?(enc) }
43
+ Aspera.assert(@available_h264_encoder, type: EnvironmentError) { "No supported H.264 encoder found in ffmpeg. Available: #{available.join(', ')}" }
44
+ @available_h264_encoder
45
+ end
46
+
47
+ # Check that external tools can be executed.
48
+ # @param skip_types [Array<Symbol>] list of tools to skip
49
+ # @raise [EnvironmentError] if a required tool binary is missing
50
+ # @return [nil]
51
+ def check_tools(skip_types = [])
52
+ tools_to_check = EXTERNAL_TOOLS.dup
53
+ tools_to_check.delete(:unoconv) if skip_types.include?(:office) || office_tool.eql?(:soffice)
54
+ tools_to_check.delete(:soffice) if skip_types.include?(:office) || office_tool.eql?(:unoconv)
55
+ # Check for binaries
56
+ tools_to_check.each do |command_sym|
57
+ silent_execute(command_sym, '-h')
58
+ rescue Errno::ENOENT => e
59
+ raise EnvironmentError, "missing #{command_sym} binary: #{e}"
60
+ rescue
61
+ nil
62
+ end
63
+ end
64
+
65
+ # Execute external command, verify it is in the supported list.
66
+ # @param args [Array] command name followed by CLI arguments
67
+ # @param kwargs [Hash] execution options passed to {Environment.secure_execute}
68
+ # @raise [Aspera::AssertError] if the command is not in {EXTERNAL_TOOLS}
69
+ # @return [Array<String>] captured stdout and stderr lines depending on mode
70
+ def execute(*args, **kwargs)
71
+ Aspera.assert_values(args.first, EXTERNAL_TOOLS) { 'command' }
72
+ Environment.secure_execute(*args, **kwargs)
73
+ end
74
+
75
+ # Execute external command, capturing and discarding output unless it fails.
76
+ # On failure, the captured stderr is included in the raised exception message.
77
+ # @param args [Array] command name followed by CLI arguments
78
+ # @return [nil]
79
+ def silent_execute(*args)
80
+ execute(*args, mode: :capture)
81
+ nil
82
+ end
83
+
84
+ # Execute `ffmpeg`, capturing output.
85
+ # On failure, the ffmpeg stderr is logged at debug level and re-raised.
86
+ # @param in [Array] input file path followed by input options
87
+ # @param out [Array] output file path followed by output options
88
+ # @param global [Array<String>] global options for ffmpeg
89
+ # @return [nil]
90
+ def ffmpeg(in:, out:, global: FFMPEG_DEFAULT_PARAMS)
91
+ Aspera.assert_type(global, Array)
92
+ # NOTE: cannot use just "in", as it is a reserved word in ruby
93
+ in_args = binding.local_variable_get(:in).dup
94
+ out_args = out.dup
95
+ Aspera.assert_type(in_args, Array)
96
+ Aspera.assert_type(out_args, Array)
97
+ in_file = in_args.shift
98
+ out_file = out_args.shift
99
+ execute(:ffmpeg, *global, *in_args, '-i', in_file, *out_args, out_file, mode: :capture)
100
+ nil
101
+ end
102
+
103
+ # Get duration of a video file using `ffprobe`.
104
+ # @param input_file [String] path to video file
105
+ # @return [Float] duration in seconds
106
+ def video_get_duration(input_file)
107
+ return execute(
108
+ :ffprobe,
109
+ '-loglevel', 'error',
110
+ '-show_entries', 'format=duration',
111
+ '-print_format', 'default=noprint_wrappers=1:nokey=1',
112
+ input_file,
113
+ mode: :capture
114
+ ).first.to_f
115
+ end
116
+
117
+ # File output pattern for ffmpeg, including temp folder.
118
+ # @param temp_folder [String] path to temp folder
119
+ # @return [String] file path pattern
120
+ def ffmpeg_fmt(temp_folder)
121
+ return File.join(temp_folder, TEMP_FORMAT)
122
+ end
123
+
124
+ # Get numbered temporary file path.
125
+ # @param temp_folder [String] path to temp folder
126
+ # @param file_number [Integer] frame index
127
+ # @return [String] file path
128
+ def get_tmp_num_filepath(temp_folder, file_number)
129
+ return File.join(temp_folder, format(TEMP_FORMAT, file_number))
130
+ end
131
+
132
+ # Duplicate a video frame by creating symlinks.
133
+ # @param temp_folder [String] path to temp folder
134
+ # @param index [Integer] frame index to duplicate
135
+ # @param count [Integer] number of duplicate frames to create
136
+ # @return [nil]
137
+ def video_dupe_frame(temp_folder, index, count)
138
+ input_file = get_tmp_num_filepath(temp_folder, index)
139
+ 1.upto(count) do |i|
140
+ FileUtils.ln_s(input_file, get_tmp_num_filepath(temp_folder, index + i))
141
+ end
142
+ nil
143
+ end
144
+
145
+ # Blend transition frames between two keyframes using ImageMagick.
146
+ # @param temp_folder [String] path to temp folder
147
+ # @param index_begin [Integer] starting frame index
148
+ # @param index_end [Integer] ending frame index
149
+ # @return [nil]
150
+ def video_blend_frames(temp_folder, index_begin, index_end)
151
+ img1 = get_tmp_num_filepath(temp_folder, index_begin)
152
+ img2 = get_tmp_num_filepath(temp_folder, index_end)
153
+ count = index_end - index_begin - 1
154
+ 1.upto(count) do |i|
155
+ percent = i * 100 / (count + 1)
156
+ filename = get_tmp_num_filepath(temp_folder, index_begin + i)
157
+ silent_execute(:magick, 'composite', '-blend', percent, img2, img1, filename)
158
+ end
159
+ nil
160
+ end
161
+
162
+ # Dump a frame from a video file
163
+ # @param input_file [String] the input file path
164
+ # @param offset_seconds [Integer] the offset in seconds
165
+ # @param scale [String] the scale of the output frame
166
+ # @param output_file [String] the output file path
167
+ # @return [nil]
168
+ def video_dump_frame(input_file, offset_seconds, scale, output_file)
169
+ ffmpeg(
170
+ in: [input_file, '-ss', offset_seconds],
171
+ out: [output_file, '-frames:v', 1, '-filter:v', "scale='#{scale}'"]
172
+ )
173
+ end
174
+
175
+ # Parse the output of `magick identify -list font` command
176
+ # @param output [String] the output from `magick -list font`
177
+ # @return [Hash] with keys :path and :fonts
178
+ # :path [String] the path to the type.xml file
179
+ # :fonts [Array<Hash>] array of font hashes with keys:
180
+ # :name, :family, :style, :stretch, :weight, :metrics, :glyphs, :index
181
+ def parse_magick_fonts(output)
182
+ result = {path: nil, fonts: []}
183
+ current_font = nil
184
+ output.each_line do |line|
185
+ line = line.strip
186
+ # Parse the Path line
187
+ if line.start_with?('Path:')
188
+ result[:path] = line.sub(/^Path:\s*/, '')
189
+ # Parse Font name
190
+ elsif line.start_with?('Font:')
191
+ # Save previous font if exists
192
+ result[:fonts] << current_font if current_font
193
+ # Start new font
194
+ current_font = {name: line.sub(/^Font:\s*/, '')}
195
+ # Parse font properties
196
+ elsif current_font && line.include?(':')
197
+ key, value = line.split(':', 2)
198
+ key = key.strip.gsub(/\s+/, '_').to_sym
199
+ value = value.strip
200
+ # Convert numeric values
201
+ value = value.to_i if key == :weight || key == :index
202
+ current_font[key] = value
203
+ end
204
+ end
205
+ # Don't forget the last font
206
+ result[:fonts] << current_font if current_font
207
+ result
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end