Skip to content

Instantly share code, notes, and snippets.

@xnuk
Created February 17, 2019 16:36
Show Gist options
  • Save xnuk/375aa776b353ebd54e86998a3807f8a0 to your computer and use it in GitHub Desktop.
Save xnuk/375aa776b353ebd54e86998a3807f8a0 to your computer and use it in GitHub Desktop.
const fs = require('fs')
const file = path => new Promise((res, err) => {
fs.open(path, 'r', (e, fd) => {
if(e != null) return err(e)
const b = Buffer.alloc(24)
fs.read(fd, b, 0, 24, 0, (e, _, buf) => {
fs.close(fd, () => {})
if(e == null) res(buf)
else err(e)
})
})
})
const getSize = buf => {
const b = buf.slice(0, 16).equals(Buffer.from(
[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0x0d, 0x49, 0x48, 0x44, 0x52]
))
if(!b) return null
return {width: buf.readUInt32BE(16), height: buf.readUInt32BE(20)}
}
const getFileSize = path => file(path).then(getSize)
module.exports = {getSize, getFileSize}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment