ruby-on-rails – Carrierwave gem安全文件路径无法读取文件错误

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Carrierwave gem安全文件路径无法读取文件错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在载波中遵循“安全上传”,这是有点混乱,因为我已经定制了文件路径和一切.当我尝试运行应用程序时,我收到’无法读取文件错误.

这是路线:

match "/uploads/tobereviewed/:user.:username.downcase/:basename.:extension",:controller => "photos",:action => "uploaded",via: :get

上传者的sotre_dir:

class SubmitUploader < CarrierWave::Uploader::Base

def store_dir
  "uploads/tobereviewed/#{model.user.username.downcase}"
end

carrierwave.rb初始化器:

CarrierWave.configure do |config|
      config.permissions = 0600
      config.directory_permissions = 0700
      config.root = Rails.root
    end

照片控制器:

def uploaded

        file = Submit.first

        send_file "#{Rails.root}/uploads/tobereviewed/#{file.user.username.downcase}/#{file.id}"
    end

完整的错误日志:

Started GET "/uploaded" for 127.0.0.1 at 2014-11-27 18:19:09 +0530
Processing by PhotosController#uploaded as HTML
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id",1]]
  Submit Load (2.6ms)  SELECT  "submits".* FROM "submits"  ORDER BY "submits"."id" ASC LIMIT 1
  User Load (2.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id",1]]
Sent file /home/pubudu/Projects/istockseller/uploads/tobereviewed/thpubs/36603935 (0.7ms)
Completed 500 Internal Server Error in 54ms

ActionController::MissingFile - Cannot read file /home/pubudu/Projects/istockseller/uploads/tobereviewed/thpubs/36603935:
  actionpack (4.2.0.beta4) lib/action_controller/Metal/data_streaming.rb:68:in `send_file'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/instrumentation.rb:49:in `block in send_file'
  activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0.beta4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/instrumentation.rb:47:in `send_file'
   () home/pubudu/Projects/istockseller/app/controllers/photos_controller.rb:24:in `uploaded'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.0.beta4) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/rendering.rb:10:in `process_action'
  actionpack (4.2.0.beta4) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:234:in `block in halting'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:169:in `block in halting'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:92:in `_run_callbacks'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0.beta4) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/rescue.rb:29:in `process_action'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.0.beta4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.0.beta4) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.0.beta4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.0.beta4) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.0.beta4) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.0.beta4) lib/action_controller/Metal.rb:195:in `dispatch'
  actionpack (4.2.0.beta4) lib/action_controller/Metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.0.beta4) lib/action_controller/Metal.rb:236:in `block in action'
  actionpack (4.2.0.beta4) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (4.2.0.beta4) lib/action_dispatch/routing/route_set.rb:42:in `serve'
  actionpack (4.2.0.beta4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.0.beta4) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.0.beta4) lib/action_dispatch/routing/route_set.rb:780:in `call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.6.0.beta) lib/rack/etag.rb:23:in `call'
  rack (1.6.0.beta) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.0.beta) lib/rack/head.rb:13:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/flash.rb:257:in `call'
  rack (1.6.0.beta) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.0.beta) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/cookies.rb:558:in `call'
  activerecord (4.2.0.beta4) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.0.beta4) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
  activerecord (4.2.0.beta4) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
  activesupport (4.2.0.beta4) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (1.1.0) lib/better_errors/middleware.rb:56:in `call'
  web-console (2.0.0.beta4) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
  web-console (2.0.0.beta4) lib/action_dispatch/debug_exceptions.rb:13:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.0.beta4) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.0.beta4) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.0.beta4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.0.beta4) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.0.beta4) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.0.beta4) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.0.beta) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.0.beta) lib/rack/runtime.rb:17:in `call'
  activesupport (4.2.0.beta4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.0.beta) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.0.beta4) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.0.beta) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.0.beta4) lib/rails/engine.rb:514:in `call'
  railties (4.2.0.beta4) lib/rails/application.rb:161:in `call'
  rack (1.6.0.beta) lib/rack/tempfile_reaper.rb:15:in `call'
  rack (1.6.0.beta) lib/rack/lint.rb:49:in `_call'
  rack (1.6.0.beta) lib/rack/lint.rb:37:in `call'
  rack (1.6.0.beta) lib/rack/showexceptions.rb:24:in `call'
  rack (1.6.0.beta) lib/rack/commonlogger.rb:33:in `call'
  rack (1.6.0.beta) lib/rack/chunked.rb:54:in `call'
  rack (1.6.0.beta) lib/rack/content_length.rb:15:in `call'
  rack (1.6.0.beta) lib/rack/handler/webrick.rb:89:in `service'
   () home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
   () home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
   () home/pubudu/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

127.0.0.1 - - [27/Nov/2014:18:19:09 +0530] "GET /uploaded HTTP/1.1" 500 - 0.0743

更新:

我设置正确的文件路径,如下所示:

send_file "#{file.filename.file}"

但是它给出了这个错误

Cannot read file #<CarrierWave::SanitizedFile:0x007f570c2b9e58>

我的路线是否正确?

解决方法

根据您的设置,CarrierWave应将文件保存在“/home/pubudu/Projects/istockseller/public/uploads/tobereviewed/thpubs/#{model.user.username.downcase}/somefile.ext”中.

所以,最好你的代码应该是

send_file "#{Rails.root}/public/#{file.image.url}"

假设您的上传列是图像.

原文链接:https://www.f2er.com/ruby/265937.html

猜你在找的Ruby相关文章