2022-06-24 09:31:32 -05:00
|
|
|
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 {
|
2022-06-27 04:53:12 -05:00
|
|
|
await processImage({ path: tmpPath, destination: destPath, newSize: size })
|
2022-06-24 09:31:32 -05:00
|
|
|
} catch (err) {
|
|
|
|
await remove(tmpPath)
|
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
2023-06-19 02:56:12 -05:00
|
|
|
|
|
|
|
return destPath
|
2022-06-24 09:31:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = downloadImage
|
|
|
|
|
|
|
|
export {
|
|
|
|
downloadImage
|
|
|
|
}
|