-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of mdn#3984
- Loading branch information
Showing
8 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const readChunk = require("read-chunk"); | ||
const FileType = require("file-type"); | ||
|
||
const { ROOTS, VALID_FILE_TYPES } = require("./constants"); | ||
const { memoize, slugToFolder } = require("./utils"); | ||
|
||
async function isFile(filePath) { | ||
if (fs.statSync(filePath).isDirectory()) { | ||
return false; | ||
} | ||
|
||
const buffer = readChunk.sync(filePath, 0, 12); | ||
if (buffer.length === 0) { | ||
return false; | ||
} | ||
const type = await FileType.fromBuffer(buffer); | ||
// If the file valid, this comes back | ||
// as, for example: { ext: 'ttf', mime: 'font/ttf' } | ||
if (!type || !VALID_FILE_TYPES.has(type.ext)) { | ||
return false; | ||
} | ||
if (!VALID_FILE_TYPES.get(type.ext).includes(type.mime)) { | ||
console.warn(`${filePath} (${type}) not a valid file type`); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function urlToFilePath(url) { | ||
const [, locale, , ...slugParts] = decodeURI(url).split("/"); | ||
return path.join(locale.toLowerCase(), slugToFolder(slugParts.join("/"))); | ||
} | ||
|
||
const find = memoize((relativePath) => { | ||
return ROOTS.map((root) => path.join(root, relativePath)).find( | ||
(filePath) => fs.existsSync(filePath) && isFile(filePath) | ||
); | ||
}); | ||
|
||
function findByURL(url) { | ||
return find(urlToFilePath(url)); | ||
} | ||
|
||
module.exports = { | ||
findByURL, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters