From 41b181287c8ef5a510f2817909f9e2f00ac51b62 Mon Sep 17 00:00:00 2001 From: Martyn Loughran Date: Mon, 6 Oct 2008 15:40:04 +0100 Subject: [PATCH] Allow deleting of videos even if a video file is missing (filesystem or S3). * A filesystem error should not prevent a video being deleted from the db * An error message is logged to console if this case occurs. --- app/models/video.rb | 4 ++++ lib/abstract_store.rb | 12 +++++++++++- lib/file_store.rb | 6 +++++- lib/s3_store.rb | 6 ++---- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/models/video.rb b/app/models/video.rb index c851d2f..e2f8dbb 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -170,8 +170,12 @@ def fetch_from_s3 Store.get(self.filename, self.tmp_filepath) end + # Deletes the video file without raising an exception if the file does + # not exist. def delete_from_s3 Store.delete(self.filename) + rescue AbstractStore::FileDoesNotExistError + false end def capture_thumbnail_and_upload_to_s3 diff --git a/lib/abstract_store.rb b/lib/abstract_store.rb index a52bc3b..7d7b809 100644 --- a/lib/abstract_store.rb +++ b/lib/abstract_store.rb @@ -1,4 +1,6 @@ class AbstractStore + class FileDoesNotExistError < RuntimeError; end + def initialize raise "Method not implemented. Called abstract class." end @@ -8,12 +10,13 @@ def set(key, tmp_file) raise "Method not implemented. Called abstract class." end - # Get file. + # Get file. Raises FileDoesNotExistError if the file does not exist. def get(key, tmp_file) raise "Method not implemented. Called abstract class." end # Delete file. Returns true if success. + # Raises FileDoesNotExistError if the file does not exist. def delete(key) raise "Method not implemented. Called abstract class." end @@ -22,4 +25,11 @@ def delete(key) def url(key) raise "Method not implemented. Called abstract class." end + + private + + def raise_file_error(key) + Merb.logger.error "Tried to delete #{key} but the file does not exist" + raise FileDoesNotExistError, "#{key} does not exist" + end end diff --git a/lib/file_store.rb b/lib/file_store.rb index 83422e9..b602d8c 100644 --- a/lib/file_store.rb +++ b/lib/file_store.rb @@ -1,4 +1,4 @@ -class FileStore +class FileStore < AbstractStore include FileUtils def initialize @@ -15,11 +15,15 @@ def set(key, tmp_file) # Get file. def get(key, tmp_file) cp(@dir / key, tmp_file) + rescue + raise_file_error(key) end # Delete file. Returns true if success. def delete(key) rm(@dir / key) + rescue + raise_file_error(key) end # Return the publically accessible URL for the given key diff --git a/lib/s3_store.rb b/lib/s3_store.rb index 9a26850..607cea4 100644 --- a/lib/s3_store.rb +++ b/lib/s3_store.rb @@ -38,8 +38,7 @@ def get(key, tmp_file) sleep 3 end rescue - Merb.logger.error "Error fetching #{key} from S3" - raise + raise_file_error(key) else true end @@ -54,8 +53,7 @@ def delete(key) sleep 3 end rescue - Merb.logger.error "Error deleting #{key} from S3" - raise + raise_file_error(key) else true end