Last active
August 29, 2015 14:21
-
-
Save Haroenv/b8751aa70175aac7a58b to your computer and use it in GitHub Desktop.
Three functions to share images from the shell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# command line file sharing # | |
# needed: a git repo on the gh-pages branch # | |
# usage: s <file> # | |
############################################# | |
function s { | |
red='\033[0;31m' | |
NC='\033[0m' | |
gitrepo="/Users/haroenviaene/git/s/" | |
site="http://haroen.me/s/" | |
cp "$1" "$gitrepo" | |
link=$(echo "$1" | rev | cut -d"/" -f1 | rev) | |
git -C $gitrepo add "$file" | |
git -C $gitrepo commit -m "$link" | |
git -C $gitrepo push | |
status=$(curl -s -o /dev/null -w "%{http_code}" $site$link) | |
echo -n "$status" | |
while [[ $status == "200" ]]; do | |
status=$(curl -s -o /dev/null -w "%{http_code}" $site$link) | |
echo -n "." | |
sleep 1 | |
done | |
echo | |
echo "$site$link" | pbcopy | |
echo -e "${red}$site$link${NC}" | |
return 0 | |
} | |
# basic url shortening # | |
# needed: a git repo on the gh-pages branch # | |
# usage: l <link to shorten> <short part> # | |
############################################# | |
function l { | |
red='\033[0;31m' | |
NC='\033[0m' | |
gitrepo="/Users/haroenviaene/git/s/" | |
site="http://haroen.me/s/" | |
sh="$1" | |
if [[ ${sh:0:4} != "http" ]]; then | |
sh="http://$1" | |
echo "added" | |
fi | |
to="$2" | |
mkdir "$gitrepo""$to" | |
echo "<!DOCTYPE HTML> | |
<meta charset='UTF-8' /> | |
<meta http-equiv='refresh' content='1'; url='$sh' /> | |
<script> | |
window.location.href = '$sh' | |
</script> | |
<title>Page Redirection</title> | |
If you are not redirected automatically, follow the <a href='$sh' />link.</a>" > "$gitrepo""$to"/index.html | |
git -C $gitrepo add "$file" | |
git -C $gitrepo commit -m "$to" | |
git -C $gitrepo push | |
status=$(curl -s -o /dev/null -w "%{http_code}" $site$to/index.html) | |
echo -n "$status" | |
while [[ $status != "200" ]]; do | |
status=$(curl -s -o /dev/null -w "%{http_code}" $site$to/index.html) | |
echo -n "." | |
sleep 1 | |
done | |
echo | |
echo "$site$to" | pbcopy | |
echo -e "${red}$site$to${NC}" | |
return 0 | |
} | |
# encoding uri's with percent notation # | |
# usage: rawurlencode <to encode> # | |
######################################## | |
rawurlencode() { | |
local string="${1}" | |
local strlen=${#string} | |
local encoded="" | |
for (( pos=0 ; pos<strlen ; pos++ )); do | |
c=${string:$pos:1} | |
case "$c" in | |
[-_.~a-zA-Z0-9] ) o="${c}" ;; | |
* ) printf -v o '%%%02x' "'$c" | |
esac | |
encoded+="${o}" | |
done | |
echo "${encoded}" | |
} | |
# basic image sharing page with embedding # | |
# needed: a git repo on the gh-pages branch # | |
# usage: i <image to share> <short part> # | |
############################################# | |
function i { | |
red='\033[0;31m' | |
NC='\033[0m' | |
gitrepo="/Users/haroenviaene/git/s/" | |
site="http://haroen.me/s/" | |
sh=$(rawurlencode "$(echo "$1" | rev | cut -d"/" -f1 | rev)") | |
title=$(echo "$1" | rev | cut -d"/" -f1 | rev) | |
to="$2" | |
width=$(convert "$1" -print "%w" /dev/null) | |
height=$(convert "$1" -print "%h" /dev/null) | |
description="Image shared with Haroen.me" | |
twitter="haroenv" | |
mkdir "$gitrepo""$to" | |
cp "$1" "$gitrepo""$to" | |
echo "<!DOCTYPE html> | |
<html lang='en' /> | |
<head> | |
<title> $title </title> | |
<meta charset='utf-8' /> | |
<meta name='viewport' content='width=device-width, initial-scale=1.0' /> | |
<meta name='apple-mobile-web-app-capable' content='yes' /> | |
<meta name='mobile-web-app-capable' content='yes' /> | |
<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' /> | |
<meta name='theme-color' content='#7f1734' /> | |
<link rel='apple-touch-icon' href='/apple-touch-icon.png' /> | |
<link rel='icon' href='/apple-touch-icon.png' /> | |
<link rel='icon' type='image/png' href='/favicon.png' /> | |
<link rel='image_src' href='$site$to/$sh' /> | |
<link rel='canonical' href='$site$to' /> | |
<meta property='og:type' content='article' /> | |
<meta property='og:title' content='$title' /> | |
<meta property='og:url' content='$site$to/' /> | |
<meta property='og:site_name' content='$site' /> | |
<meta property='author' content='$name' /> | |
<meta property='article:author' content='$name' /> | |
<meta property='og:image' content='$site$to/$sh' /> | |
<meta property='og:image:width' content='$width' /> | |
<meta property='og:image:height' content='$height' /> | |
<meta property='og:description' content='$description' /> | |
<meta name='twitter:card' content='photo' /> | |
<meta name='twitter:site' content='@$twitter' /> | |
<meta name='twitter:domain' content='$site' /> | |
<meta name='twitter:creator' content='@$twitter' /> | |
<meta name='twitter:title' content='$title' /> | |
<meta name='twitter:image' content='$site$to/$sh' /> | |
<meta name='twitter:url' content='$site$to' /> | |
<meta name='twitter:description' content='$description' /> | |
<style> | |
body { background-color: #7F1734; text-align: center; } | |
.main img { max-width: 98%; max-height: 98vh; } | |
.lightbox { display: none; position: fixed; z-index: 2; width: 100%; height: 100%; top: 0; left: 0; background: rgba(0, 0, 0, 0.2); overflow: auto; } | |
.lightbox img { max-width: inherit; max-height: inherit; margin: 1%; border-radius: 5px; } | |
.lightbox:target {display: block;outline: none; } | |
</style> | |
</head> | |
<body> | |
<a href='#large' class='main' /> | |
<img src='$sh' /> | |
</a> | |
<a href='#_' class='lightbox' id='large' /> | |
<img src='$sh' /> | |
</a> | |
</body> | |
</html> | |
" > "$gitrepo""$to"/index.html | |
git -C $gitrepo add "$to" | |
git -C $gitrepo commit -m "$to" | |
git -C $gitrepo push | |
status=$(curl -s -o /dev/null -w "%{http_code}" $site$to/index.html) | |
echo -n "$status" | |
while [[ $status != "200" ]]; do | |
status=$(curl -s -o /dev/null -w "%{http_code}" $site$to/index.html) | |
echo -n "." | |
sleep 1 | |
done | |
echo | |
echo "$site$to" | pbcopy | |
echo -e "${red}$site$to${NC}" | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment