Arghh, Downtime

Apologies to anyone that has visited the website in the last couple of days, it would appear that there has been a fairly large amount of downtime. Everything is back up and operational again. I will be keeping a closer eye on the status of the website from now on to try and prevent this type of situation arising again. Thanks for your understanding.

Check back soon.

Update: It would also appear that the category browsing is currently broken, I am working on a fix and will push it out shortly.

Just An Introduction

I’ve been promising to write this post for a while now, it is exactly what is says in the title, an introduction. A good friend of mine, after much persuasion has finally ‘got his blog on’. Fortunately he has a far better blogging style than me and blogs far more often so it shouldn’t be too much of a strain on the senses to bookmark him and/or add him to your feed reader. Funnily enough he is also a keen photographer so don’t forget to check out his Flickr page, you’ll find a link from his blog.

So here he is, (drum roll please), Mr Rob Young: robyoung.me.uk.

Be nice, I don’t want him scared back off the internet, it took long enough to get him on here.

Don’t forget to check back here sometimes too.

Calculating Work Days In Java

In work this week I came across a couple of problems in which I needed to performs some calculations involving dates and a number of “work days” rather than just normal numbers of days. My first few attempts filed miserably, so I did some Googling to see if I could find anyone else that had come across the same problem and of course, there were plenty of people.

The code I eventually used I found shoehorned into the middle of a coding help forum and, with a little bit of tweaking, it calculated the number of whole work days in between two dates. The code is as follows:

public static int calculateDuration(Date startDate, Date endDate)
{
  Calendar startCal = Calendar.getInstance();
  startCal.setTime(startDate);

  Calendar endCal = Calendar.getInstance();
  endCal.setTime(endDate);

  int workDays = 0;

  if (startCal.getTimeInMillis() > endCal.getTimeInMillis())
  {
    startCal.setTime(endDate);
    endCal.setTime(startDate);
  }

  do
  {
    startCal.add(Calendar.DAY_OF_MONTH, 1);
    if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY)
    {
      workDays++;
    }
  }
  while (startCal.getTimeInMillis() <= endCal.getTimeInMillis());

  return workDays;
}

This code solved my first problem, however I couldn’t find any code to solve my second problem, how to calculate a final date from a start date and a specified number of work days, to do the calculation I came up with the following code:

