[英]Rails file download not working in JSON API with devise token authentication
在破坏了一个工作日之后,我迫切需要帮助。
我有一个Rails 4.1.0应用程序,该应用程序还公开了JSON API。 在API中,我将devise与token_authentication一起使用。 有一个带有回形针附件的图像模型。 尝试下载图像时,邮递员插件显示状态为200的成功。但是未下载图像。
我试过同时使用send_file和send_data方法。 对于这两个服务器,显示两个日志条目。 还在调试时,我看到ApiController的'authenticate_user_from_token'方法执行了两次,第二次缺少'X-Auth-Token标头'(这很明显,因为我没有发送第二个请求)。 这将导致401未经授权的错误,并且不会下载文件(请参阅底部的日志)。 我不确定为什么send_file或send_data方法导致对服务器的第二个请求。
这是我的代码。
controllers / api / v1 / images_controller.rb
class Api::V1::ImagesController < Api::V1::ApiController
def download
@image = Image.find(params[:id])
# Tried send_file
send_file @image.pic.path(:original)
# Tried send_data
data = File.read(@image.pic.path(:original))
send_data data, filename: @image.pic.original_filename, type: @image.pic.content_type
end
end
controllers / api / v1 / api_controller.rb
class Api::V1::ApiController < ApplicationController
respond_to :json
before_filter :authenticate_user_from_token!
before_filter :authenticate_user!
protect_from_forgery with: :null_session
private
def authenticate_user_from_token!
user_email = params[:email].presence
user = user_email && User.find_by_email(user_email)
auth_token = request.headers["X-Auth-Token"]
if user && Devise.secure_compare(user.authentication_token, auth_token)
sign_in user, store: false
end
end
end
控制器/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
型号/ image.rb
class Image < ActiveRecord::Base
PAPERCLIP_ROOT = "#{Rails.root}/storage"
has_attached_file :pic, :styles => {:original => "720x720", :medium => "120x120", :thumbnail => "40x40"}
end
config / routes.rb
Rails.application.routes.draw do
#API
namespace :api , defaults: {format: :json} do
namespace :v1 do
resources :images do
member do
get 'download'
end
end
devise_scope :user do
post "/sign_in", :to => 'sessions#create'
post "/sign_out", :to => 'sessions#destroy'
end
end
end
end
server_log
Started GET "/api/v1/images/14/download?email=user@example.com" for 127.0.0.1 at 2014-09-21 13:57:25 +0530
ActiveRecord::SchemaMigration Load (0.7ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by Api::V1::ImagesController#download as */*
Parameters: {"email"=>"user@example.com", "id"=>"14", "image"=>{}}
.
.
Sent file /Users/rajveershekhawat/workspace/dine_connect/storage/images/pics/000/000/014/original/images.jpg (0.5ms)
Completed 200 OK in 14067ms (ActiveRecord: 13.5ms)
Started GET "/api/v1/images/14/download?email=user@example.com" for 127.0.0.1 at 2014-09-21 13:57:40 +0530
Processing by Api::V1::ImagesController#download as HTML
Parameters: {"email"=>"user@example.com", "id"=>"14"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'user@example.com' LIMIT 1
Completed 401 Unauthorized in 3462ms
请帮忙。 非常感谢。
更新:
有人可以告诉我为什么有两个请求日志吗?
我们甚至可以像普通下载一样通过Base64编码通过json api下载图像吗?
尝试使用send_data
而不是send_file
吗?
您可能需要对回形针文件路径进行读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.