Skip to content

Commit

Permalink
Allow deleting of videos even if a video file is missing (filesystem …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Martyn Loughran committed Oct 8, 2008
1 parent 9833053 commit 41b1812
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/models/video.rb
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/abstract_store.rb
@@ -1,4 +1,6 @@
class AbstractStore
class FileDoesNotExistError < RuntimeError; end

def initialize
raise "Method not implemented. Called abstract class."
end
Expand All @@ -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
Expand All @@ -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
6 changes: 5 additions & 1 deletion lib/file_store.rb
@@ -1,4 +1,4 @@
class FileStore
class FileStore < AbstractStore
include FileUtils

def initialize
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lib/s3_store.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 41b1812

Please sign in to comment.