我正在尝试使用carrierwave gem将用户个人资料图片从我的应用程序上传到集中式应用程序。在参数中,头像已通过但未更新。
这是我的应用日志:
Started PATCH "/api/v1/users/47" for ::1 at 2018-07-01 13:45:14 +0530
Processing by Api::V1::UsersController#update as JSON
Parameters: {"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007f7c7cda6e20 @tempfile=#<Tempfile:/tmp/RackMultipart20180701-10347-otl55f.jpg>, @original_filename="leotolstoy1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n">, "first_name"=>"Mayuresh"}, "id"=>"47"}
和集中的应用程序日志:
Started POST "/api/v1/users/update" for 127.0.0.1 at 2018-07-01 13:45:15 +0530
Processing by Api::V1::UsersController#update as */*
Parameters: {"first_name"=>"Mayuresh", "last_name"=>nil, "avatar"=>{"tempfile"=>"#<File:0x00007f7c7c6ba468>", "original_filename"=>"leotolstoy1.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n"}, "user"=>{"first_name"=>"Mayuresh", "last_name"=>nil, "avatar"=>{"tempfile"=>"#<File:0x00007f7c7c6ba468>", "original_filename"=>"leotolstoy1.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n"}}}
Application Load (0.4ms) SELECT "oauth_applications".* FROM "oauth_applications" WHERE "oauth_applications"."secret" = $1 ORDER BY "oauth_applications"."id" ASC LIMIT $2 [["secret", "<secret_token>"], ["LIMIT", 1]]
AccessToken Load (0.4ms) SELECT "oauth_access_tokens".* FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."active" = $1 AND "oauth_access_tokens"."token" = $2 ORDER BY "oauth_access_tokens"."id" ASC LIMIT $3 [["active", true], ["token", "<access_token>"], ["LIMIT", 1]]
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(1.8ms) BEGIN
User Exists (1.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) AND "users"."type" = $3 LIMIT $4 [["email", "<email>"], ["id", 1], ["type", "User"], ["LIMIT", 1]]
SQL (0.4ms) UPDATE "users" SET "first_name" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["first_name", "Mayuresh"], ["updated_at", 2018-07-01 08:15:15 UTC], ["id", 1]]
(12.7ms) COMMIT
如您所见,只有名字正在更新而没有头像。
集中式应用程序中的头像上传器:
class AvatarUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
'tmp/upload/'
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_whitelist
%w(jpg jpeg png)
end
def content_type_whitelist
/image\//
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
即使我试图从集中式应用程序的Rails控制台进行更新,也不会更新。
2.3.4 :049 > user.update avatar: 'https://static1.squarespace.com/static/586af0132994caa37cca0067/5876e27f9f74561d8c5ce4df/5876e296c534a514869fe56e/1484187414154/Tree+On+The+Bank.jpg'
(0.7ms) BEGIN
User Exists (0.8ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) AND "users"."type" = $3 LIMIT $4 [["email", "<email>"], ["id", 1], ["type", "User"], ["LIMIT", 1]]
SQL (0.4ms) UPDATE "users" SET "updated_at" = $1, "avatar" = $2 WHERE "users"."id" = $3 [["updated_at", 2018-07-01 07:39:16 UTC], ["avatar", "abc.jpg"], ["id", 1]]
(13.4ms) COMMIT
=> true
无法弄清这里出了什么问题。
答案 0 :(得分:0)
问题在于,传递给集中式应用程序的文件对象不是原始文件对象,因为按照代码,我将参数转换为json,然后通过法拉第进行连接以连接到集中式应用程序。
我所做的是建立一个单独的法拉第连接,用于上传文件和传递的参数,因为它在有效负载中如下所示:
conn = Faraday.new url: url do |f|
f.request :multipart
f.request :url_encoded
f.adapter Faraday.default_adapter
end
payload = { avatar: Faraday::UploadIO.new(update_params[:avatar], update_params[:avatar].content_type, update_params[:avatar].original_filename) }
conn.put '/api/v1/users/update', payload
现在我的应用程序参数:
Started PATCH "/api/v1/users/47" for ::1 at 2018-07-01 13:45:14 +0530
Processing by Api::V1::UsersController#update as JSON
Parameters: {"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007f7c7cda6e20 @tempfile=#<Tempfile:/tmp/RackMultipart20180701-10347-otl55f.jpg>, @original_filename="leotolstoy1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n">, "first_name"=>"Mayuresh"}, "id"=>"47"}
和集中的应用程序参数:
Started PUT "/api/v1/users/update" for 127.0.0.1 at 2018-07-05 02:12:04 +0530
Processing by Api::V1::UsersController#update as */*
Parameters: {"avatar"=>#<ActionDispatch::Http::UploadedFile:0x0000000775e1d8 @tempfile=#<Tempfile:/tmp/RackMultipart20180705-23054-1cbcry3.png>, @original_filename="nio.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"leotolstoy1.jpg\"\r\nContent-Length: 272286\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: binary\r\n">}
如您所见,头像参数是实际的ActionDispatch
对象,这就是图像正确上传的原因。
对于远程URL,我得到了解决方案here。它将完成如下操作:
2.3.4 :049 > user.update remote_avatar_url: 'https://static1.squarespace.com/static/586af0132994caa37cca0067/5876e27f9f74561d8c5ce4df/5876e296c534a514869fe56e/1484187414154/Tree+On+The+Bank.jpg'