在 Rails 应用程序中查找未使用的图像?

发布于 2024-08-15 07:27:03 字数 214 浏览 2 评论 0原文

我熟悉 Deadweight 等工具来查找 Rails 应用程序中未使用的 CSS,但是是否存在任何东西对于图像?我正在参与一个项目,其中包含与各种设计师合作的大量资产目录,并且我正在努力减少该项目中的负担。将资产转移到我们的 CDN 时尤其痛苦。

有什么想法吗?

I'm familiar with tools like Deadweight for finding CSS not in use in your Rails app, but does anything exist for images? I'm sitting in a project with a massive directory of assets from working with a variety of designers and I'm trying to trim the fat in this project. It's especially a pain when moving assets to our CDN.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

々眼睛长脚气 2024-08-22 07:27:03

这很大程度上取决于使用图像的代码。总是有可能计算文件名(通过连接两个值或字符串替换等),因此简单地按文件名进行 grep 不一定足够。

你可以尝试运行 wget (如果你有 Linux 机器,可能已经安装了,否则 http:// users.ugent.be/~bpuype/wget/ )来镜像您的整个网站。如果可以的话,请在同一台计算机或网络上执行此操作,它将抓取您的整个网站并抓取所有图像。

# mirror mysite.com accepting only jpg, png and gif files
wget -A jpg,png,gif --mirror www.mysite.com

完成此操作后,您将获得网站层次结构的第二个副本,其中包含所有活跃的图像通过抓取您的网站可访问的任何页面链接到。然后,您可以备份源图像目录,并将其替换为 wget 的副本。接下来,监视日志文件中是否有与 gif/jpg/png 文件相关的 404。希望有帮助。

It depends greatly on the code using the images. It's always possible that a filename is computed (by concatenating two values or string substitution etc) so a simply grepping by filename isn't necessarily enough.

You could try running wget (probably already installed if you've got a linux machine, otherwise http://users.ugent.be/~bpuype/wget/ ) to mirror your whole site. Do this on the same machine or network if you can, it'll crawl your whole site and grab all the images

# mirror mysite.com accepting only jpg, png and gif files
wget -A jpg,png,gif --mirror www.mysite.com

Once you've done that, you're going to have a second copy of your site's hierarchy containing any images that are actively linked to by any page reachable by crawling your site. You can then backup your source image directory, and replace it with wget's copy. Next, monitor your log files for 404's pertaining to gif/jpg/png files. Hope that helps.

糖果控 2024-08-22 07:27:03

查找未使用的图像应该比 CSS 更容易。

只需使用 glob 查找 *.jpg *.png *gif,将这些文件名放入字典或数组中,然后在 html、css、js 文件中查找这些文件名,如果找到则删除文件名,您将获得未使用的列表,然后将这些图像移动到另一个文件夹具有相同的目录结构(以防万一,这将有助于恢复)

基本上就像这样,当然对于加密/编码/混淆的文件名将不起作用。

require "fileutils"

img=Dir.glob("**/*.jpg")+Dir.glob("**/*.png")+Dir.glob("**/*.gif")
data=Dir.glob("**/*.htm*")+Dir.glob("**/*.css")+Dir.glob("**/*.js")

puts img.length.to_s+" images found & "+data.length.to_s+" files found to search against"

content=""
data.each do |f|
    content+=File.open(f, 'r').read   
end

img.each do |m|
    if not content=~ Regexp.new("\\b"+File.basename(m)+"\\b")
        FileUtils.mkdir_p "../unused/"+File.dirname(m)
        FileUtils.mv m,"../unused/"+m
        puts "Image "+m+" moved to ../unused/"+File.dirname(m)+" folder"
    end
end

PS:我使用了fileutils,因为正常的makedirsmv在我的windows版本的ruby中不起作用

而且我不擅长ruby,所以使用前请仔细检查。

这是我在 Windows 中示例 Rails 文件夹的根文件夹中运行的示例结果

---\ruby>ruby img_coverage.rb
5 images found & 12 files found to search against
Image depot/public/images/test.jpg moved to ../unused/depot/public/images folder

Finding unsed images should be easier than CSS.

Just find *.jpg *.png *gif with glob, put those filenames to dictionary or array and find those filenames againt html, css, js files, remove filename if found and you will get unused list, and move those images to another folder with same directory structure (It will be good for restoring for just in case)

Basically like this, and of course for the file names that encrypted/encoded/obcuscated will not work.

require "fileutils"

img=Dir.glob("**/*.jpg")+Dir.glob("**/*.png")+Dir.glob("**/*.gif")
data=Dir.glob("**/*.htm*")+Dir.glob("**/*.css")+Dir.glob("**/*.js")

puts img.length.to_s+" images found & "+data.length.to_s+" files found to search against"

content=""
data.each do |f|
    content+=File.open(f, 'r').read   
end

img.each do |m|
    if not content=~ Regexp.new("\\b"+File.basename(m)+"\\b")
        FileUtils.mkdir_p "../unused/"+File.dirname(m)
        FileUtils.mv m,"../unused/"+m
        puts "Image "+m+" moved to ../unused/"+File.dirname(m)+" folder"
    end
end

PS: I used fileutils, because normal makedirs and mv are not works in my windows version of ruby

And I am not good at ruby, so please double check it before you use it.

Here is the sample results I ran in root folder of sample rails folder in my windows

---\ruby>ruby img_coverage.rb
5 images found & 12 files found to search against
Image depot/public/images/test.jpg moved to ../unused/depot/public/images folder
坠似风落 2024-08-22 07:27:03

如果您的图像 URL 通常来自许多计算/连接的字符串以及其他难以在源代码中以编程方式跟踪的内容,并且您的应用程序正在大量使用,您可以尝试像这样的软“蜜罐”方法:将

  • 所有资源移动到不同的目录,例如 /attic
  • 设置一个空的 /images 目录(或者您的资产目录的名称)
  • 设置一个 .htaccess 文件(当然,如果您使用的是 Apache),则使用 -f 标志将所有对不存在的图像文件的请求重定向到脚本
  • 脚本复制请求的文件/attic 进入 /images 目录并显示它
  • 对该图像的下一个请求将直接转到该图像,因为它现在已经存在

经过一段时间和足够的使用后,所有需要图像应该已经被复制到资产目录中。

当然,这是一种“软”方法,因为在此期间任何用户都无法打开/输入/使用对话框/情况(例如错误消息图标之类的内容)。但它会识别所有使用过的文件,无论它们是从哪里请求的,并且可能有助于整理出许多不需要的文件。

If your image URLs often come from many computed / concatenated strings and other stuff hard to track programmatically within your source code, and your application is in heavy use, you could try a soft "honeypot" approach like this:

  • Move all the assets to a different directory, e.g. /attic
  • Set up an empty /images directory (or what your asset directory is called)
  • Set up a .htaccess file (if you're on Apache of course) that, using the -f flag, redirects all requests to nonexistent image files to a script
  • The script copies the requested file from the /attic into the /images directory and displays it
  • The next request to that image will go directly to the image, because it exists now

After some time and sufficient usage, all needed images should have been copied to the assets directory.

It's a "soft" approach of course because a dialog / situation could have not been opened/entered/used by any user during that time (things like error message icons for example). But it will recognize all used files, no matter where they're requested from, and might help sort out much of the unneeded files.

〃温暖了心ぐ 2024-08-22 07:27:03

如果您的文件管理器支持,请尝试按文件的“上次访问”日期对图像目录进行排序。长时间未访问的文件很可能不再使用。

同样,您还可以过滤或 grep Web 服务器的日志,并列出过去几个月提供的图像文件。任何不在此列表中的图像都可能未使用。

If your file manager supports it, try sorting your images directory by the files' "last accessed" date. Files that haven't been accessed in a long time most likely aren't used any longer.

Along the same lines, you can also filter or grep through your web server's logs and make a list of the image files that it has served up in the last several months. Any images not in this list are likely unused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文