Class: Telegem::Plugins::FileExtract
- Inherits:
-
Object
- Object
- Telegem::Plugins::FileExtract
- Defined in:
- lib/plugins/file_extract.rb
Instance Method Summary collapse
- #cleanup ⇒ Object
- #extract ⇒ Object
- #extract_html ⇒ Object
- #extract_text ⇒ Object
-
#initialize(bot, file_id, **options) ⇒ FileExtract
constructor
A new instance of FileExtract.
Constructor Details
#initialize(bot, file_id, **options) ⇒ FileExtract
Returns a new instance of FileExtract.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/plugins/file_extract.rb', line 9 def initialize(bot, file_id, **) @bot = bot @file_id = file_id @options = { auto_delete: true, max_file_size: 50 * 1024 * 1024, timeout: 60 }.merge() @temp_file = nil @detected_type = nil end |
Instance Method Details
#cleanup ⇒ Object
176 177 178 179 |
# File 'lib/plugins/file_extract.rb', line 176 def cleanup @temp_file.unlink if @temp_file @temp_file = nil end |
#extract ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/plugins/file_extract.rb', line 20 def extract download_if_needed detect_type case @detected_type when :pdf then extract_pdf when :json then extract_json when :html then extract_html when :txt, :md, :csv then extract_text else { success: false, error: "Unsupported file type: #{@detected_type}" } end end |
#extract_html ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/plugins/file_extract.rb', line 122 def extract_html begin content = File.read(@temp_file.path) if content.strip.empty? return { success: false, error: "HTML file is empty" } end { success: true, type: :html, content: content, metadata: { size: content.size, title: content[/<title>(.*?)<\/title>/i, 1] } } rescue => e { success: false, error: "Failed to extract HTML: #{e.}" } ensure cleanup if @options[:auto_delete] end end |
#extract_text ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/plugins/file_extract.rb', line 149 def extract_text begin content = File.read(@temp_file.path) if content.strip.empty? return { success: false, error: "Text file is empty" } end { success: true, type: :text, content: content, metadata: { size: content.size, line_count: content.lines.count } } rescue => e { success: false, error: "Failed to extract text: #{e.}" } ensure cleanup if @options[:auto_delete] end end |