data_uri_revived 0.1.1.beta1
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/README.rdoc +92 -0
- data/Rakefile +9 -0
- data/lib/data_uri/uri.rb +76 -0
- data/lib/data_uri.rb +3 -0
- data/lib/data_uri_revived.rb +3 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3387185bb9f128eeeded93f7e9ffd3cf2830618cfcd1cba04b8ee5856ba0a400
|
4
|
+
data.tar.gz: 9d54ec7a5bfcf39a0083d8e3243d605fc065c9decbddf7a5b971144aeb822709
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d263b3c9b10a9b47090e9ca27b3732f049fe2e6d7af7431ab9a2abb8a3684e2b7565ce1f4c0b480d706da8e1041380877788e8fd5861a84f1ef1b3feb7dc8547
|
7
|
+
data.tar.gz: 22c227426e43fd6d81b796673e799defca78ae61a3b080bea25f739da65b5692a7daec7f2667ccc2fc698d23a8bfb1f08734c5d0232cf05a2848a1e204e1fc4c
|
data/README.rdoc
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
= Data URI Revived {<img src="https://img.shields.io/github/actions/workflow/status/data-uri-ruby/data_uri/tests.yaml?branch=master&style=flat-square" alt="Build Status" />}[https://github.com/data-uri-ruby/data_uri/actions/workflows/tests.yaml]
|
2
|
+
|
3
|
+
Fork of unmaintained original `data_uri` (No commit for 11 years)
|
4
|
+
|
5
|
+
== Introduction
|
6
|
+
|
7
|
+
Data URIs allow resources to be embedded inside a URI. The URI::Data class
|
8
|
+
provides support for parsing these URIs using the normal URI.parse method.
|
9
|
+
|
10
|
+
I wrote it to support embedding binary data inside JSON messages in a
|
11
|
+
relatively reasonable way. If you find some other use for it, please drop me
|
12
|
+
a line.
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
# Replacement of `data_uri`, do not mix with it
|
17
|
+
gem 'data_uri_revived'
|
18
|
+
|
19
|
+
== Usage
|
20
|
+
|
21
|
+
require 'data_uri_revived'
|
22
|
+
# Or the old one in case you have old require statements
|
23
|
+
require 'data_uri'
|
24
|
+
|
25
|
+
uri = URI::Data.new('data:image/gif;base64,...')
|
26
|
+
uri.content_type # image/gif
|
27
|
+
uri.data # Base64 decoded data
|
28
|
+
|
29
|
+
# Same as `URI::Data.new`
|
30
|
+
uri = URI.parse('data:image/gif;base64,...')
|
31
|
+
|
32
|
+
Data URIs CANNOT play nicely with open-uri (was mentioned to be working in original `data_uri` gem
|
33
|
+
|
34
|
+
== Features & Limitations
|
35
|
+
|
36
|
+
URI.parse knows about URI::Data, but unfortunately, its regexp for splitting
|
37
|
+
URIs into components maxes out at 92 characters for an opaque URI, which is
|
38
|
+
far too small to be useful for data URIs.
|
39
|
+
|
40
|
+
It accepts URIs with charset MIME parameters:
|
41
|
+
|
42
|
+
data:text/html;charset=utf-8,...
|
43
|
+
|
44
|
+
and if the String object has a force_encoding method, as in ruby-1.9.x, it
|
45
|
+
will call it with the value of the charset parameter on the decoded data.
|
46
|
+
Other MIME parameters are parsed and stored, but are not exposed.
|
47
|
+
|
48
|
+
The base64 pseudo-parameter must appear immediately prior to the data, as per
|
49
|
+
the RFC. Google Chrome apparently misbehaves in this regard, so if this
|
50
|
+
causes grief to anyone, yell and I'll relax the constraint. If base64 coding
|
51
|
+
is not specified, the data are URI decoded.
|
52
|
+
|
53
|
+
Some non-authoritative documentation about data URIs state that whitespace
|
54
|
+
is allowed and ignored within them. The RFC seems to contradict this
|
55
|
+
interpretation, but support seems to be widespread in the browser world.
|
56
|
+
URI::Data cannot parse URIs containing whitespace. If
|
57
|
+
this causes grief to anyone, yell and I'll either add a class method to
|
58
|
+
URI::Data for explicit relaxed parsing, and/or figure out how to convince
|
59
|
+
URI::Generic to be more open minded about the kinds of URIs it allows.
|
60
|
+
|
61
|
+
Even when using the open-uri support file, Kernel.open will not accept string
|
62
|
+
data URIs, only URI::Data objects. Open-uri is opinionated about the
|
63
|
+
formats of the URIs it will try to open, and it's ambiguous to me at least if
|
64
|
+
data URIs can necessarily be distinguished from filesystem paths on all
|
65
|
+
platforms. Again, if this causes undue pain and suffering, we can commit
|
66
|
+
minor violence to open-uri to convince it to see reason.
|
67
|
+
|
68
|
+
== License:
|
69
|
+
|
70
|
+
(The MIT License)
|
71
|
+
|
72
|
+
Copyright (c) 2010 Donald Ball <donald.ball@gmail.com>
|
73
|
+
Copyright (c) 2025 PikachuEXE <git@pikachuexe.net>
|
74
|
+
|
75
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
76
|
+
a copy of this software and associated documentation files (the
|
77
|
+
'Software'), to deal in the Software without restriction, including
|
78
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
79
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
80
|
+
permit persons to whom the Software is furnished to do so, subject to
|
81
|
+
the following conditions:
|
82
|
+
|
83
|
+
The above copyright notice and this permission notice shall be
|
84
|
+
included in all copies or substantial portions of the Software.
|
85
|
+
|
86
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
87
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
88
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
89
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
90
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
91
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
92
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/data_uri/uri.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'base64'
|
5
|
+
|
6
|
+
module URI
|
7
|
+
|
8
|
+
class Data < Generic
|
9
|
+
|
10
|
+
COMPONENT = [:scheme, :opaque].freeze
|
11
|
+
MIME_TYPE_RE = %r{^([-\w.+]+/[-\w.+]*)}.freeze
|
12
|
+
MIME_PARAM_RE = /^;([-\w.+]+)=([^;,]+)/.freeze
|
13
|
+
|
14
|
+
attr_reader :content_type, :data
|
15
|
+
|
16
|
+
def initialize(*args)
|
17
|
+
if args.length == 1
|
18
|
+
uri = args.first.to_s
|
19
|
+
unless uri.match(/^data:/)
|
20
|
+
raise URI::InvalidURIError.new('Invalid Data URI: ' + args.first.inspect)
|
21
|
+
end
|
22
|
+
@scheme = 'data'
|
23
|
+
@opaque = uri[5 .. -1]
|
24
|
+
else
|
25
|
+
super(*args)
|
26
|
+
end
|
27
|
+
@data = @opaque
|
28
|
+
if (md = MIME_TYPE_RE.match(@data))
|
29
|
+
@content_type = md[1]
|
30
|
+
@data = @data[@content_type.length .. -1]
|
31
|
+
end
|
32
|
+
@content_type ||= 'text/plain'
|
33
|
+
@mime_params = {}
|
34
|
+
while (md = MIME_PARAM_RE.match(@data))
|
35
|
+
@mime_params[md[1]] = md[2]
|
36
|
+
@data = @data[md[0].length .. -1]
|
37
|
+
end
|
38
|
+
if (base64 = /^;base64/.match(@data))
|
39
|
+
@data = @data[7 .. -1]
|
40
|
+
end
|
41
|
+
unless /^,/.match(@data)
|
42
|
+
raise URI::InvalidURIError.new('Invalid data URI')
|
43
|
+
end
|
44
|
+
@data = @data[1 .. -1]
|
45
|
+
@data = base64 ? Base64.decode64(@data) : URI.decode_www_form_component(@data)
|
46
|
+
if @data.respond_to?(:force_encoding) && (charset = @mime_params['charset'])
|
47
|
+
@data.force_encoding(charset)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.build(arg)
|
52
|
+
data = nil
|
53
|
+
content_type = nil
|
54
|
+
case arg
|
55
|
+
when IO
|
56
|
+
data = arg
|
57
|
+
when Hash
|
58
|
+
data = arg[:data]
|
59
|
+
content_type = arg[:content_type]
|
60
|
+
end
|
61
|
+
raise 'Invalid build argument: ' + arg.inspect unless data
|
62
|
+
if !content_type && data.respond_to?(:content_type)
|
63
|
+
content_type = data.content_type
|
64
|
+
end
|
65
|
+
new('data', nil, nil, nil, nil, nil, "#{content_type};base64,#{Base64.encode64(data.read).chop}", nil, nil)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
unless methods(false).include?(:register_scheme)
|
70
|
+
def self.register_scheme(scheme, klass)
|
71
|
+
@@schemes[scheme] = klass
|
72
|
+
end
|
73
|
+
end
|
74
|
+
register_scheme('DATA', Data)
|
75
|
+
|
76
|
+
end
|
data/lib/data_uri.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_uri_revived
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1.beta1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Donald Ball
|
8
|
+
- PikachuEXE
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-01-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: base64
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: URI class for parsing data URIs
|
56
|
+
email:
|
57
|
+
- donald.ball@gmail.com
|
58
|
+
- git@pikachuexe.net
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files:
|
62
|
+
- README.rdoc
|
63
|
+
files:
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- lib/data_uri.rb
|
67
|
+
- lib/data_uri/uri.rb
|
68
|
+
- lib/data_uri_revived.rb
|
69
|
+
homepage: https://github.com/data-uri-ruby/data_uri
|
70
|
+
licenses: []
|
71
|
+
metadata: {}
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '3.0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubygems_version: 3.6.2
|
87
|
+
specification_version: 4
|
88
|
+
summary: A URI class for parsing data URIs as per RFC2397
|
89
|
+
test_files: []
|