Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload Images in Moments #1444

Merged
merged 14 commits into from
Jun 25, 2019
17 changes: 15 additions & 2 deletions app/controllers/moments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class MomentsController < ApplicationController
include MomentsFormHelper
include Shared

before_action :set_moment, only: %i[show edit update destroy]
before_action :load_viewers, only: %i[new edit create update]
before_action :set_moment, only: %i[show edit update destroy picture]
before_action :load_viewers, only: %i[new edit create update picture]

# GET /moments
# GET /moments.json
Expand Down Expand Up @@ -70,6 +70,19 @@ def destroy
redirect_to_path(moments_path)
end

# POST /moments/1/picture
# POST /moments/1/picture.json
def picture
# to do: add image upload options
cloudinary_response = CloudinaryService.upload(file, options)

return if cloudinary_response.nil?

moment = set_moment(params[:moment_id])
moment.picture_id = cloudinary_response['public_id']
moment.save!
end

private

def set_moment
Expand Down
20 changes: 20 additions & 0 deletions app/services/cloudinary_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true
include CloudinaryHelper
include ReactOnRailsHelper

# TODO: better exception handling. and error reporting
class CloudinaryService
class << self
def upload(file, options = {})
Cloudinary::Uploader.upload(file, options)
rescue StandardError
nil
end

def delete(public_id, options = {})
Cloudinary::Uploader.destroy(public_id, options)
rescue StandardError
nil
end
end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
end
end

resources :moments
resources :moments do
post 'picture', to: 'moments#picture', as: 'picture'
end

resources :secret_shares, only: %i[create show destroy]

Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/moments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def post_create(moment_params)
end
end

context 'when the useri_d is hacked' do
it 'creates a new moment, ignoring the useri_d parameter' do
context 'when the user_id is hacked' do
it 'creates a new moment, ignoring the user_id parameter' do
# passing a useri_d isn't an error, but it shouldn't
# affect the owner of the created item
another_user = create(:user2)
Expand Down
53 changes: 53 additions & 0 deletions spec/services/cloudinary_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true
FILENAME = "moment.jpeg"
FILE_PATH = "/spec/uploads/"

describe CloudinaryService do
subject { described_class }

describe '#upload' do
context 'successfully' do
it 'returns json response from cloudinary' do
file = File.new(File.join(::Rails.root.to_s, FILE_PATH, FILENAME))
response = subject.upload(file)

expect(response).to have_key("public_id")
expect(response).to have_key("secure_url")
expect(response).to have_key("signature")
expect(response["original_filename"]).to eq("moment")
end
end

context 'it gracefully fails' do
it 'returns nil' do
file = File.new(File.join(::Rails.root.to_s, FILE_PATH, FILENAME))
response = subject.upload(nil)
expect(response).to be nil
end
end
end

describe '#delete' do
context 'successfully' do
it 'returns success json response from cloudinary' do
# upload image
file = File.new(File.join(::Rails.root.to_s, FILE_PATH, FILENAME))
uploaded_file = subject.upload(file)
expect(uploaded_file).to have_key("public_id")
expect(uploaded_file).to have_key("secure_url")

# delete image
response = subject.delete(uploaded_file["public_id"])
expect(response).to eq({"result"=>"ok"})
end
end

context 'gracefully fails' do
it 'returns not found' do
# upload image
response = subject.delete("untitled")
expect(response).to eq({"result"=>"not found"})
end
end
end
end
Binary file added spec/uploads/moment.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.