public static Date calculateEndDate(Date startDate, int duration)
{       
  Calendar startCal = Calendar.getInstance();

  startCal.setTime(startDate);

  for (int i = 1; i < duration; i++)
  {
    startCal.add(Calendar.DAY_OF_MONTH, 1);
    while (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
    {
      startCal.add(Calendar.DAY_OF_MONTH, 1);
    }
  }

  return startCal.getTime();
}

Hope this code works as well for you as it did for me.

Check back soon.

Update: Improved the calculating code with the suggestion from Anass (below) and made some more accuracy & business logic improvements to the calculateDuration method. Enjoy.

Flickr BBCode

phpBB is one of the most widely used forum systems on the internet and Flickr is one of the most popular photo sharing websites, unfortunately there is still no concrete way to integrate the two of them. As a temporary solution I came up with the following piece of BBCode to allow for the easy display of Flickr images in phpBB forums.

First, you will need to navigate to the “Admin Control Panel” of phpBB then to the “Posting” section. From here navigate to the screen that allows you to add custom BBCode.

In the “Usage” section paste the following code:

[flickr=<a href="{URL1}" title="{TEXT1}"><img src="{URL2}" width="{NUMBER1}" height="{NUMBER2}" alt="{TEXT2}" /></a>][/flickr]

In the “HTML” section paste:

<a href="{URL1}" title="{TEXT1}"><img src="{URL2}" width="{NUMBER1}" height="{NUMBER2}" alt="{TEXT2}" /></a><br /><a href="{URL1}">{TEXT2}</a>

Then in the “Help” section paste:

Flickr: [flickr=<Flickr, Medium Size, Copy & Paste HTML>][/flickr]

And that’s it, you can use the tag by navigating to one of your photo’s Flickr “All Sizes → Medium” page (e.g. http://www.flickr.com/photos/digitalpardoe/2900618617/sizes/m/), copying the code in the “1. Copy and paste this HTML into your webpage:” box. And inserting it after the “=” in the Flickr BBCode tag, e.g.:

[flickr=<a href="http://www.flickr.com/photos/digitalpardoe/2900618617/" title="Isolation by digital:pardoe, on Flickr"> <img src="http://farm4.static.flickr.com/3095/2900618617_26bc8abc12.jpg" width="500" height="334" alt="Isolation" /></a>][/flickr]

Check back soon.

Updates, Updates, Updates

Again, it has been quite a while since my last post (almost a month in fact) and quite a lot has happened since then. Any regular visitors to the website will have noticed the new theme, the website maintains the new layout released a couple of months ago but the colour scheme has changed to something a bit more minimal. Along with the new theme I have re-introduced some small adverts to the bottom of pages and modified the URL of the blog feed to allow tracking through FeedBurner.

The other main updates to the website are the introduction of My Book Icons 0.8 and My Passport Icons 0.3. The My Book Icons update includes a new icon for the Western Digital My Book Studio II drive. Both of the icon sets now include icons in Windows ICO format and Linux PNG format. On the Mac side of the icon sets, after popular demand, I have added copy & pasteable icons to allow setting of icons using the “Get Info” window in Finder.

You can download My Book Icons here and My Passport Icons here

That’s all for now, check back soon.

My Passport Icons

By popular demand, but a little late, I have released a set of icons to represent the Western Digital My Passport range of external hard drives, this release was driven primarily by requests that I have received and the fact that I have just purchased a WD My Passport drive myself.

For now the release contains only two icons, they are for the red and black “Essential” variants of the My Passport drive. I will be releasing icons for the remaining drives shortly so if you don’t have one of the two drives included keep checking the website.

You can download the icons here.

I will be writing a post shortly to explain why my presence has been lacking recently and why releases are so slow in coming too, so, check back soon.

Just A Few Updates

It’s been almost a month since my last post and a lot has happened since then so I thought I’d bring any readers of my blog up to date.

If you are a frequent visitor to the website you may have noticed the new layout (implemented about 20 days ago now). The layout is much simplified over the old layout. It makes use of a single column layout with items from the old sidebar in the top corners. For now the advertising and Last.fm widgets have been removed whilst I decide if they should make an appearance in the new website. The visual changes comes with a fairly comprehensive code re-write that should drastically improve the performance and security of the website, the changes come thanks to a series of Rails podcasts I have discovered, you can find them here.

A quick list of the main changes to the website are as follows; the removal of all AJAX & AJAX-like functionality (other than Lightbox), everything now works statically for increased accessibility, the blog can now be browsed by category. More user feedback has been introduced. The appearance of forms has been improved and the website now makes sole us of PNG and JPEG images for transparency and quality.

The secondary reason for this post is to officially announce the availability of iSyncIt 1.5, you can download it here. This release, whilst not been too far detached visually from version 1.3.1 comes with a boat load of code improvements. The main changes are as follows; a new preferences system, doesn’t look any different but it’s easier for me to manage. The move to Leopard only. A fix for the problems that were caused in version 1.3 by Growl notifications. You can now enable & disable the login item from within the application. Updates to Sparkle & Growl. Menus now update properly. Scheduling now functions better and the control of bluetooth is improved.

Finally, an unfortunate side-effect of the iSyncIt re-write is the loss of German localization. I will be getting in touch with my friendly volunteer translator shortly to see if he will be willing to re-localize the application for me.

That’s all for now, check back soon.

Caching Your Photographs

If you have at some point followed my now fairly ancient rFlickr tutorial you may have noticed that your photo page loads quite slowly, and that my photo page loads fairly quickly. To get my page to load as quickly as it does required a small custom caching method and a willingness on my part to sacrifice some bandwidth. Here’s how I did it.

This tutorial assumes that you have already worked through the previously mentioned rFlickr tutorial and have something similar to it set up. It also assumes that you have some knowledge of Rails, not that my knowledge was particularly wide ranging at the point I wrote this caching method.

First of all, you will need a table in your database to store the information about your photographs, I suggest the structure illustrated in the migration below:

class CreatePhotos < ActiveRecord::Migration
  def self.up
    create_table :photos, :id => false do |t|
      t.column "flickr_id", :string, :limit => 25, :null => false
      t.column "title", :string, :limit => 250
      t.column "description", :text
      t.column "url", :string, :limit => 250
    end

    add_index :photos, :flickr_id
  end

  def self.down
    drop_table :photos
  end
end

Once you have created this table you will need to create some folders to store the cached images, I created the following folders and will be using them throughout this tutorial:

"#{RAILS_ROOT}/public/images/flickr_cache/small/"
"#{RAILS_ROOT}/public/images/flickr_cache/large/"

Then generate the model for this photos table:

cd /your/rails/application
./script/generate model Photo

Your view from the first tutorial can remain almost the same (details at the end of the post), however, to see the greatest speed improvement I suggest caching it, i.e:

<% cache do %>
... Your view code here. ...
<% end %>

Then modify your view method in your photography controller to read something like:

def view
  unless read_fragment({})
    check_cache
    @photos = Photo.find(:all)
  end
end

The above code will make sure that a cached photography page doesn’t already exist, if it doesn’t, then and only then will it check that the photograph cache is up to date and query the database.

We have not yet created a check_cache method, this method is the core method to make the photography page load much, much faster, even when the photography page’s cache does not exist. The method should be placed as the last method in your photography controller, the code is as follows:

private
def check_cache
  if ENV['RAILS_ENV'] == 'production'
    flickr = Flickr.new(RAILS_ROOT + "/config/flickr.cache", FLICKR_API_KEY, FLICKR_SHARED_SECRET)
    @photos = flickr.people.getPublicPhotos(flickr.people.findByUsername(FLICKR_USERNAME))

    @db_photos = Array.new
    Photo.find(:all).each { |p| @db_photos.push(p.flickr_id) }

    for photo in @photos.reverse
      if [email protected]_photos.include?(photo.id)

        db_photo = Photo.new
        db_photo.flickr_id = photo.id.to_i
        db_photo.title = photo.flickr.photos.getInfo(photo.id).title
        db_photo.description = photo.flickr.photos.getInfo(photo.id).description
        db_photo.url = photo.flickr.photos.getInfo(photo.id).urls.values[0]

        db_photo.save

        open(File.expand_path("#{RAILS_ROOT}/public/images/flickr_cache/small/" + photo.id + ".jpg"),"w").write(open(photo.url('s')).read)
        open(File.expand_path("#{RAILS_ROOT}/public/images/flickr_cache/large/" + photo.id + ".jpg"),"w").write(open(photo.url).read)
      end
    end
  end
end

The following code will only run if you are in production mode (and probably test mode too). It will then load the necessary information from Flickr using the methods outlined in the rFlickr tutorial post. The method then iterates through the collection of photos from Flickr in reverse, so they appear in the same order in the database as the order they appear on Flickr.

It will then check if the photo already exists in the database, if it does not it will store a copy of the photograph’s information in the database and download previews of the images from Flickr to your server, previews of images can then be loaded from your server rather than Flickr’s slow servers.

To take advantage of the cache you will also need to modify your view to access the thumbnails from your newly created local repository rather than from Flickr’s servers, i.e:

<% for photo in @photos.reverse %>
  <%= image_tag("/images/flickr_cache/small/" + photo.flickr_id + ".jpg", :alt => photo.title) %>
<% end %>

Hope this goes some way to helping you improve the speed of your website.

Check back soon.

Update: As I have just been reminded in the comments, I forgot a piece of code to make this tutorial work correctly. You should put the line:

Either in the bottom of you environment.rb file or just under the ‘class’ line in the controller that is responsible for your photography page.

Update: The code in the check_cache method has been updated slightly, it now makes less round trips to the database, should speed things up if your DB server is in a different location to your application server. Also, any redirection problems you may have faced before should be solved by the new rFlickr ruby gem that has support for farm URLs built in.

Oops

Due to a massive oversight on my part at the point I implemented a user groups system on the website it would appear, for standard users at least, that the comments system has not been functioning correctly. The problem has now been solved and the comments system should be working correctly. Apologies to anyone that have ever tried to post a comment on the website for it not to work.

Check back soon.

About Time Too

It’s been well over a month since I managed to write a post now so I thought it was about time I started the cogs whirring again, especially as all my assignments are now finished and handed in.

Even though there has been a lack of posts in the recent month there hasn’t been a lack of feverish activity. First on the books was the release of Set Icon 0.2, this release just bought basic bug fixes and a much improved authentication system. This was followed in recent weeks by the release of Set Icon 0.3, in this release I fixed yet more bugs and introduce an option to remove a custom icon from a hard drive. You can, as always, download Set Icon from its download page.

On a more website orientated note, I have moved the website to new servers. Prime Hosting weren’t terribly Ruby on Rails centric in the end, hence the continual website downtime. The website is now hosted with media72. They have proven themselves over the last month to be far more reliable hosts than Prime, even though I am hosted on their beta testing server.

Along with the change of host I have modified the way in which the website is run, updates to the website are now performed via Capistrano, I hope to write a little more on this system in a later post to explain how it can help improve your Ruby on Rails development.

Don’t expect too many posts or software updates over the next two weeks, unfortunately I have a series of exams that require some serious revision, however, if I get really bored of revising you might see some posts / software appearing.

I’ve also recently acquired a new lens for my camera, a Tokina 12-24mm f/4. As part of my endeavor to expand what I write about I will hopefully (assuming I remember) write a small review about the lens as it took me a long time to find any concrete opinions on it before I purchased it.

That’s all I can think of to write for now, no doubt I will think of something else eventually.

Check back soon.

Set Icon, The Initial Release

After literally tens of emails from people having problems applying the ‘My Book Icons’ to their external hard drives I came to the decision that I needed to write some software to make the whole process a little easier. The result of this decision and a couple of (short) days work is Set Icon.

You may remember from my previous tutorial that setting a custom hard drive icon (correctly) required some Terminal wizardry and some extra command line tools. Set Icon provides and all in one, drag and drop way of setting a custom (ICNS format) icon for a hard drive (including your internal hard drive), without the need for any external tools.

The advantages of using Set Icon rather than copy & paste are; the icon will appear correctly in the Finder sidebar under Leopard, will appear correctly when the hard drive is mounted over a network and will appear correctly in the Boot Camp chooser.

There are a few extra features I plan on adding to Set Icon and I also plan on creating a Tiger compatible version in the near future so all you Tiger users don’t feel left out.

You can download Set Icon from its software page. If you like the software please consider making a donation using the button on the right hand side of the website.

That’s all the news for now, check back soon.

My Book Icons 0.7

It must be that time of the year again, I’m on holiday so I’ve started to update all of the downloads I maintain. The first of these updates, and the one most requested, are the vertical orientation icons for the new Western Digital My Book drives. You can download the new version of these icons from the downloads page. I am planning on updating iSyncIt in the next few weeks too.

The website has also undergone a bit of an update, nothing major, just a few tweaks to make things work a little better now. For example when you login, you will no longer be sent to a confirmation page, you will be returned to the page you were on. Much more convenient when posting comments.

P.S. If you are having trouble applying the icons see if this tutorial helps: Setting Hard Drive Icons In OS X.

That’s all for now, check back soon.

The Advantages Of Time Machine

I wasn’t sure about Time Machine when it was announced, sure it was great to have regular, up-to-date backups with no effort but I couldn’t believe that restoring would be as easy as Apple said it would be. Unfortunately I had a chance to test Time Machine to its fullest in the week just gone.

My iMac was feeling a little sluggish so I ran the cleanup scripts in a program called OnyX, the program has never let me down before but as the scripts were running I noticed the whole contents of my home directory beginning to disappear. I panicked a little but new Time Machine had just completed a backup before I ran the scripts.

First I tried restoring all my data from Time Machine piece by piece but it was taking far too long. The solutions was easy, boot of the Leopard DVD and select the option to restore my whole system from time machine, 45 minutes later the computer rebooted and I was greeted with my old system, all my files, back where they used to be like nothing had happened.

My Aperture library was backed up separately using vaults because Time Machine always insists on backing up the whole library, the restore for Aperture was another half-hour and all my photos were back too.

I couldn’t have asked for an easier restore, yet another shining example of Apple’s software. I won’t personally be using OnyX again but a word of warning, if you don’t use Time Machine, back up your system as frequently as you can afford to. You think data loss will never happen to you but more than likely it will and it saves a lot of trouble (and possibly heartbreak) if you have good backups.

Check back soon for more tales of survival.

rFlickr And Rails 2.0

Any of you that followed my tutorial on setting up rFlickr and have subsequently upgraded to Ruby on Rails 2.0 may have noticed some server errors on your photo pages, this is due to the fact that a single line of code in rFlickr does not work correctly with Rails 2.0.

Line 644 of lib/base.rb had to be modified from:

def from_xml(xml,photo=nil)

to:

def self.from_xml(xml,photo=nil)

Hope this gets you back on your feet, so to speak. Check back soon.

Update: You may also want to install the updated version of actionwebservice because it is no longer bundled with the Rails distribution. Other people have reported rFlickr not working because of a lack of this gem, however, the lack of this gem didn’t affect my setup at all.

Ruby On Rails 2.0

Rails 2.0 has been out for a couple of months now, I had refrained from upgrading to it because I wasn’t sure what it would break. After putting the website under version control I decided it was too easy not to upgrade to Rails 2.0, so here it is, a website that looks and runs exactly like it did and only a few problems to be found.

To go along with the upgrade I have also added a couple of new features to the website, the photography page now links to my Flickr photostream so if you want to post comments on my photos, feel free. I have also added the ability for me to add images from other sources to the blog, not that important but no doubt you’ll notice the change. The largest change at the moment is the addition of social networking sites on the sidebar. If you like what you read to can add the page to Delicious, Digg, reddit or StumbleUpon.

Mainly to test the upgrade, you can admire one of my flickr photos, the most popular of my photos, or check back soon.

Update: Oops, no image. Remember, if you update something to allow longer links, update the database too.

Update: Generally a broken site is a sign of bad testing, that’s true in this case, the archives were broken, now they’re fixed.

Back Online

Just a quick update on the status of the website.

Something has changed with my hosts configuration, they won’t tell me what and I can’t work out what. However it has meant that the website has been down for a day and a half. I have managed to get it back online by reverting from Mongrel to Apache served and running the website on CGI instead of FastCGI. For all the non-techies out there this means the website may respond more slowly than normal, although the caches should keep it moving for the most part and my host may shout at me for increasing the server load. These issues also mean that the uptime of the website is currently unknown, hopefully it will stick around long enough for my host to sort the problem or for me to get a new host.

Check back soon (just to see if I’m still online).

How To Ruin Your Filesystem

It’s been a while since I last posted, and, as my iMac is currently ‘busy’ I thought now would be as good a time as any for my first post of the new year. First of all I have to say, Happy New Year, a bit late I know but better late than never. Now I can get to the true purpose of this post.

This post is a cautionary tale about external hard drives and, what I see as an error in Mac OS, more specifically, Leopard. In my endeavor to keep as many Time Machine backups as possible I have recently retired my old Western Digital My Book Premium 320GB in favor of a Western Digital My Book Studio 1TB. The hard drive itself is great, really quiet, large size. It is due to the large size that I am now in the situation I am in.

Because of all my new found space I realized I could keep Time Machine backups of both my iMac and my PowerBook on the same drive. To keep backups of the PowerBook my idea was to mount the WD drive over AFP on my PowerBook and Time Machine from there, this would have been the sensible thing to do, however backing up over 11GB of the network would have been time consuming so I opted to create a network, temporarily, with firewire, so it would be much faster.

It would appear however that firewire daisy chaining works in mysterious ways, when I connected the iMac and PowerBook with a firewire cable the WD drive was ‘passed through’ to the PowerBook and appeared mounted on both desktops. I thought this was a clever feature of Leopard and used it to my advantage & started the Time Machine backup.

The backup subsequently hung, so I stopped it, decided I couldn’t be bothered and I could live without Time Machine for my laptop. I tried using the drive again on my iMac, first deleting the image created by the PowerBook & received an error. Then after trying to verify the drive and finding it couldn’t be repaired, realized I had found what could be considered to be a bug & a combination of my own stupidity.

I am currently running DiskWarrior and the external drive if full of overlapped files. This is because the ‘pass through’ was not a clever Leopard trick, it was a result of the firewire implementation (that could have been blocked in the OS I am sure), and caused simultaneous writes to my external hard drive to occur.

Fortunately it only appears to have written off my Time Machine backups (it was Time Machine that started up on the iMac whilst the PowerBook was doing the same) and not the other data on the drive, which I managed to salvage (in case DiskWarrior fails) by mounting the drive in read only mode.

Take my advice, think before you do something like this, I should have realized this would happen but I thought Apple were good enough to stop me doing something this stupid.

Check back soon for more useful posts.