file_type_detector_sfedu 0.1.1 → 0.2.1

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: 9449915d78b7866c453a5a8cb813839d83f3f96e39beac2f2c15dd5f94df712b
4
+ data.tar.gz: a52208251fa8436adb24a6b15b051cc6fa730de720c05774e83d14beafa36602
5
5
  SHA512:
6
- metadata.gz: 2917a5ee6c5226b02cc0d794cdbe2509f05161cb8f89d07438e6630ecdf1bc227299209748d2717177e9db01aa2741e98dbb0c74e225231c91deeaae46b7713c
7
- data.tar.gz: caeda6a5ffbd575493f1ef284838fed004e4b66ce217eafbe08a34a6d8b69ec120a6e273fb6d4a134873566c2f3f579f13518d817f467fc25939f60ff0a87c13
6
+ metadata.gz: 3c90836cd61fa5af87721f74abdc53687fa5cc35a3bbb9587de1eecbf8be00f922d80d4e9b128c79318248fb3e472554cd4f5257f65986169e8e73feb0aebb2e
7
+ data.tar.gz: 00abb630169031a53cc78834f3f711d4235ae1fb3c3b698e85e9f743b956fda39af44fafd49fb847a23321c21bac4a7fb902a26f667593955e5b0c60881ba92f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
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-04-5
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
20
+
21
+ ## [0.2.1] - 2024-04-6
22
+
23
+ - Added documentation to all functions
24
+ - 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.1"
5
5
  end
@@ -2,94 +2,143 @@
2
2
 
3
3
  require_relative "file_type_detector/version"
4
4
 
5
+ # Provides methods for detecting file types based on their content.
5
6
  module FileTypeDetector
6
- class Error < StandardError; end
7
+ # Determines the file type based on its content.
8
+ #
9
+ # @param [String] file_path The path to the file to be checked.
10
+ # @return [Boolean] Returns true if the file type is recognized, otherwise false.
11
+ def self.check(file_path)
12
+ if error_handling(file_path)
13
+ file_extension = File.extname(file_path).downcase
14
+
15
+ FILE_CHECKS.each do |check_function|
16
+ return check_function.call(file_path) if file_extension == ".#{check_function.name.to_s.split("_")[0]}"
17
+ end
18
+ end
7
19
 
8
- # file_type_detector.rb
20
+ false
21
+ end
9
22
 
10
- class FileTypeDetector
11
- def initialize(file_path)
12
- @file_path = file_path
13
- end
23
+ # Identifies the file type based on its content.
24
+ #
25
+ # @param [String] file_path The path to the file to be identified.
26
+ # @return [String] Returns the identified file type.
27
+ def self.identify(file_path)
28
+ return unless error_handling(file_path)
14
29
 
15
- def detect
16
- detector.detect
17
- end
30
+ FILE_CHECKS.find { |fn| fn.call(file_path)}.name.to_s.split("_")[0][1..]
31
+ end
18
32
 
19
- private
33
+ # =============================================
34
+ # Error handling for file operations.
35
+ #
36
+ # @param [String] file_path The path to the file to be checked.
37
+ # @return [Boolean] Returns true if no errors are encountered, otherwise raises IOError.
38
+ def self.error_handling(file_path)
39
+ raise IOError, "File '#{file_path}' not found" unless File.exist?(file_path)
40
+ raise IOError, "File '#{file_path}' is not a file" unless File.file?(file_path)
41
+ raise IOError, "File '#{file_path}' is not available for reading" unless File.readable?(file_path)
42
+
43
+ true
44
+ end
20
45
 
21
- def detector
22
- detectors.detect { |detector| detector.supported? }
23
- end
46
+ # =============================================
47
+ # Checks if the file has a PDF format.
48
+ #
49
+ # @param [String] file_path The path to the file to be checked.
50
+ # @return [Boolean] Returns true if the file has a PDF format, otherwise false.
51
+ def self.pdf_check(file_path)
52
+ return unless error_handling(file_path)
24
53
 
25
- def detectors
26
- [
27
- PDFDetector.new(@file_path),
28
- PNGDetector.new(@file_path),
29
- # Добавьте сюда новые детекторы для других типов файлов по мере необходимости
30
- ]
31
- end
54
+ File.read(file_path, 5) == "%PDF-"
32
55
  end
33
56
 
34
- # Base Detector class
35
- class Detector
36
- def initialize(file_path)
37
- @file_path = file_path
38
- end
57
+ # Checks if the file has a DOCX format.
58
+ #
59
+ # @param [String] file_path The path to the file to be checked.
60
+ # @return [Boolean] Returns true if the file has a DOCX format, otherwise false.
61
+ def self.docx_check(file_path)
62
+ return unless error_handling(file_path)
39
63
 
40
- def supported?
41
- raise NotImplementedError, "#{self.class} must implement supported?"
42
- end
64
+ File.open(file_path, "rb") { |file| file.read(4) } == "PK\x03\x04"
65
+ end
43
66
 
