Anton Jenkins | February 23, 2009
Because the Enki blogging engine currently doesn’t have a method for uploading files for use in your posts I needed to come up with a quick and easy solution. This solution will also work for any website which doesn’t have an upload facility built in. The technique used is easily replicated.
Could we use git?
It would be tempting to just check the images that are going to be used in posts into git and be done with it. They could be stored in the
/public/images directory and get uploaded onto the server when you deploy. But this is messy. Version control is for files that are part of the infrastructure of your site, not the actual content.
So we need a different solution which maintains this separation.
When you deploy with capistrano it creates a symbolic link from
current/public/system to the
shared/system directory. Because we don’t want to lose our uploaded images whenever we redeploy it makes perfect sense to store our images in the shared area (as this persists across deploys) and have the current deploy link across to them.
But first we need to get them there. What we’re going to do is create the file structure on our local development copy of the site and then use capistrano and rsync to push it up to the server.
To start with we need to create the
system directory in
public. Oh, and don’t forget to add this folder to your
.gitignore! As discussed above, we don’t want this in version control. I’ve opted for the following structure to mimic the structure of the URLs that Enki uses :
As you can see, all the images and files will be in the
system/assets directory. You’ll need to ssh into your server and create the assets directory in the corresponding system directory before taking the next step.
We should be at this stage :
Now we need a capistrano task in our
deploy.rb to push the files in our local
assets directory up to the
assets directory on the server :
1 2 3 4
desc "Sync assets on server with development public/system/assets" task :sync_assets do `rsync -qrpt --delete --rsh=ssh public/system/assets firstname.lastname@example.org:/path-to-my-app/shared/system/assets` end
Note : Don’t forget to alter the above command and change the destination to fit your environment. Unless of course your app does live at
The combination of rsync and ssh is perfect for uploading the files up to the server. It is important to be aware of the
--delete option used. If we delete a file at the source then when we next rsync it will delete the corresponding file at the destination. So make sure you keep all your images on your local machine otherwise the next sync will ditch them on the server!
With everything in place it’s simply a case of firing off the task :
You won’t get much clue as to whether it worked so ssh into the server and make sure everything has copied over. If it’s not worked then try copying the rsync command and running it from the command line to look at the output. You might have got the paths wrong or something simple like that.
If it’s worked the files can be accessed from your posts using the textile markup like so :
As a quick solution it works quite well!