File size: 881 Bytes
74aacd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { useCallback, useEffect, useState } from 'react'

export default function useInputImage() {
  const [inputImage, setInputImage] = useState<File>()

  const fetchInputImage = useCallback(() => {
    const headers = new Headers()
    headers.append('pragma', 'no-cache')
    headers.append('cache-control', 'no-cache')

    fetch('/inputimage', { headers }).then(async res => {
      const filename = res.headers
        .get('content-disposition')
        ?.split('filename=')[1]
        .split(';')[0]

      const data = await res.blob()
      if (data && data.type.startsWith('image')) {
        const userInput = new File(
          [data],
          filename !== undefined ? filename : 'inputImage'
        )
        setInputImage(userInput)
      }
    })
  }, [setInputImage])

  useEffect(() => {
    fetchInputImage()
  }, [fetchInputImage])

  return inputImage
}