Recover deleted photos

Shit happens… but for Linux users, there is a power tool straight from the US Air Force called "foremost" to help you recover (aka undelete) your photos easily!
Note: you’ll need sudo rights to install this software tool.

But most importantly: do not write any new data to your device (SD card, flash drive or whatever)! Otherwise, you’ll overwrite what is still there and lose your photos forever.

The steps:
Remove or unplug the media/drive/card from your comupter.
Open a terminal window.

Install the recovery software.

sudo apt-get install foremost

Create a folder to put the recovered photos in. Important: don’t put this forler on the drive where you want to recover photos from! See above…

mkdir recovered-photos && cd recovered-photos

Find the device: copy the following command to your terminal and press the [Tab] key twice

foremost -v -t jpg /dev/sd

It will say something like this

sda   sda1  sda2  sda3  sda4  sda5  sda6  sdb

Now, plug in your drive/card/medium and press [Tab] again, twice.
It should change to something like this

sda   sda1  sda2  sda3  sda4  sda5  sda6  sdb  sdc  sdc1

In my case sdc and scd1 showed up. Thus, sdc1 is the partition on the device to recover from. Thus, I my case the full command would be (You need to replace sdc1 with whatever showed up previously on your own terminal!)

foremost -v -t jpg /dev/sdc1

… press return and see the magic happen! Your photos will show up in a subfolder of recovered-photos. 🙂

Setting up a linux server to forward all incoming mails

Typical thing when having a new site up: you want to get to end up in you normal inbox, say Gmail.

Here are the steps!
Note: If you are logged in as root, you do not need to use “sudo” in front of each command.


It’s recommended to have a user account to receive the mail other than root.
So either choose a user you have created already or create a new one:

sudo adduser [username]

Does your provider require you to use an SMTP relay? (check the documentation, or ask). You need to know this to continue.

Install postfix

Postfix is a software package, a MTA, it handles incoming mail and forwarding.

sudo apt-get install postfix

And answer all configuration questions as below:
Server configuration type: If you use an SMTP relay, choose “Internet with smarthost”, otherwise “Internet”
System mail name: [] (no subdomain)
SMTP relay host: [] (only showing if you chose “Internet with smarthost”)
Root and postmaster mail recipient: The created/chosen before (not root)
Other destinations to accept mail for: keep the suggested defaults and add in front [] and a space
Force synchronous updates on mail queue: no
Local networks: keep suggested
Use procmail for local delivery?: yes
Mailbox size limit: 0
Local address extension character: +
Internet protocols to use: all

Manual configuration

All edits with sudo if you not logged in as root.
Edit /etc/postfix/ add following two lines to the end

virtual_alias_domains = []
virtual_alias_maps = hash:/etc/postfix/virtual

Create/edit /etc/postfix/virtual and add

@[]    []

Finally, after editing this file call

sudo postmap /etc/postfix/virtual

so that postmap actually applies your changes.


If you forward to Gmail, do not use the same address to send test mails that the mails will be forwarded to – Gmail will silently ignore/delete mail where the sender and recipient are the same and you will think the forwarding does not work.

Scale images for the web — quickly

Starting Gimp just to scale down some photos to send them by mail? Meh.
How about mark them, right-click, "Send To" -> "Scale for web"?
No problemo:
First, get a tool to scale images:

sudo apt-get install imagemagick

Now, create a script that takes a list of images as input, scales them down, and stores them with a prefixed file name. Go!

pwd > ~/f0.txt
echo "$@" > ~/f1.txt
for file in $@
	convert "$file" -quality 50 -resize 1024x768 "${file%/*}/web-${file##*/}"

Make sure we chmod +x the script file we just created.
Next, let’s integrate it into "Send To". (This is an example for Thunar)
Create a .desktop file in the sendto folder, e.g. /usr/share/Thunar/sendto/scale-for-web.desktop with following content:

# Scale images for the web
[Desktop Entry]
Exec=scale-for-web %F
Name=Scale for the web

Replace both scale-for-web with the name you gave to your script file. Is the script’s folder in $PATH? No idea? Then just put the entire path+filename.
Now, open Thunar, select some images (Jpeg or Png), right-click, "Send To" -> "Scale for the web", wait a few seconds and you’ll see web-....jpg files appearing in the same folder. 🙂

References: how to scale images with imageMagick, extract path segments, more details on string manipulation

Serving Static Files with Meteor and Spiderable

