file_to_data_uri 0.0.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 +7 -0
- data/CHANGELOG.md +14 -0
- data/LICENSE.md +21 -0
- data/README.md +52 -0
- data/file_to_data_uri.gemspec +33 -0
- data/lib/file_to_data_uri/version.rb +5 -0
- data/lib/file_to_data_uri.rb +72 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b9ff8efa62708efb190149d2a5e62112b2ec32702beeece7360e5b56dde892e1
|
4
|
+
data.tar.gz: 40bb5ee786facf6af6242114e31922c3eda602d6e2faba9ec7b603b1913dc67b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27d6273e888342c32b4f1101ae8e6c07ae11f3ded13d4b8d6c618a168e69a668a64d7db522ffab118d01a06b2ffbbbc2dbec1edef822646ab6e7556281ab4963
|
7
|
+
data.tar.gz: 2bc4c535e9fac1dd1eef05021363b70586689cb19b89594a85e654773e9cb90f967058d6dd3934ac3e8e9a6b4d737501b07cfc82542cdb15b39f9e50a0587bc2
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [0.0.1] - 2025-04-29
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- Initial implementation of DataURI class
|
12
|
+
- Support for converting file paths and file-like objects to data URIs
|
13
|
+
- Automatic MIME type detection from file extensions
|
14
|
+
- Base64 encoding of file contents
|
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Raghu Betina and FIRSTDRAFT LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# FileToDataURI
|
2
|
+
|
3
|
+
A Ruby gem that converts files or file-like objects to data URI strings suitable for use in HTML elements.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
### Gemfile way (preferred)
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "file_to_data_uri", "< 1.0.0"
|
13
|
+
```
|
14
|
+
|
15
|
+
And then, at a command prompt:
|
16
|
+
|
17
|
+
```
|
18
|
+
bundle install
|
19
|
+
```
|
20
|
+
|
21
|
+
### Direct way
|
22
|
+
|
23
|
+
Or, install it directly with:
|
24
|
+
|
25
|
+
```
|
26
|
+
gem install file_to_data_uri
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# With a file path
|
33
|
+
data_uri = DataURI.new("path/to/image.jpg")
|
34
|
+
data_uri.to_s # => "data:image/jpeg;base64,..."
|
35
|
+
|
36
|
+
# With a file-like object (anything that responds to `read`)
|
37
|
+
file = File.open("image.png")
|
38
|
+
data_uri = DataURI.new(file)
|
39
|
+
data_uri.to_s # => "data:image/png;base64,..."
|
40
|
+
|
41
|
+
# Direct use in HTML
|
42
|
+
# <img src="<%= DataURI.new('logo.png').to_s %>">
|
43
|
+
```
|
44
|
+
|
45
|
+
### Supported Input Types
|
46
|
+
|
47
|
+
- **File paths**: Pass a string with a path to a local image file.
|
48
|
+
- **File-like objects**: Pass an object that responds to `read` (like `File.open("image.jpg")` or a Rails uploaded file).
|
49
|
+
|
50
|
+
## License
|
51
|
+
|
52
|
+
This gem is available as open source under the terms of the [MIT License](LICENSE).
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/file_to_data_uri/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "file_to_data_uri"
|
7
|
+
spec.version = DataURI::VERSION
|
8
|
+
spec.authors = ["Raghu Betina"]
|
9
|
+
spec.email = ["raghu@firstdraft.com"]
|
10
|
+
spec.homepage = "https://github.com/firstdraft/file_to_data_uri"
|
11
|
+
spec.summary = "Convert files to data URI strings for use in HTML elements"
|
12
|
+
spec.description = "A Ruby gem that converts files or file-like objects to data URI strings suitable for use in HTML elements like img tags."
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata = {
|
16
|
+
"bug_tracker_uri" => "https://github.com/firstdraft/file_to_data_uri/issues",
|
17
|
+
"changelog_uri" => "https://github.com/firstdraft/file_to_data_uri/blob/main/CHANGELOG.md",
|
18
|
+
"homepage_uri" => "https://github.com/firstdraft/file_to_data_uri",
|
19
|
+
"funding_uri" => "https://github.com/sponsors/firstdraft",
|
20
|
+
"label" => "DataURI",
|
21
|
+
"rubygems_mfa_required" => "true",
|
22
|
+
"source_code_uri" => "https://github.com/firstdraft/file_to_data_uri"
|
23
|
+
}
|
24
|
+
|
25
|
+
spec.required_ruby_version = ">= 2.0.0"
|
26
|
+
spec.add_runtime_dependency "mime-types", "~> 3.0"
|
27
|
+
spec.add_runtime_dependency "base64", "~> 0.1", ">= 0.1.0"
|
28
|
+
|
29
|
+
# All development dependencies are specified in the Gemfile
|
30
|
+
|
31
|
+
spec.extra_rdoc_files = Dir["README*", "LICENSE*", "CHANGELOG*"]
|
32
|
+
spec.files = Dir["*.gemspec", "lib/**/*"]
|
33
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "base64"
|
4
|
+
require "mime/types"
|
5
|
+
require_relative "file_to_data_uri/version"
|
6
|
+
|
7
|
+
# DataURI class for generating data URIs from files or file-like objects
|
8
|
+
class DataURI
|
9
|
+
# Initialize with a file path or file-like object
|
10
|
+
#
|
11
|
+
# @param source [String, #read] File path or file-like object
|
12
|
+
def initialize(source)
|
13
|
+
@source = source
|
14
|
+
end
|
15
|
+
|
16
|
+
# Convert to data URI string
|
17
|
+
#
|
18
|
+
# @return [String] A data URI representation of the file
|
19
|
+
def to_s
|
20
|
+
content = read_content
|
21
|
+
mime_type = determine_mime_type
|
22
|
+
encoded_content = Base64.strict_encode64(content)
|
23
|
+
|
24
|
+
"data:#{mime_type};base64,#{encoded_content}"
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# Read content from file path or file-like object
|
30
|
+
#
|
31
|
+
# @return [String] Binary content of the file
|
32
|
+
def read_content
|
33
|
+
if @source.respond_to?(:read)
|
34
|
+
# File-like object
|
35
|
+
@source.rewind if @source.respond_to?(:rewind)
|
36
|
+
content = @source.read
|
37
|
+
@source.rewind if @source.respond_to?(:rewind)
|
38
|
+
content
|
39
|
+
else
|
40
|
+
# File path
|
41
|
+
File.binread(@source.to_s)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Determine the MIME type of the file
|
46
|
+
#
|
47
|
+
# @return [String] MIME type
|
48
|
+
def determine_mime_type
|
49
|
+
if @source.respond_to?(:content_type) && @source.content_type
|
50
|
+
# For Rails uploaded files or similar objects that expose content_type
|
51
|
+
@source.content_type
|
52
|
+
elsif @source.respond_to?(:path) && @source.path
|
53
|
+
# If it's a file object with a path
|
54
|
+
mime_from_path(@source.path)
|
55
|
+
elsif @source.is_a?(String)
|
56
|
+
# If it's a file path as string
|
57
|
+
mime_from_path(@source)
|
58
|
+
else
|
59
|
+
# Default to octet-stream if we can't determine
|
60
|
+
"application/octet-stream"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Get MIME type from file path
|
65
|
+
#
|
66
|
+
# @param path [String] Path to file
|
67
|
+
# @return [String] MIME type
|
68
|
+
def mime_from_path(path)
|
69
|
+
mime = MIME::Types.type_for(path.to_s).first
|
70
|
+
mime ? mime.to_s : "application/octet-stream"
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_to_data_uri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Raghu Betina
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-04-29 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: mime-types
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '3.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '3.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: base64
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0.1'
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 0.1.0
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0.1'
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.1.0
|
46
|
+
description: A Ruby gem that converts files or file-like objects to data URI strings
|
47
|
+
suitable for use in HTML elements like img tags.
|
48
|
+
email:
|
49
|
+
- raghu@firstdraft.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files:
|
53
|
+
- README.md
|
54
|
+
- LICENSE.md
|
55
|
+
- CHANGELOG.md
|
56
|
+
files:
|
57
|
+
- CHANGELOG.md
|
58
|
+
- LICENSE.md
|
59
|
+
- README.md
|
60
|
+
- file_to_data_uri.gemspec
|
61
|
+
- lib/file_to_data_uri.rb
|
62
|
+
- lib/file_to_data_uri/version.rb
|
63
|
+
homepage: https://github.com/firstdraft/file_to_data_uri
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata:
|
67
|
+
bug_tracker_uri: https://github.com/firstdraft/file_to_data_uri/issues
|
68
|
+
changelog_uri: https://github.com/firstdraft/file_to_data_uri/blob/main/CHANGELOG.md
|
69
|
+
homepage_uri: https://github.com/firstdraft/file_to_data_uri
|
70
|
+
funding_uri: https://github.com/sponsors/firstdraft
|
71
|
+
label: DataURI
|
72
|
+
rubygems_mfa_required: 'true'
|
73
|
+
source_code_uri: https://github.com/firstdraft/file_to_data_uri
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 2.0.0
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubygems_version: 3.6.2
|
89
|
+
specification_version: 4
|
90
|
+
summary: Convert files to data URI strings for use in HTML elements
|
91
|
+
test_files: []
|