Guest User

Untitled

a guest
Jul 27th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs')
  2. const { promisify } = require('util')
  3. const { pipeline } = require('stream')
  4. const got = require('got')
  5.  
  6. ;(async () => {
  7.     const url = 'https://www.onlygfx.com/wp-content/uploads/2016/04/tree-2.png'
  8.     const readStream = got.stream(url)
  9.     const writeStream = fs.createWriteStream('./image.png')
  10.  
  11.     readStream.once('data', checkType)
  12.  
  13.     pipeline(
  14.         readStream,
  15.         writeStream,
  16.         err => {
  17.             console.error(err)
  18.             readStream.off('data', checkType)
  19.         }
  20.     )
  21.  
  22.     function checkType(chunk) {
  23.         const slice = chunk.slice(1, 4).toString('ascii')
  24.  
  25.         console.log('slice', slice)
  26.  
  27.         if (slice !== 'PNG') {
  28.             const err = new Error('не PNG')
  29.             return this.destroy(err)
  30.         }
  31.     }
  32. })()
  33.  
Advertisement
Add Comment
Please, Sign In to add comment