aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/worker/workers/image-downloader.ts
blob: 4b32f723ef2bc67434f7183a35662c4e1f42189e (plain) (blame)
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
import { remove } from 'fs-extra'
import { join } from 'path'
import { processImage } from '@server/helpers/image-utils'
import { doRequestAndSaveToFile } from '@server/helpers/requests'
import { CONFIG } from '@server/initializers/config'

async function downloadImage (options: {
  url: string
  destDir: string
  destName: string
  size: { width: number, height: number }
}) {
  const { url, destDir, destName, size } = options

  const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName)
  await doRequestAndSaveToFile(url, tmpPath)

  const destPath = join(destDir, destName)

  try {
    await processImage({ path: tmpPath, destination: destPath, newSize: size })
  } catch (err) {
    await remove(tmpPath)

    throw err
  }
}

module.exports = downloadImage

export {
  downloadImage
}