Skip to content

Commit

Permalink
📦 add file-type package and check for jpg, png integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielstuff committed Feb 3, 2020
1 parent f887397 commit be3edf3
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 15 deletions.
89 changes: 86 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"command-exists": "^1.2.8",
"commander": "^3.0.1",
"express": "^4.17.1",
"file-type": "^14.0.0",
"finalhandler": "^1.1.2",
"ip": "^1.1.5",
"mime-types": "^2.1.24",
Expand Down
54 changes: 42 additions & 12 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const pathHelper = require('path')
//const mediainfo = require('mediainfo-q')
const ffprobe = require('node-ffprobe')
const ffprobeInstaller = require('@ffprobe-installer/ffprobe')
const FileType = require('file-type')
const fs = require('fs')
ffprobe.FFPROBE_PATH = ffprobeInstaller.path
ffprobe.SYNC = true
const untildify = require('untildify')
Expand All @@ -15,18 +17,16 @@ var spacebro = require('./spacebro')
let watcher
let cb = null

const checkIntegrityAsync = async path => {
const checkMediaIntegrityAsync = async path => {
try {
const result = await ffprobe(path)
// console.log(result)
if (isVideo(path) && result.streams && result.streams.length > 0 && result.format.duration && Number(result.format.duration) > 0) {
return true
} else if (isImage(path)) {
return result.format.nb_streams > 0
} else if (isSound(path) && result.streams && result.streams.length > 0 && result.format.duration && Number(result.format.duration) > 0) {
return true
} else {
console.warn('looks like it is not a media or it is corrupted')
console.warn('looks like it is not a video or sound media or it is corrupted.')
return false
}
} catch (err) {
Expand All @@ -35,6 +35,18 @@ const checkIntegrityAsync = async path => {
}
}

const checkImageIntegrity = async path => {
const stream = fs.createReadStream(path)
try {
const {ext, mime} = await FileType.fromStream(stream)
return {ext, mime}
} catch (err) {
return null
}
}



var log = console.log.bind(console)

const update = folder => {
Expand Down Expand Up @@ -72,11 +84,20 @@ const init = (settings, callback) => {
.on('add', async path => {
log(`File ${path} has been added`)
if (shouldCheckIntegrity) {
let integrity = await checkIntegrityAsync(path)
if (integrity === true) {
spacebro.send(path)
if (!isImage(path)) {
let integrity = await checkMediaIntegrityAsync(path)
if (integrity === true) {
spacebro.send(path)
} else {
console.error(`file ${path} is not ok`)
}
} else {
console.error('file is not ok')
let res = await checkImageIntegrity(path)
if (res) {
spacebro.send(path)
} else {
console.error(`file ${path} is not ok`)
}
}
} else {
console.warn('No integrity file check')
Expand All @@ -86,11 +107,20 @@ const init = (settings, callback) => {
.on('change', async path => {
log(`File ${path} has been changed`)
if (shouldCheckIntegrity) {
let integrity = await checkIntegrityAsync(path)
if (integrity === true) {
spacebro.send(path)
if (!isImage(path)) {
let integrity = await checkMediaIntegrityAsync(path)
if (integrity === true) {
spacebro.send(path)
} else {
console.error(`file ${path} is not ok`)
}
} else {
console.error('file is not ok')
let res = await checkImageIntegrity(path)
if (res) {
spacebro.send(path)
} else {
console.error(`file ${path} is not ok`)
}
}
} else {
console.warn('No integrity file check')
Expand Down

0 comments on commit be3edf3

Please sign in to comment.