From e0f7244db72e4bb069f9fc3f84b14c1622fa751c Mon Sep 17 00:00:00 2001 From: Glenn Date: Sun, 31 Dec 2023 13:47:16 +0100 Subject: [PATCH] feat: add support for mime-type --- shard.lock | 6 ++++++ shard.yml | 11 +++++------ src/cli.cr | 8 ++++++-- src/config.cr | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 shard.lock diff --git a/shard.lock b/shard.lock new file mode 100644 index 0000000..fc4f3d1 --- /dev/null +++ b/shard.lock @@ -0,0 +1,6 @@ +version: 2.0 +shards: + magic: + git: https://github.com/dscottboggs/magic.cr.git + version: 1.1.0 + diff --git a/shard.yml b/shard.yml index 4eebce1..f2fd335 100644 --- a/shard.yml +++ b/shard.yml @@ -9,16 +9,15 @@ targets: authors: - Glenn Y. Rolland +dependencies: + magic: + github: dscottboggs/magic.cr + # description: | # Short description of chatgpt-preloader -# dependencies: -# pg: -# github: will/crystal-pg -# version: "~> 0.5" - # development_dependencies: # webmock: # github: manastech/webmock.cr -# license: MIT +license: LGPL-3.0-or-later diff --git a/src/cli.cr b/src/cli.cr index 56080a9..3ac9bb0 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -3,6 +3,7 @@ require "file" require "option_parser" +require "magic" require "./config" @@ -100,10 +101,13 @@ module CodePreloader private def process_file(root_path : String, file_path : String, output_file : IO::FileDescriptor) relative_file_path = file_path.sub(/^#{Regex.escape(root_path)}/, ".").lstrip - output_file.puts "@@ File \"#{relative_file_path}\"" + fh = File.open(file_path) + mime = Magic.mime_type.of(fh) + output_file.puts "@@ File \"#{relative_file_path}\" (Mime-Type: #{mime.inspect})" output_file.puts "" - output_file.puts(File.read(file_path)) + output_file.puts(fh.gets_to_end) output_file.puts "" + fh.close end end end diff --git a/src/config.cr b/src/config.cr index 4f1ad8d..99d139f 100644 --- a/src/config.cr +++ b/src/config.cr @@ -54,7 +54,7 @@ module CodePreloader end private def validate_arguments - abort("Missing repository path.") if @repository_path.nil? || @repository_path.try(&.empty?) + abort("Missing repository path.") if @repository_path_list.empty? abort("Missing repository path.") if STDERR.puts("Output file path not specified (using STDOUT)") if @output_file_path.nil? || @output_file_path.try(&.empty?) end