Skip to content

Instantly share code, notes, and snippets.

@wonderflow
Created September 24, 2014 08:50
Show Gist options
  • Save wonderflow/29b286948ba2e0cde6f7 to your computer and use it in GitHub Desktop.
Save wonderflow/29b286948ba2e0cde6f7 to your computer and use it in GitHub Desktop.
Check asset encoding for valid UTF-8
# 其中包括文件名的非法编码检测以及文件内部非法字符的检测
# 检查特定格式的文件后缀,如css,js可以修改如下:
# Dir.glob(dir_files + "/**.{js,css}").each do |file|.{js,css}
dirstr = "/root/ZTC/"
dir = Dir.new(dirstr)
dir.each do |path|
next if path == "." || path == ".."
dir_path = dirstr+path
if File.exists?(dir_path)
dir_files = File.join(dir_path, "**")
Dir.glob(dir_files + "/*").each do |file|
next if file == dir_files+"/." || path == dir_files+"/.."
unless file.valid_encoding?
puts "#{file} name is valid."
end
# make sure we're not trying to process a directory
unless File.directory?(file)
# read the file and check its encoding
data = File.read(file)
unless data.valid_encoding?
puts "#{ file } does not have valid encoding!"
end
end
end # end Dir.glob
end #end File.exists
end # end paths.each
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment