Comments (8)
Why stop at base64
? This can be done using only POSIX utilities!
# Create a data URL from a file
dataurl() {
# https://github.com/mathiasbynens/dotfiles/commit/5d76fc286f
# POSIX does not define `-b`/`--brief` or `--mime-type`
mimeType="$(LC_ALL='C' file -b --mime-type -- "${1-}")"
if printf -- '%s\n' "${mimeType-}" | grep -q -e '^text/'; then
mimeType="${mimeType-}"';charset=utf-8'
fi
# `uuencode` – part of POSIX since the 1900s – instead of `openssl` or `base64`
uuencode -m -- "${1-}" /dev/stdout |
# remove the first line, then the last line, then remove all newlines
sed -e '1d' -e '$d' - |
sed -e ':a' -e 'N' -e '$! b a' -e 's/\n//g' - |
awk -v mimeType="${mimeType-}" -- '{printf "data:%s;base64,%s\n", mimeType, $0}' -
# `unset` instead of non-standard `local`, but send its errors to `/dev/null` just in case
unset -v -- mimeType 2>/dev/null
}
from dotfiles.
IIRC macOS didn't have base64
preinstalled when this was written.
from dotfiles.
IIRC macOS didn't have
base64
preinstalled when this was written.
Thanks for explaining. I know little about macOS history
Why stop at base64? This can be done using only POSIX utilities! [...]
I like how you used --
to escape args, just-in-case a filename starts with "-". We should probably merge that change (--
) into main
BTW, I tried using uuencode
in my system, and it wasn't even installed, lol
from dotfiles.
BTW, speaking of special chars, I just noticed that both openssl base64
and base64
, are not URL-safe. This completely defeats the purpose of dataurl
.
@mathiasbynens Should I rename this issue to "dataurl
suggestions", or should I open a new issue about this?
I'm willing to open a PR for this, but I don't know which commands are more appropriate. I'll do some research
from dotfiles.
@Rudxain What do you mean with URL-safe?
from dotfiles.
What do you mean with URL-safe?
I haven't tested the output of dataurl
on a browser (neither address bar, nor HTML files), but I assume it can sometimes (depends on input data) cause issues because of the "/" chars
AFAIK, most base64
implementations on Unix-like OSes aren't concerned with being URL-safe, so most systems are susceptible to this problem
from dotfiles.
According to this SO answer, basenc --base64url
is the best alternative (if we aren't concerned with retro-compatibility)
There were other answers that don't require basenc
, but instead require cut
, tr
, awk
, sed
, etc... (some have more dependencies than others)
from dotfiles.
I just read Wikipedia: data
URI scheme allows standard B64 (not "URL-safe"). I'm so confused lol. I'm sorry for the false alarm
from dotfiles.
Related Issues (20)
- "Could not write domain com.apple.universalaccess; exiting" error in macOS Monterey HOT 6
- Calls to systemsetup in macos script return error message HOT 1
- .os issues HOT 2
- boot in recovery mode
- `.macos` changes in macOS Ventura HOT 2
- Are the macOS defaults sane for Ventura as well? HOT 2
- Arch Chip
- `gz()` is inefficient HOT 2
- In Browser Fast Keyboard Repeat Similar to Terminal (.macos) HOT 1
- `alias ip` collision
- GH Desktop can't open file manager on Linux Mint HOT 1
- `urlencode` is only compatible with Py2
- QuickTime autoplay on open does not work
- `git rev-parse --is-inside-work-tree` does not handle bare Git repositories correctly
- f0e40cf342566f07277b2bec80890be4350e126c
- Check for `ls` flavor in `.aliases` is no longer correct
- com.apple.SoftwareUpdate
- Normalised `open` doesn't work as expected on WSL2
- Add a method for uninstalling the dot files HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dotfiles.