在 Rails 应用程序中查找未使用的图像?
我熟悉 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很大程度上取决于使用图像的代码。总是有可能计算文件名(通过连接两个值或字符串替换等),因此简单地按文件名进行 grep 不一定足够。
你可以尝试运行 wget (如果你有 Linux 机器,可能已经安装了,否则 http:// users.ugent.be/~bpuype/wget/ )来镜像您的整个网站。如果可以的话,请在同一台计算机或网络上执行此操作,它将抓取您的整个网站并抓取所有图像。
完成此操作后,您将获得网站层次结构的第二个副本,其中包含所有活跃的图像通过抓取您的网站可访问的任何页面链接到。然后,您可以备份源图像目录,并将其替换为 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
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.
查找未使用的图像应该比 CSS 更容易。
只需使用 glob 查找 *.jpg *.png *gif,将这些文件名放入字典或数组中,然后在 html、css、js 文件中查找这些文件名,如果找到则删除文件名,您将获得未使用的列表,然后将这些图像移动到另一个文件夹具有相同的目录结构(以防万一,这将有助于恢复)
基本上就像这样,当然对于加密/编码/混淆的文件名将不起作用。
PS:我使用了
fileutils
,因为正常的makedirs
和mv
在我的windows版本的ruby中不起作用而且我不擅长ruby,所以使用前请仔细检查。
这是我在 Windows 中示例 Rails 文件夹的根文件夹中运行的示例结果
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.
PS: I used
fileutils
, because normalmakedirs
andmv
are not works in my windows version of rubyAnd 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
如果您的图像 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:
/attic
/images
directory (or what your asset directory is called).htaccess
file (if you're on Apache of course) that, using the-f
flag, redirects all requests to nonexistent image files to a script/attic
into the/images
directory and displays itAfter 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.
如果您的文件管理器支持,请尝试按文件的“上次访问”日期对图像目录进行排序。长时间未访问的文件很可能不再使用。
同样,您还可以过滤或 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.