44
- def detect
45
- raise NotImplementedError, "#{self.class} must implement detect"
46
- end
67
+ # Checks if the file has a PNG format.
68
+ #
69
+ # @param [String] file_path The path to the file to be checked.
70
+ # @return [Boolean] Returns true if the file has a PNG format, otherwise false.
71
+ def self.png_check(file_path)
72
+ return unless error_handling(file_path)
73
+
74
+ first_bytes = File.open(file_path, "rb") { |file| file.read(16) }
75
+ first_bytes.start_with?("\x89PNG\r\n\x1A\n".b)
47
76
  end
48
77
 
49
- # Detector for PDF files
50
- class PDFDetector < Detector
51
- def supported?
52
- File.extname(@file_path).downcase == '.pdf'
53
- end
78
+ # Checks if the file has a GIF format.
79
+ #
80
+ # @param [String] file_path The path to the file to be checked.
81
+ # @return [Boolean] Returns true if the file has a GIF format, otherwise false.
82
+ def self.gif_check(file_path)
83
+ return unless error_handling(file_path)
54
84
 
55
- def detect
56
- # Определяем тип PDF-файла
57
- if File.read(@file_path, 5) == '%PDF-'
58
- 'PDF'
59
- else
60
- 'Unknown' # Если содержимое файла не соответствует формату PDF
61
- end
62
- end
85
+ first_bytes = File.open(file_path, "rb") { |file| file.read(6) }
86
+ %w[GIF87a GIF89a].include?(first_bytes)
63
87
  end
64
88
 
65
- # Detector for PNG files
66
- class PNGDetector < Detector
67
- def supported?
68
- File.extname(@file_path).downcase == '.png'
69
- end
89
+ # Checks if the file has a JPEG format.
90
+ #
91
+ # @param [String] file_path The path to the file to be checked.
92
+ # @return [Boolean] Returns true if the file has a JPEG format, otherwise false.
93
+ def self.jpeg_check(file_path)
94
+ return unless error_handling(file_path)
95
+
96
+ first_bytes = File.open(file_path, "rb") { |file| file.read(2) }
97
+ first_bytes == "\xFF\xD8".b
70
98
 
71
- def detect
72
- # Определяем тип PNG-файла, возможно, с помощью какой-то библиотеки для работы с изображениями
73
- # В данном примере просто возвращаем строку 'PNG'
74
- 'PNG'
75
- end
76
99
  end
77
100
 
78
- end
101
+ # Checks if the file has a JPG format.
102
+ #
103
+ # @param [String] file_path The path to the file to be checked.
104
+ # @return [Boolean] Returns true if the file has a JPG format, otherwise false.
105
+ def self.jpg_check(file_path)
106
+ jpeg_check(file_path)
107
+ end
79
108
 
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
109
+ # Checks if the file has a JSON format.
110
+ #
111
+ # @param [String] file_path The path to the file to be checked.
112
+ # @return [Boolean] Returns true if the file has a JSON format, otherwise false.
113
+ def self.json_check(file_path)
114
+ return unless error_handling(file_path)
115
+
116
+ first_chars = File.open(file_path, "rb") { |file| file.read(2) }
117
+ first_chars.start_with?("{") || first_chars.start_with?("[")
94
118
  end
119
+
120
+ # Checks if the file has a XML format.
121
+ #
122
+ # @param [String] file_path The path to the file to be checked.
123
+ # @return [Boolean] Returns true if the file has a XML format, otherwise false.
124
+ def self.xml_check(file_path)
125
+ return unless error_handling(file_path)
126
+
127
+ File.open(file_path, "rb") { |file| file.read(2) } == "<?"
128
+ end
129
+
130
+ # =============================================
131
+ # Array containing check functions for different file types.
132
+ FILE_CHECKS = [
133
+ method(:pdf_check),
134
+ method(:docx_check),
135
+ method(:png_check),
136
+ method(:gif_check),
137
+ method(:jpeg_check),
138
+ method(:jpg_check),
139
+ method(:json_check),
140
+ method(:xml_check)
141
+ # Add your methods here
142
+ ].freeze
143
+
95
144
  end
data/ruby_gem.gemspec CHANGED
@@ -5,7 +5,7 @@ require_relative "lib/file_type_detector/version"
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "file_type_detector_sfedu"
7
7
  spec.version = FileTypeDetector::VERSION
8
- spec.authors = ["Garik", "watermelon0guy", "synthematikj", "kamchatnaya"]
8
+ spec.authors = ["Garik Mnacakanyan", "Mikhail Babichev", "Maria Putrova", "Elena Kamchatnaya"]
9
9
  spec.email = ["garfild_2003@mail.ru"]
10
10
 
11
11
  spec.summary = "A gem for detecting file types based on their extensions or content."
metadata CHANGED
@@ -1,17 +1,17 @@
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.1
5
5
  platform: ruby
6
6
  authors:
7
- - Garik
8
- - watermelon0guy
9
- - synthematikj
10
- - kamchatnaya
7
+ - Garik Mnacakanyan
8
+ - Mikhail Babichev
9
+ - Maria Putrova
10
+ - Elena Kamchatnaya
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-06 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