user9032546
user9032546

Reputation:

Download image from url by nestjs

I want to download user profile picture from telegram account and store it in local storage with nestjs framework.

Upvotes: 5

Views: 16594

Answers (1)

Aleksandr Yatsenko
Aleksandr Yatsenko

Reputation: 869

@Controller()
export class Controller {
    constructor(
        private readonly httpService: HttpService,
    ) {
    }

    @Get()
    async downloadImage(@Res() res) {
        const writer = fs.createWriteStream('./image.png');

        const response = await this.httpService.axiosRef({
            url: 'https://example.com/image.png',
            method: 'GET',
            responseType: 'stream',
        });

        response.data.pipe(writer);

        return new Promise((resolve, reject) => {
            writer.on('finish', resolve);
            writer.on('error', reject);
        });
    }
}

Upvotes: 20

Related Questions