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 +4 -4
- data/CHANGELOG.md +21 -2
- data/README.md +56 -43
- data/lib/file_type_detector/version.rb +1 -1
- data/lib/file_type_detector.rb +118 -69
- data/ruby_gem.gemspec +1 -1
- metadata +6 -7
- data/sig/ruby_gem.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9449915d78b7866c453a5a8cb813839d83f3f96e39beac2f2c15dd5f94df712b
|
4
|
+
data.tar.gz: a52208251fa8436adb24a6b15b051cc6fa730de720c05774e83d14beafa36602
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c90836cd61fa5af87721f74abdc53687fa5cc35a3bbb9587de1eecbf8be00f922d80d4e9b128c79318248fb3e472554cd4f5257f65986169e8e73feb0aebb2e
|
7
|
+
data.tar.gz: 00abb630169031a53cc78834f3f711d4235ae1fb3c3b698e85e9f743b956fda39af44fafd49fb847a23321c21bac4a7fb902a26f667593955e5b0c60881ba92f
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,24 @@
|
|
1
|
-
## [
|
1
|
+
## [Released]
|
2
2
|
|
3
|
-
|
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
|
-
#
|
2
|
-
|
3
|
-
**
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this gem to your Gemfile and run `bundle install`:
|
8
|
-
|
9
|
-
```ruby
|
10
|
-
gem
|
11
|
-
```
|
12
|
-
|
13
|
-
Alternatively, install it directly using the gem command:
|
14
|
-
|
15
|
-
```ruby
|
16
|
-
gem install
|
17
|
-
```
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
To use the gem, you need to
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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).
|
data/lib/file_type_detector.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
20
|
+
false
|
21
|
+
end
|
9
22
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
17
|
-
end
|
30
|
+
FILE_CHECKS.find { |fn| fn.call(file_path)}.name.to_s.split("_")[0][1..]
|
31
|
+
end
|
18
32
|
|
19
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
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
|
-
#
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
|
42
|
-
end
|
64
|
+
File.open(file_path, "rb") { |file| file.read(4) } == "PK\x03\x04"
|
65
|
+
end
|
43
66
|
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
#
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
56
|
-
|
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
|
-
#
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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", "
|
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.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
- Garik
|
8
|
-
-
|
9
|
-
-
|
10
|
-
-
|
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-
|
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