Skip to content

Commit

Permalink
Encrypt file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelavila committed Nov 21, 2015
1 parent b88b5c5 commit 8563a18
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
SECRET_KEY_BASE=123
BB_SECRET='this is a test secret'
8 changes: 0 additions & 8 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,4 @@ def has_text_or_file?
errors[:base] << "Either file or text are required"
end
end

def file_mime_type
Mime::Type.lookup_by_extension(file_extension).to_s
end

def file_name
"#{id}.#{file_extension}"
end
end
15 changes: 11 additions & 4 deletions lib/crypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ def file_contents
raise CryptError
end

def file_mime_type
@message.file_mime_type
def file_extension
@crypto.decrypt64 @message.file_extension
rescue OpenSSL::Cipher::CipherError
raise CryptError
end

def file_name
@message.file_name
"#{@message.id}.#{file_extension}"
end

def file_mime_type
Mime::Type.lookup_by_extension(file_extension).to_s
end

# Active Record API
Expand All @@ -51,7 +57,8 @@ def destroy

def save
if has_file?
@message.file_contents = @crypto.encrypt64 @message.file_contents
@message.file_contents = @crypto.encrypt64 @message.file_contents
@message.file_extension = @crypto.encrypt64 @message.file_extension
end
if has_text?
@message.text = @crypto.encrypt64 @message.text
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/messages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
before do
allow(Crypt).to receive(:find).with(guid, key) { crypt }
allow(crypt).to receive(:file_contents) { "secret message" }
allow(crypt).to receive(:file_extension) { "txt" }
end

it "sends a file" do
Expand Down
9 changes: 6 additions & 3 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "crypt"
require "rails_helper"

describe Message do
Expand All @@ -8,12 +9,14 @@
end

it "determines its mime type" do
expect(Message.new(file_extension: "txt").file_mime_type).to eq "text/plain"
message = Crypt.new Message.new(file_contents: "foo bar", file_extension: "txt")
message.save
expect(message.file_mime_type).to eq "text/plain"
end

it "generates a filename from the id and extension" do
message = Message.new(id: "1b0dad15-732e-45cc-8da4-749b4b21585d", file_extension: "txt")

message = Crypt.new Message.new(id: "1b0dad15-732e-45cc-8da4-749b4b21585d", file_contents: "foo bar", file_extension: "txt")
message.save
expect(message.file_name).to eq "1b0dad15-732e-45cc-8da4-749b4b21585d.txt"
end
end

0 comments on commit 8563a18

Please sign in to comment.