Previously when connected to a FTP server which was on filezilla server I was able to list files and download them but as I switched to FTPS on VSFTPD server the files are listed as it is but the download is not working. It doesn't show any error just stuck on loading screen for response from server. npm Packages tried basic-ftp,ssh2,ssh2-sftp-client
import { Injectable } from '@nestjs/common';
import 'dotenv/config';
import * as fs from 'fs';
import { Readable } from 'stream';
import * as mime from 'mime';
import * as ftp from 'ftp';
@Injectable()
export class CommonFTPService {
constructor() {
}
//Connection
async connectToFTPS(): Promise<any> {
return new Promise(function (resolve, reject) {
var FTPClient = require('ftp');
var conn = new FTPClient();
let connectionOption = {
host: process.env.FTP_HOST,
user: process.env.FTP_USER,
password: process.env.FTP_PASSWORD,
port: process.env.FTP_PORT,
readyTimeout: 20000,
secureOptions: { rejectUnauthorized: false },
secure: true
};
conn.connect(connectionOption);
conn.on('ready', function (err, res) {
if (err) {
reject(err);
} else {
resolve(conn);
}
});
conn.on('error', function (err) {
// console.log("c",err)
reject(err);
});
});
}
//File listing
async listFilesFTP(ftp, path): Promise<any> {
return new Promise(async (resolve, reject) => {
ftp.list(path, (err, files) => {
if (err) {
// console.log(err)
reject({ err, code: err.code || 0 });
} else {
const fileList = [];
for (const file of files) {
fileList.push(file);
}
resolve({ list: fileList });
}
});
});
}
//Download files
async downloadftpfile(ftp, filename, res) {
var getfile = filename
console.log('File Path:: ' + getfile);
var mimetype = mime.getType(filename);
console.log('Mime Type:: ' + mimetype);
ftp.get(getfile, function (err, stream) {
let fileBuffer = null;
let chunks = [];
if (err) throw err;
stream.once('close', function () {
ftp.end();
fileBuffer = Buffer.concat(chunks);
const readstream = new Readable();
readstream.push(fileBuffer);
readstream.push(null);
res.set({
'Access-Control-Expose-Headers': '*',
'Content-Type': mimetype,
'content-disposition': '; filename=' + filename,
'Content-Length': fileBuffer.length,
});
readstream.pipe(res);
});
stream.on('data', function (chunk) {
chunks.push(chunk);
});
});
}
}