Class: Rufio::FilePreview
- Inherits:
-
Object
- Object
- Rufio::FilePreview
- Defined in:
- lib/rufio/file_preview.rb
Constant Summary collapse
- BINARY_THRESHOLD =
treat as binary if 30% or more binary characters
0.3- DEFAULT_MAX_LINES =
50- MAX_LINE_LENGTH =
500- BINARY_SAMPLE_SIZE =
Binary detection constants
512- PRINTABLE_CHAR_THRESHOLD =
32- CONTROL_CHAR_TAB =
9- CONTROL_CHAR_NEWLINE =
10- CONTROL_CHAR_CARRIAGE_RETURN =
13
Instance Method Summary collapse
-
#initialize ⇒ FilePreview
constructor
A new instance of FilePreview.
- #preview_file(file_path, max_lines: DEFAULT_MAX_LINES) ⇒ Object
Constructor Details
#initialize ⇒ FilePreview
Returns a new instance of FilePreview.
16 17 18 |
# File 'lib/rufio/file_preview.rb', line 16 def initialize # future: hold syntax highlight settings etc. end |
Instance Method Details
#preview_file(file_path, max_lines: DEFAULT_MAX_LINES) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rufio/file_preview.rb', line 20 def preview_file(file_path, max_lines: DEFAULT_MAX_LINES) return error_response(ConfigLoader.('file.not_found')) unless File.exist?(file_path) return error_response(ConfigLoader.('file.not_readable')) unless File.readable?(file_path) file_size = File.size(file_path) return empty_response if file_size == 0 begin # binary file detection sample = File.binread(file_path, [file_size, BINARY_SAMPLE_SIZE].min) return binary_response(file_path) if binary_file?(sample) # process as text file lines = read_text_file(file_path, max_lines) file_type = determine_file_type(file_path) { type: file_type[:type], language: file_type[:language], lines: lines[:content], truncated: lines[:truncated], size: file_size, modified: File.mtime(file_path), encoding: lines[:encoding] } rescue => e error_response("#{ConfigLoader.('file.read_error')}: #{e.}") end end |