UNPKG

@hyperbytes/wappler-get-file-from-url-or-api

Version:

Wappler Download from URL

63 lines (49 loc) 2.37 kB
// JavaScript Document exports.APIFileDownloadAction = async function (options) { // retrieves a file via remote http request and saves the file to the local server options = this.parse(options); const { toSystemPath } = require('../../../lib/core/path'); const axios = require('axios'); // use axios instead of request const path = require("path"); const fs = require('fs'); let save_path = toSystemPath(this.parseRequired(this.parse(options.local_path), 'string', 'fs.exists: path is required.')); let remoteURL = this.parseRequired(options.file_url, 'string', 'parameter value is required.'); let custom_file_name = this.parse(options.custom_file_name); let custom_extension = this.parse(options.force_extn); if (custom_extension) { custom_extension = custom_extension.substring(0, 1) == '.' ? custom_extension : '.' + custom_extension } let file_extn = custom_extension ? custom_extension : path.extname(remoteURL); let org_file_name = custom_file_name ? custom_file_name + file_extn : path.basename(remoteURL); let file_name = save_path + '/' + org_file_name; let headersObj = {}; if (options.headers) { headersObj = { ...headersObj, ...options.headers }; } await new Promise((resolve, reject) => { axios({ method: 'get', url: remoteURL, // full signed URL with query params responseType: 'stream', headers: headersObj, maxRedirects: 5, // just in case the signed URL includes redirects validateStatus: status => status < 500 // allow handling 403/404 within response }).then(response => { if (response.status !== 200) { return reject(new Error(`Download failed with status code ${response.status}`)); } const writer = fs.createWriteStream(file_name); response.data.pipe(writer); writer.on('finish', resolve); writer.on('error', reject); }).catch(error => { console.error("Axios download error:", error.message || error); reject(error); }); }); return { "file_name": org_file_name, "path": options.local_path.substring(7), // "status_code": ???, you might want to handle this differently with axios }; };