file_type_detector_sfedu 0.1.1 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd4c402e08497af69516d8d8c5626a9c4fa40e36fc357348a45395b4fdc00c0f
4
- data.tar.gz: f0cb62187c310f14573ce1964b4459c68e27a75164c633f0485c5614e9566221
3
+ metadata.gz: 5a5b6d66dd409a03792b309a5a50070c32ec1cb8a3578d0d9f791511425013d5
4
+ data.tar.gz: 3a714ef0dd6ef17e4e05c562c4a3f4b10bceb11d8c8f297469c6c7063d7d3ebb
5
5
  SHA512:
6
- metadata.gz: 2917a5ee6c5226b02cc0d794cdbe2509f05161cb8f89d07438e6630ecdf1bc227299209748d2717177e9db01aa2741e98dbb0c74e225231c91deeaae46b7713c
7
- data.tar.gz: caeda6a5ffbd575493f1ef284838fed004e4b66ce217eafbe08a34a6d8b69ec120a6e273fb6d4a134873566c2f3f579f13518d817f467fc25939f60ff0a87c13
6
+ metadata.gz: 99e503196037d1ca0fb09ae51011f4f510ad81ff03045b356ee1e2df0eb7f4fc81d3aec90d20b9f7ee9b7a460ebf2aa65d71f59ffe8393d51d7bb884a0b1a49c
7
+ data.tar.gz: b4b0bd07b6ed8d92204dff6ee187d5375bdcbe44294b94b0a9d84526ed905ca1484c003d93c09ee6cc6404a104e94d02c0b575fb22ecd4d2b5604ffca127ebea
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
2
 
3
- ## [0.1.0] - 2024-03-18
3
+
4
+ ## [0.1.1] - 2024-03-31
4
5
 
5
6
  - Initial release