In short: To serve files which are not part of your project’s code (i.e. which are supposed to be served without being compiled), put them in the public folder. This folder will be served from /.

Some more background and details:
Tried to add a web-font to my project, thus, I put it in /client/webfonts/somefont.woff. But the font didn’t show up. I tested it with wget -O- http://localhost:3000/webfonts/somefont.woff … and *surprise* I got the HTML of the default page — not some binary gibberish that you would expect to find in a font file.

All file types unknown to meteor get stripped out during compile time. Instead you need to put them in a separate folder called public — in my case in /public/webfonts/somefont.woff. Rookie mistake I guess. 🙂

Callback Method for Twitter’s Typeahead

When you start using Twitter’s typeahead, e.g. because you switched from Bootstrap 2.3 to 3.0, you will notice that it is NOT a simple drop-in replacement for Bootstrap’s original typeahead!
Before, you generated the suggestions shown to the user by implementing a callback method while creating the typeahead like this:

  source: function(query, process) { 
    ... return a list of suggestions (see doc for details) 

Since version 3, Bootstrap does not provide its own typeahead any more, instead it is using Twitter’s own implementation which seems to be a good choice at least on the long run. But it does not provide any mean for defining a callback method to provide the suggestions — no clue why.
Instead you can provide a predefined array locally, a predefined array that will be loaded on start-up, or a remote service defined by a URL.
You will find several good examples of why you need a callback method sometimes.

  • You want/have to use a Javascript library to access the service.
  • There is no database for the suggestions, they a generated on the fly, e.g.if you want to suggest sentence completion while the user types.
  • You are using Meteor’s collections holding your suggestions, thus, there is no URL you could point to.

But taking a look into the internals revealed following solution: completely by-pass the internal suggestion aggregation of Twitter’s typeahead by replacing the getSuggestions(query, callback) method of the first dataset, which we defined by local: [];.

(t = $("field")).typeahead({ local: [] });"ttView").datasets[0].getSuggestions = function(query, callback) {
  var suggestions = ... gather your suggestions here ...
  var data = [];
  for (suggestion in suggestions) { data.push(this._transformDatum(suggestion)); }

(Translated from Coffeescript on-the-fly – consider as a draft than an out-of-the-box solution.)

Happy hacking. 🙂

p.s. for Meteor users: The callback breaks each time the template gets re-rendered – despite that you use the package preserve-inputs or <#constant> due to a bug. Somehow, constant areas and that particular package do not get along with each other. Solution: removed the package and use constant areas around typeahead input fields works. This should be fixed as soon as the new template engine gets rolled out.

How to Use JQuery in GWT

After adding the JQuery script to your project HTML file’s header section

<script src="" type="text/javascript"></script>

You will be able to use JQuery in native (JSNI) methods by prepending $wnd., e.g.:

private native void someMethod() /*-{
	$wnd.$(function() {

Without $wnd. you will end up with following error: (ReferenceError): $ is not defined

JUnit-Testing and GAE APIs

Some issues I encountered when testing a new Java library for loading multiple URLs in parallel on the Google Apps Engine (GAE) (more about that library in another post).

Here are some of the typical errors you might encounter:

Missing timeout class

java.lang.NoSuchMethodError: org.mortbay.thread.Timeout

Context menu on project: Properties -> Build Path -> Order and Export: move GWT SDK *above* App Engine SDK (cf. here)

Compilation unit was not seen The test class 'com.test.client.MyTest' was not found in module 'com.text.Module'; no compilation unit for that type was seen

  • The test class needs to be within a package that is configured in the module file (*.gwt.xml). You should create a new source folder (Context menu on project -> New -> Source Folder) and create the whole package structure you use within your project up to client, e.g., and put your GWT tests in it. This way you make sure that the tests are not going to be deployed to GAE later on.
  • If you are testing client-side code, your test class needs implement GWTUnit *and* be run as GWTUnitTest *not* as JUnitTest 😉

API package not found

java.util.concurrent.ExecutionException:$CallNotFoundException: The API package 'urlfetch' or call 'Fetch()' was not found.

The GAE environment has not been initialized.

  • Set up the required libraries for testing:
    • Context menu on project -> Properties -> Build Path -> Libraries -> Add Variable -> Configure Variables -> New…: Set "SDK_ROOT" as name and search you disk for "appengine-api-stubs.jar" and copy the part before "/lib/impl" into the text box named "Path"
    • Select "SDK_ROOT" from the previous dialog, click "Extend…", unfold lib and impl and select "…labs.jar", "…stubs.jar", and "…api.jar"; unfold "lib/testing" and select "…testing.jar" — done, whew.
  • Add following lines to your test class:
    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalURLFetchServiceTestConfig());
    @Before public void setUp() { helper.setUp(); }
    @After public void tearDown() { helper.tearDown(); }
  • Have a look in to see all the services/APIs available for testing and replace LocalURLFetchServiceTestConfig with the one you need.

I tried to keep it as brief as possible, for more details have a look at this page.

Multiple Projects and GWT

When working on several large scale projects (in Eclipse), it’s convenient and of course more efficient to share and reuse code via common libraries. While those are in an early stage they need to be changed a lot and therefore it’s handy to link projects in instead of creating new jars each time the library has been updated.
Unfortunately, this standard approach for Java development in Eclipse does not work that straight forward as with plain old Java projects, it requires three steps in total:

  1. Link the library project to all relevant projects ("Project Preferences" -> "Java Build Path" -> "Projects" -> "Add…")
  2. Then, add the client-side code of the library (by adding it as a module.) Therefore, edit the gwt.xml of your application and add for example <inherits name="net.svenbuschbeck.gwt.lib.SuperLib "/> where SuperLib is the file name of the gwt.xml file in you library project and before that is the package it lies in.
  3. Add the code to the project by linking a source folder. Unfortunately, this is required if you do not want to write an Ant script to compile your application. (If you prefer Ant check this out) I don’t like the idea of using such a script because if you forget to run it each time you make changes, you will end up in confusion—let’s go for the convenient, automatic way then.
    1. Add a folder to your application project; open the "advanced" section in the folder creation dialog, select "Link to alternate location" and pick the source folder (usually "src") of your library project. (Hint: if you work within a team using a versioning system, you can create a variable for that folder and use this instead. This way, each of your colleagues can put the library project in a different folder and accommodate for that by adjusting the variable 🙂 )
    2. Right click the folder, "Build Path" -> "Use as Source Folder". Done.

Surprisingly, the GWT plugin for Eclipse does not honor the project linking, thus all the references need to be made explicit or you will end up with lots of the following: ClassNotFoundException.

How to Execute Code When the GWT Application Is Going Down

My goal was to store the UI state of my application just before it gets terminated to be able to restore it next time the way the user left it the other day.

I tried to add an addAttachHandler to the RootPanel to get informed about the root panel getting detached from the DOM so that I can finalize my application. Surprisingly, that does not work in Chrome (tested Chrome and Firefox only).

But besides that this sounds like a bug to me, I found the “proper” way of doing things before the application ends:

Window.addWindowClosingHandler(new Window.ClosingHandler() {
	@Override public void onWindowClosing(ClosingEvent event) { ... } 

In the end, I think something like Document.addUnloadHandler would be more suggestive… closing the window or reloading a page is both exiting the application by unloading the DOM – not closing the window.

Java Cookies from the Future Past

While working with cookies in Java/GWT and thus—to set the expire date—with Date, I found a doubtful Java behavior.

My goal was to set a cookie to expire in about one month from today like this:

Date expires = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 30);
Cookies.setCookie("myCookie", "myData", expires);

And kept wondering why the cookie never got stored.

And finally created a simple test case like this:

Date today = new Date();
Date tomorrow = new Date(today.getTime() + 1000 * 60 * 60 * 24);
Date nextMonth = new Date(today.getTime() + 1000 * 60 * 60 * 24 * 30);

And got following dates:

today=Mon Feb 07 14:27:50 CET 2011
tomorrow=Tue Feb 08 14:27:50 CET 2011
nextMonth=Tue Jan 18 21:25:02 CET 2011

According to Java’s calculation, the cookie was expired already before even being set.

Took me a bit to understand why:
1000 * 60 * 60 * 24 * 30 = 2,592,000,000 = 0x9A7EC800
Thus, the first bit got set to one… a classical overflow causing the integer value to become negative – just try:

System.out.println(1000 * 60 * 60 * 24 * 30);

It will print out -1702967296.

Fix: Add a little L will solve the issue by forcing the compiler to calculate using the scope of long:

Date nextMonthLong = new Date(today.getTime() + 1000L * 60 * 60 * 24 * 30);

I guess I will fall for that one again sometime as the error is not obvious in my opinion — especially because getTime() returns a long and still, the compiler sticks with an int for the multiplication part.