prep
For reasons-reasons other than convenience-that I would not be able to fully articulate should you choose to put a gun to my head, I store my website’s files (WordPress), database and content images separately. I guess if one’s data centre is bombed then the other two will be okay?
Well, back to the convenience. I post a great many images to this blog; for the aforementioned convenience, I store my images in Dropbox. I can quickly and easily move, rename or alter images on my blog without having to piss about with FTP. Now, beyond handling the core image processing I don’t want to get bogged down in resizing, foldering, renaming and uploading all of those files. Because of this I script the action, but my current script only creates preview and thumbnail sizes:
#!/bin/sh
MED=m
SML=s
mkdir -p $MED $SML
for file in $(find . -iname "*.jpg")
do
cp $file $MED
cp $file $SML
done
mogrify -quality 90 -resize 540x540+0+0 $MED/*.jpg
mogrify -quality 90 -gravity center -crop 256x256+0+0 $SML/*.jpg
exit 0
Today I want to add three two new features to it:
- Move the given folder into my
~/Dropbox/public/contentfolder. - Construct the URL code I use and ready it for pasting. Typical code is below:
<a title="Click to view larger size" href="http://dl.dropbox.com/u/4144919/content/1466601748/1.jpg"><img src="http://dl.dropbox.com/u/4144919/content/1466601748/1.jpg" alt="" /></a>
For both steps, I need the ability to output only the name of the current directory I am working in, since this directory will be named according the number to the post. In the case of this blog post the folder name will be 1466601748. I spent about three hours fucking about with awk, sed, Google and pipes before I discovered a very simple one-liner that delivers the folder name unto me:
basename $(pwd)
[mark][1466601748] # pwd
/home/mark/1466601748
[mark][1466601748] # basename $(pwd)
1466601748
Call me stupid. So, #1 is the easiest to achieve:
[mark][1466601748] # mv $(pwd) ~/Dropbox/Public/content
#2, generating the URL code, will be interesting, although trivial too. It seems like the easiest way is to echo the URL while I insert the post number and then stick the hot, steaming result into my clipboard. This works fine…but my sense of neatness is screaming at me that there is probably a more elegant way to achieve my goal:
#!/bin/sh
USERID=4144919
POSTID=$(basename $(pwd))
echo \<a title=\"Click to view larger size\" href=\"http\:\/\/dl.dropbox.com\/u\/$USERID\/content\/$POSTID\/1.jpg\"\>\<img src=\"http\:\/\/dl.dropbox.com\/u\/$USERID\/content\/$POSTID\/m\/1.jpg\" alt=\"\" \/\>\<\/a\> | xclip -sel clip
exit 0
Short version: Escape the shit out of everything and let God judge the survivors. Using $USERID instead of a hard-set user number gives me a little bit of portability for you, the consumer.
#!/bin/sh PUB=/home/mark/Dropbox/Public/content/ MED=m SML=s USERID=4144919 POSTID=$(basename $(pwd)) mkdir -p $MED $SML for file in $(find . -iname "*.jpg") do cp $file $MED cp $file $SML done mogrify -quality 90 -resize 540x540+0+0 $MED/*.jpg mogrify -quality 90 -gravity center -crop 256x256+0+0 $SML/*.jpg echo <a title=\"Click to view larger size\" href=\"http://dl.dropbox.com/u/$USERID/content/$POSTID/1.jpg\"><img src=\"http://dl.dropbox.com/u/$USERID/content/$POSTID/m/1.jpg\" alt=\"\" /></a> | xclip -sel clip mv $(pwd) $PUB exit 0
Enjoy.
/smrt
Categorised as: linux
