简体   繁体   English

Ruby,限制每个请求使用查找模块返回的文件数

[英]Ruby, limit the number of files returned with Find module per request

I have a controller that calls a find_photos method, passing it a query string (name of file) 我有一个控制器,它调用find_photos方法,并向其传递查询字符串(文件名)

class BrandingPhoto < ActiveRecord::Base

  def self.find_photos(query)
    require "find"

    found_photos = []

    Find.find("u_photos/photo_browse/photos/") do |img_path|
        # break off just the filename from full path
        img = img_path.split('/').last

        if query.blank? || query.empty?
        # if query is blank, they submitted the form with browse all- return all photos
            found_photos << img
        else
        # otherwise see if the file includes their query and return it
            found_photos << img if img.include?(query)
        end
    end

    found_photos.empty? ? "no results found" : found_photos
  end
end

This is just searching a directory full of photos- there is no table backing this. 这只是搜索一个充满照片的目录-没有支持此的表。

Ideally what I would like is to be able to limit the number of results returned by find_photos to around 10-15, then fetch the next 10-15 results as needed. 理想情况下,我希望能够将find_photos返回的结果数限制为10-15个左右,然后根据需要获取下一个10-15个结果。

I was thinking that the code to do this might involve looping through 10 times and grabbing those files- store the last filename in a variable or as a parameter, and then send that variable back to the method, telling it to continue the search from that filename. 我当时认为执行此操作的代码可能涉及循环10次并抓取这些文件-将最后一个文件名存储在变量中或作为参数,然后将该变量发送回方法,并告诉其从该位置继续搜索文档名称。

This assumes that the files are looped through in the same order everytime, and that there is no simpler way to accomplish this. 这假设每次都以相同的顺序遍历文件,并且没有简单的方法可以完成此操作。

If there are any suggestions, I'd love to hear them/see some examples of how you'd accomplish this. 如果有任何建议,我很乐意听取他们的意见/查看一些有关如何实现此建议的示例。

Thank you. 谢谢。

The first thing that comes to mind for this problem is to cut the array down after you come out of the loop. 解决此问题的第一件事是在退出循环后将数组缩减。 This wouldn't work well with a ton of files though A different solution might be to add a break for the size of the array viz. 尽管很多解决方案可能是增加数组viz的大小,但这种方法无法处理大量文件。 break if found_photos.length > 10 inside the loop break if found_photos.length > 10在循环内则break if found_photos.length > 10

It's not too hard to do what you want, but you need to consider how you'll handle entries that are added or removed in-between page loads, filenames with UTF-8 or Unicode characters, and embedded/parent directories. 做您想要的事情不是很困难,但是您需要考虑如何处理在页面加载,具有UTF-8或Unicode字符的文件名以及嵌入式/父目录之间添加或删除的条目。

This is old-school code for the basis for what you're talking about: 这是您正在谈论的基础的老式代码:

require 'erb'
require 'sinatra'

get '/list_photos' do

  dir    = params[ :dir    ]
  offset = params[ :offset ].to_i
  num    = params[ :num    ].to_i

  files = Dir.entries(dir).reject{ |fn| fn[/^\./] || File.directory?(File.join(dir, fn)) }
  total_files = files.size

  prev_a = next_a = ''

  if (offset > 0)
    prev_a = "<a href='/list_photos?dir=#{ dir }&num=#{ num }&offset=#{ [ 0, offset - num ].max }'>&lt;&lt; Previous</a>"
  end

  if (offset < total_files)
    next_a = "<a href='/list_photos?dir=#{ dir }&num=#{ num }&offset=#{ [ total_files, offset + num ].min }'>Next &gt;&gt;</a>"
  end

  files_to_display = files[offset, num]

  template = ERB.new <<EOF
<html>
  <head></head>
  <body>
    <table>
    <% files_to_display.each do |f| %>
      <tr><td><%= f %></td></tr>
    <% end %>
    </table>
    <%= prev_a %> | <%= total_files %> files | <%= next_a %>
  </body>
</html>
EOF

  content_type 'text/html'
  template.result(binding)

end

It's a little Sinatra server, so save it as test.rb and run from the command-line using: 这是一台小型Sinatra服务器,因此将其另存为test.rb并使用以下命令从命令行运行:

ruby test.rb

In a browser connect to the running Sinatra server using a URL like: 在浏览器中,使用类似以下网址的URL连接到正在运行的Sinatra服务器:

http://hostname:4567/list_photos?dir=/path/to/image/files&num=10&offset=0

I'm using Sinatra for convenience, but the guts of the routine is the basis for what you want. 为了方便起见,我正在使用Sinatra,但是例程的勇气是您想要的基础。 How to convert it into Rails terms is left as an exercise for the reader. 读者将练习如何将其转换为Rails术语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM