Remove Files After a Destroy

Posted by Chad

This blog was started with the purpose of writing mostly about code. I have yet to do so until now. Here we go.

My current project, similar to AmieStreet, requires the uploading of files (eg mp3’s) that are linked to a record in the database. While the database stores all of the information about the song (eg Title) the mp3 itself is stored on the filesystem. If, for whatever reason, we delete the record from the database we should also remove the associated mp3 from the filesystem.

Rails makes this incredibly easy for us. As the convention goes, I will call the delete a destroy from here on out.

Version 1
def after_destroy
  if FileTest.exist?(source)
    FileUtils.rm self.source
  end
  if FileTest.exist?(clip)
    FileUtils.rm clip
  end
end

This was my original code. I have 2 mp3 files, the source (entire song) and the clip (sample from song), each file should be removed from the filesystem after the record is destroyed. Conveniently, the after_destroy callback lets me run my logic after the destroy.

Since I have 2 files, I test to see if each exists if so I remove it. Note: the source and clip methods just return full paths to the files, like RAILS_ROOT+"/path/to/file.mp3".

It could be so much better, let’s DRY things up a bit.

Version 2
def after_destroy
  removables [source, clip]
end
def removables(paths = [])
  paths.each{|p| FileUtils.rm p if FileTest.exist?p }
end

Here I wanted to make a method that I could feed multiple file paths and it would execute the logic. So I made the removables method, which steps through each path in the paths array and removes the file if it exists. This is pretty clean, but it gets better.

Version 3, Final
def after_destroy
  FileUtils.rm_f [source, clip]
end

Upon closer inspection, I noticed that the FileUtils.rm* methods actually accept a list of files to delete. And, with the force option the method does not throw errors if the file is not there. I was so used to always having to check whether a file existed, in order to avoid errors that come up if I tried to delete a nonexistent file, that I did not even think this could be done. Of course, with Ruby, the solution is simple.

Conclusion

This is a perfect example of how I am trying to think like a Rubyist. From V1 to V3, Final took me about 5 minutes so I feel that I am getting a good deal better … and oh so addicted.

Flickr API app on hiatus mode, cue Social Music app

Posted by Chad

Well I have finally completed my first big App with Ruby on Rails. I learned a lot, had a good bit of fun and grew even more addicted to the framework. Ohh, and you must be wondering “Where’s the APP!” It’s a bit of a sad story really. The app is built around the Flickr API and does wonderful things with photos that, as far as I can tell, have not been done before. I found out why, it is damn hard on the servers. My shared account on DreamHost certainly can not support the load and continues to kill off all my processes. So, unfortunately, I am not able to release the App at this time. I simply do not have the server to run it. Hopefully, one day this limitation will not be a problem.

Keep on truckin’ with more development projects. I’ve been trolling through my brain, reading over all the web 2.0 blogs, and generally looking for some inspiration on a project to undertake. A couple of ideas came up.

Dating sites have been done over a billion times, but that’s okay. I read a blog post mentioning the success of PlentyOfFish with their streamlined operations generating revenue from adwords ($10K a day?). POF has an awful user experience; the layout is just plain crap. But hey, you get what you pay for. For a dating service to be useful it has to capture market share which means it has to advertise (paid usually, maybe dessemenated through the masses via blogging would be a good way?) The best/stupidest idea came to me when thinking of ways to finance such an advertising blitz. Note: this is the riskiest financing method around second only to loan sharking which is harmful to your health, this method is extremely harmful to your wealth.

  1. Develop a killer App and make it free. Obviously, once users visit your site they will never want to use another dating service again. That’s how good it is.
  2. Sell short the stocks of your competitors (Match, Y!, eHarmony, etc … I assume these firms are public, I have not researched whether they are)
  3. Use the income received from the sale of stock options to finance a massive marketing campaign
  4. You’re filthy rich, you stole everyone’s market share, their earning tanked and now their stock is worthless so the option will not be executed. You can find ways to get your own $10K a day via adwords or monetize this large audience via other routes.

Like I said, this is stupid, I know it. Just a day dream while at the dog park earlier today, but I thought it was a pretty creative financing method though – from a perspective that risk is of no concern.

Okay, so if dating services are out, what is my next app? Amie.st is a great we application for fair music pricing with no DRM and it supports independent artist to a large degree. I’ve decided to make my own version of their site. Unlike my dating site discussion above, I do not want to develop a competing product. I simply want to use their domain model to enhance my knowledge of Ruby on Rails. After all, that’s what it’s all about.