Skip to content

Commit

Permalink
Add support to delete files.
Browse files Browse the repository at this point in the history
  • Loading branch information
roidrage committed Apr 1, 2012
1 parent 17cc1e4 commit 5c31f77
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -10,7 +10,8 @@ Installation

The app assumes you're storing files in a bucket that has a CNAME attached to
it, e.g. s3itch.mydomain.com, and that you're setting this CNAME as base URL in
Skitch.
Skitch. Skitch sends a HEAD request to the base URL (your S3 bucket) after
uploading to check if the file was properly stored.

It's made for deployment on Heroku:

Expand Down
20 changes: 16 additions & 4 deletions app.rb
Expand Up @@ -6,22 +6,34 @@
require 'mime/types'

class S3itchApp < Sinatra::Base
# When Skitch uploads via WebDAV, it uses
# the file name as the URL and includes the
# image in the body.
put '/:name' do
retries = 0
s3 = Fog::Storage.new(provider: 'AWS', aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_REGION'])
begin
directory = s3.directories.get(ENV['S3_BUCKET'])
content_type = MIME::Types.type_for(params[:name]).first.content_type
file = directory.files.create(key: params[:name], public: true, body: request.body.read, content_type: content_type)
file = bucket.files.create(key: params[:name], public: true, body: request.body.read, content_type: content_type)
puts "Uploaded file #{params[:name]} to S3"
redirect "http://#{ENV['S3_BUCKET']}/#{params[:name]}", 201
rescue => e
puts "Error uploading file #{params[:name]} to S3: #{e.message}"
if e.message =~ /Broken pipe/
if e.message =~ /Broken pipe/ && retries < 5
retries += 1
retry
end

500
end
end

delete '/:name' do
file = bucket.files.find(params[:name])
file.destroy
end

def bucket
s3 = Fog::Storage.new(provider: 'AWS', aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: ENV['AWS_REGION'])
s3.directories.get(ENV['S3_BUCKET'])
end
end

0 comments on commit 5c31f77

Please sign in to comment.