Add code from express-unpkg repo
This commit is contained in:
35
server/middleware/FileUtils.js
Normal file
35
server/middleware/FileUtils.js
Normal file
@ -0,0 +1,35 @@
|
||||
const fs = require('fs')
|
||||
const mime = require('mime')
|
||||
|
||||
const TextFiles = /\/?(LICENSE|README|CHANGES|AUTHORS|Makefile|\.[a-z]*rc|\.git[a-z]*|\.[a-z]*ignore)$/i
|
||||
|
||||
const getContentType = (file) =>
|
||||
TextFiles.test(file) ? 'text/plain' : mime.lookup(file)
|
||||
|
||||
const getStats = (file) =>
|
||||
new Promise((resolve, reject) => {
|
||||
fs.lstat(file, (error, stats) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve(stats)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const getFileType = (stats) => {
|
||||
if (stats.isFile()) return 'file'
|
||||
if (stats.isDirectory()) return 'directory'
|
||||
if (stats.isBlockDevice()) return 'blockDevice'
|
||||
if (stats.isCharacterDevice()) return 'characterDevice'
|
||||
if (stats.isSymbolicLink()) return 'symlink'
|
||||
if (stats.isSocket()) return 'socket'
|
||||
if (stats.isFIFO()) return 'fifo'
|
||||
return 'unknown'
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getContentType,
|
||||
getStats,
|
||||
getFileType
|
||||
}
|
Reference in New Issue
Block a user