MCPcopy Create free account
hub / github.com/github/docs / checkFile

Function checkFile

script/validate-asset-images.js:97–136  ·  view source on GitHub ↗
(filePath)

Source from the content-addressed store, hash-verified

95}
96
97async function checkFile(filePath) {
98 const ext = path.extname(filePath)
99
100 const { size } = await fs.stat(filePath)
101 if (!size) {
102 return [CRITICAL, filePath, 'file is 0 bytes']
103 }
104
105 if (ext === '.svg') {
106 // Can't use `fileTypeFromFile` so have to check "manually"
107 const content = await fs.readFile(filePath, 'utf-8')
108 if (!content.trim()) {
109 return [CRITICAL, filePath, 'file is empty']
110 }
111 if (!isSVG(content)) {
112 return [CRITICAL, filePath, 'not a valid SVG file']
113 }
114 try {
115 checkSVGContent(content)
116 } catch (error) {
117 return [CRITICAL, filePath, error.message]
118 }
119 } else if (EXPECT[ext]) {
120 const fileType = await fileTypeFromFile(filePath)
121 if (!fileType) {
122 return [CRITICAL, filePath, "can't extract file type"]
123 }
124 const { mime } = fileType
125 const expect = EXPECT[ext]
126 if (expect !== mime) {
127 return [CRITICAL, filePath, `Expected mime type '${expect}' but was '${mime}'`]
128 }
129 } else if (!ext) {
130 return [WARNING, filePath, 'Has no file extension']
131 } else {
132 return [WARNING, filePath, `Don't know how to validate '${ext}'`]
133 }
134
135 // All is well. Nothing to complain about.
136}
137
138function checkSVGContent(content) {
139 const $ = cheerio.load(content)

Callers

nothing calls this directly

Calls 1

checkSVGContentFunction · 0.85

Tested by

no test coverage detected