Rails paperclip gem - 从私人文件夹获取文件 [英] Rails paperclip gem - Get file from private folder

查看:159
本文介绍了Rails paperclip gem - 从私人文件夹获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用回形针将文件上传到我的服务器。
如果我没有指定路径,回形针将文件保存到公用文件夹,然后我可以通过访问<%= @ user.file.url%>
但是如果我指定非公用文件夹的路径,显然不可能从视图获取文件。

I'm using paperclip to upload files to my server. If I don't specify the path, paperclip saves the file to a public folder, and then I can download it by accessing <%= @user.file.url %> in the view. But if I specify the path to a non-public folder, it's not possible getting the file from the view, obviously.

我想知道一些从网络和ruby脚本下载保存的文件在私人文件夹中的方法。

I would like to know some way to download the saved files in a private folder, from the web and from a ruby script.

推荐答案

我们需要的第一件事要添加路由到routes.rb访问文件。

The first thing we need to do is add a route to routes.rb for accessing the files.

编辑routes.rb并添加:member参数以粗体显示:

Edit routes.rb and add the :member parameter in bold:

资源:users,:member => {:avatars =>:get}

例如,我们可以发出一个这样的URL:

Now to get the avatar for user 7, for example, we can issue a URL like this:

 localhost:3000/users/7/avatars

...,请求将路由到头像 用户的操作控制器(复数,因为用户可能有多种风格的头像)。

… and the request will be routed to the avatars action in the users controller (plural since a user might have more than one style of avatar).

所以现在我们来吧,实现化身方法,并添加一些代码来将文件下载到客户端。执行此操作的方法是使用 ActionController :: Streaming :: send_file 。很简单我们只需要将文件的路径传递给send_file以及客户端使用的MIME内容类型,作为决定如何显示文件的线索,就是这样!让我们对这些值进行硬编码,以便更好地理解(更新您的机器的路径):

So now let’s go right ahead and implement the avatars method and add some code to download a file to the client. The way to do that is to use ActionController::Streaming::send_file. It’s simple enough; we just need to pass the file’s path to send_file as well as the MIME content type which the client uses as a clue for deciding how to display the file, and that’s it! Let’s hard code these values for the better understanding (update the path here for your machine):

    class UsersController < ApplicationController
      def avatars
       send_file '/path/to/non-public/system/avatars/7/original/mickey-mouse.jpg',
       :type => 'image/jpeg'
      end
    end

现在,如果输入 localhost:3000 / users / 7 / avatars 到您的浏览器中,您应该可以看到米奇图像。

Now if you type localhost:3000/users/7/avatars into your browser you should see the mickey image.

在化身方法中的路径,我们显然需要能够处理任何用户记录的任何头像文件附件的请求。要做到这一点,请配置纸夹,并告诉它文件现在存储在文件系统上,以及我们配置了我们的routes.rb文件使用的URL。

Instead of hard coding the path in the avatars method, we obviously need to be able to handle requests for any avatar file attachment for any user record. To do this, configure Paperclip and tell it where the files are now stored on the file system, and which URL we have configured our routes.rb file to use.

为此,我们需要在我们的用户模型(user.rb)中调用has_attached_file添加几个参数,

To do this, we need to add a couple of parameters to our call to has_attached_file in our User model (user.rb),

    has_attached_file :avatar,
    :styles => { :thumb => "75x75>", :small => "150x150>" },
    :path => 
    ':rails_root/non-public/system/:attachment/:id/:style/:basename.:extension',
    :url => '/:class/:id/:attachment' 

但是现在我们可以将UserController中的代码推广到处理任何用户,如下所示:

But now we can generalize our code in UserController to handle any user, like this:

    def avatars
      user = User.find(params[:id])
      send_file user.avatar.path, :type => user.avatar_content_type
    end         

现在我们可以测试 localhost:3000 / users / 7 /头像再次确定我们没有破坏任何东西。

Now we can test localhost:3000/users/7/avatars again to be sure that we haven’t broken anything.

干杯!

这篇关于Rails paperclip gem - 从私人文件夹获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