Skip to content

Commit

Permalink
Update Photo upload url for add user login as photo path
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jan 7, 2021
1 parent 67c0e0b commit 1cffe09
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
end

# 浮动窗口上传
@photo = Photo.new(image: params[:file], user_id: current_user.id)
@photo = Photo.new(image: params[:file], user: current_user)
if @photo.save
render json: { ok: true, url: @photo.image.url(:large) }
else
Expand Down
1 change: 1 addition & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class Photo < ApplicationRecord

# 封面图
mount_uploader :image, PhotoUploader
after_commit :remove_image!, on: :destroy
end
2 changes: 1 addition & 1 deletion app/uploaders/photo_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class PhotoUploader < BaseUploader
def filename
if super.present?
@name ||= SecureRandom.uuid
"#{Time.now.year}/#{@name}.#{file.extension.downcase}"
"#{model.user&.login || Time.now.year}/#{@name}.#{file.extension.downcase}"
end
end
end
2 changes: 1 addition & 1 deletion test/controllers/photos_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
post photos_path, params: { file: file }
assert_equal 200, response.status
assert_equal true, response.parsed_body["ok"]
assert_match Regexp.new("/uploads/photo/#{Date.today.year}/[a-zA-Z0-9\\-]+.png!large"), response.parsed_body["url"]
assert_match Regexp.new("/uploads/photo/#{user.login}/[a-zA-Z0-9\\-]+.png!large"), response.parsed_body["url"]
end

it "POST /photos failure for blank data" do
Expand Down

0 comments on commit 1cffe09

Please sign in to comment.