7
+
8
+
9
+ ## [0.2.0] - 2024-03-2
10
+
11
+ - Tests have been updated to test functions more correctly
12
+ - The existing code has been significantly improved:
13
+ - The functions have been optimized
14
+ - Removed unnecessary code
15
+ - Added error handling
16
+ - Improved code style
17
+ - Added functions for checking new types of extensions: png, jpeg/jpg, gif, json, xml
18
+ - Updated the readme file
19
+ - Other minor improvements
data/README.md CHANGED
@@ -1,43 +1,56 @@
1
- # file_type_detector
2
-
3
- **file_type_detector** is a Ruby gem that allows you to determine the type of files based on their extension or content.
4
-
5
- ## Installation
6
-
7
- Add this gem to your Gemfile and run `bundle install`:
8
-
9
- ```ruby
10
- gem 'file_type_detector'
11
- ```
12
-
13
- Alternatively, install it directly using the gem command:
14
-
15
- ```ruby
16
- gem install file_type_detector
17
- ```
18
-
19
- ## Usage
20
-
21
- To use the gem, you need to create an instance of FileTypeDetector::FileTypeDetector, specifying the path to the file as the constructor argument. Then call the detect method to determine the file type.
22
-
23
- ```ruby
24
- require 'file_type_detector'
25
-
26
- detector = FileTypeDetector::FileTypeDetector.new('path/to/your/file.pdf')
27
- puts detector.detect
28
- ```
29
- Please replace `"path/to/your/file.pdf"` with the path to your file that you want to check.
30
-
31
- ## Supported File Types
32
- The gem provides support for detecting the following file types:
33
-
34
- - PDF
35
- - PNG
36
-
37
- ## Contributing
38
-
39
- You can contribute to the development of the gem by creating new detectors for other file types and improving existing code. Report bugs and submit feature requests through the [GitHub repository](https://github.com/synthematik/file_type_detector_gem).
40
-
41
- ## License
42
-
43
- This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
1
+ # file_type_detector_sfedu
2
+
3
+ **file_type_detector_sfedu** is a Ruby gem that allows you to determine the type of files based on their extension or content.
4
+
5
+ ## Installation
6
+
7
+ Add this gem to your Gemfile and run `bundle install`:
8
+
9
+ ```ruby
10
+ gem "file_type_detector_sfedu"
11
+ ```
12
+
13
+ Alternatively, install it directly using the gem command:
14
+
15
+ ```ruby
16
+ gem install file_type_detector_sfedu
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ To use the gem, you need to call the `check` method with a parameter in the form of a file path:
22
+ ```ruby
23
+ require "file_type_detector_sfedu"
24
+
25
+ puts FileTypeDetector.check("path/to/your/file.pdf") # if it's truly pdf, value will be true, unless - false
26
+ ```
27
+ `check` method will automatically detect the file type by its extension and tell if its contents match the extension.
28
+
29
+ You can also use a specific detector:
30
+ ```ruby
31
+ puts FileTypeDetector.pdf_check("path/to/your/real_pdf.pdf") # true
32
+ puts FileTypeDetector.pdf_check("path/to/your/fake_pdf.pdf") # false
33
+
34
+ puts FileTypeDetector.docx_check("path/to/your/real_docx.docx") # true
35
+ puts FileTypeDetector.docx_check("path/to/your/fake_docx.docx") # false
36
+ ```
37
+
38
+ ## Supported File Types
39
+ The gem provides support for detecting the following file types:
40
+
41
+ - PDF
42
+ - DOCX
43
+ - PNG
44
+ - GIF
45
+ - JPEG/JPG
46
+ - JSON
47
+ - XML
48
+
49
+ ## Contributing and links
50
+
51
+ You can contribute to the development of the gem by creating new detectors for other file types and improving existing code. Report bugs and submit feature requests through the [GitHub repository](https://github.com/synthematik/file_type_detector_gem).
52
+
53
+ You can visit our gem's page here - [Rubygems.org](https://rubygems.org/gems/file_type_detector_sfedu)
54
+ ## License
55
+
56
+ This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FileTypeDetector
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -3,93 +3,99 @@
3
3
  require_relative "file_type_detector/version"
4
4
 
5
5
  module FileTypeDetector
6
- class Error < StandardError; end
6
+ # @param [String] file_path
7
+ # @return [Boolean]
8
+ def self.check(file_path)
9
+ if error_handling(file_path)
10
+ file_extension = File.extname(file_path).downcase
11
+
12
+ FILE_CHECKS.each do |check_function|
13
+ return check_function.call(file_path) if file_extension == ".#{check_function.name.to_s.split("_")[0]}"
14
+ end
15
+ end
7
16
 
8
- # file_type_detector.rb
17
+ false
18
+ end
9
19
 
10
- class FileTypeDetector
11
- def initialize(file_path)
12
- @file_path = file_path
13
- end
20
+ # =============================================
21
+ # Error handling
22
+ def self.error_handling(file_path)
23
+ raise IOError, "File '#{file_path}' not found" unless File.exist?(file_path)
24
+ raise IOError, "File '#{file_path}' is not a file" unless File.file?(file_path)
25
+ raise IOError, "File '#{file_path}' is not available for reading" unless File.readable?(file_path)
14
26
 
15
- def detect
16
- detector.detect
17
- end
27
+ true
28
+ end
18
29
 
19
- private
30
+ # =============================================
31
+ # Write your methods here
32
+ def self.pdf_check(file_path)
33
+ return unless error_handling(file_path)
20
34
 
21
- def detector
22
- detectors.detect { |detector| detector.supported? }
23
- end
35
+ File.read(file_path, 5) == "%PDF-"
24
36
 
25
- def detectors
26
- [
27
- PDFDetector.new(@file_path),
28
- PNGDetector.new(@file_path),
29
- # Добавьте сюда новые детекторы для других типов файлов по мере необходимости
30
- ]
31
- end
32
37
  end
33
38
 
34
- # Base Detector class
35
- class Detector
36
- def initialize(file_path)
37
- @file_path = file_path
38
- end
39
+ def self.docx_check(file_path)
40
+ return unless error_handling(file_path)
39
41
 
40
- def supported?
41
- raise NotImplementedError, "#{self.class} must implement supported?"
42
- end
42
+ File.open(file_path, "rb") { |file| file.read(4) } == "PK\x03\x04"
43
43
 
44
- def detect
45
- raise NotImplementedError, "#{self.class} must implement detect"
46
- end
47
44
  end
48
45
 
49
- # Detector for PDF files
50
- class PDFDetector < Detector
51
- def supported?
52
- File.extname(@file_path).downcase == '.pdf'
53
- end
46
+ def self.png_check(file_path)
47
+ return unless error_handling(file_path)
48
+
49
+ first_bytes = File.open(file_path, "rb") { |file| file.read(16) }
50
+ first_bytes.start_with?("\x89PNG\r\n\x1A\n".b)
54
51
 
55
- def detect
56
- # Определяем тип PDF-файла
57
- if File.read(@file_path, 5) == '%PDF-'
58
- 'PDF'
59
- else
60
- 'Unknown' # Если содержимое файла не соответствует формату PDF
61
- end
62
- end
63
52
  end
64
53
 
65
- # Detector for PNG files
66
- class PNGDetector < Detector
67
- def supported?
68
- File.extname(@file_path).downcase == '.png'
69
- end
54
+ def self.gif_check(file_path)
55
+ return unless error_handling(file_path)
56
+
57
+ first_bytes = File.open(file_path, "rb") { |file| file.read(6) }
58
+ %w[GIF87a GIF89a].include?(first_bytes)
70
59
 
71
- def detect
72
- # Определяем тип PNG-файла, возможно, с помощью какой-то библиотеки для работы с изображениями
73
- # В данном примере просто возвращаем строку 'PNG'
74
- 'PNG'
75
- end
76
60
  end
77
61
 
78
- end
62
+ def self.jpeg_check(file_path)
63
+ return unless error_handling(file_path)
64
+
65
+ first_bytes = File.open(file_path, "rb") { |file| file.read(2) }
66
+ first_bytes == "\xFF\xD8".b
67
+
68
+ end
69
+
70
+ def self.jpg_check(file_path)
71
+ jpeg_check(file_path)
72
+ end
73
+
74
+ def self.json_check(file_path)
75
+ return unless error_handling(file_path)
76
+
77
+ first_chars = File.open(file_path, "rb") { |file| file.read(2) }
78
+ first_chars.start_with?("{") || first_chars.start_with?("[")
79
+ end
80
+
81
+ def self.xml_check(file_path)
82
+ return unless error_handling(file_path)
83
+
84
+ (File.open(file_path, "rb") { |file| file.read(2) }) == "<?"
79
85
 
80
- def detect_file_type(file_path)
81
- File.open(file_path, 'rb') do |file|
82
- header = file.read(4)
83
- if header.start_with?('%PDF')
84
- return 'PDF'
85
- elsif header.start_with?('GIF8')
86
- return 'GIF'
87
- elsif header.start_with?("\x89PNG\r\n\x1A\n")
88
- return 'PNG'
89
- elsif header.start_with?("\xFF\xD8\xFF")
90
- return 'JPEG'
91
- else
92
- return 'Неизвестный тип файла'
93
- end
94
86
  end
87
+
88
+ # =============================================
89
+ FILE_CHECKS = [
90
+ method(:pdf_check),
91
+ method(:docx_check),
92
+ method(:png_check),
93
+ method(:gif_check),
94
+ method(:jpeg_check),
95
+ method(:jpg_check),
96
+ method(:json_check),
97
+ method(:xml_check),
98
+ # Add your methods here
99
+ ].freeze
100
+
95
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_type_detector_sfedu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garik
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2024-03-30 00:00:00.000000000 Z
14
+ date: 2024-04-05 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: FileTypeDetector is a Ruby gem that provides functionality for determining
17
17
  the type of a file based on its extension or content.
@@ -30,7 +30,6 @@ files:
30
30
  - lib/file_type_detector.rb
31
31
  - lib/file_type_detector/version.rb
32
32
  - ruby_gem.gemspec
33
- - sig/ruby_gem.rbs
34
33
  homepage: https://github.com/synthematik/file_type_detector_gem
35
34
  licenses:
36
35
  - MIT
data/sig/ruby_gem.rbs DELETED
@@ -1,4 +0,0 @@
1
- module RubyGem
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end