Using FTP on wix

Someone has tried recently using wix to connect to a ftp server? I’ve tried using my
websites to connect to my server but I have got very inconsistent results, sometimes I’m
able to send the file to my server, sometimes it gets stuck and never uploads and
sometimes it works on the first try … idk it’s very inconsistent. I have not configured
anything else, just the js functions and the server info to connect. The function receives the content from the file, and the filename and directory. Here is my code

const ftp = require(‘ftp’);

const { Readable } = require(‘stream’);

export function uploadFileFTP(credentials, fileContent, remotePath) {

**return** **new** Promise((resolve, reject) => {

    **const** client = **new** ftp();

    **let** isSettled = **false**;

    **const** finish = (err, info) => {

        **if** (isSettled) **return**;

        isSettled = **true**;

        **if** (err) {

            console.error('FTP transfer error:', err.message || err);

            client.end();

            **return** reject(err);

        }

        client.end();

        resolve(info);

    };

    client.once('ready', () => {

        console.log('Connected to FTP server');

        // Convert content to Buffer (text or binary)

        **const** buffer = Buffer.isBuffer(fileContent)

            ? fileContent

            : Buffer.**from**(fileContent, 'utf8');

        **const** contentStream = Readable.**from**(buffer);

        **const** totalBytes = buffer.length;

        console.log(\`Uploading to "${remotePath}" (${totalBytes} bytes)...\`);

        // Split folder and file name

        **const** lastSlash = remotePath.lastIndexOf("/");

        **const** folder = lastSlash > 0 ? remotePath.slice(0, lastSlash) : "";

        **const** fileName = lastSlash > 0 ? remotePath.slice(lastSlash + 1) : remotePath;

        **const** upload = () => {

            client.put(contentStream, remotePath, (err) => {

                **if** (err) **return** finish(err);

                console.log('File uploaded successfully');

                finish(**null**, {

                    success: **true**,

                    bytesTransferred: totalBytes,

                    remotePath

                });

            });

        };

        **if** (folder) {

            // Create folder if it doesn't exist

            client.mkdir(folder, **true**, (err) => {

                **if** (err) **return** finish(err);

                upload();

            });

        } **else** {

            upload();

        }

    });

    client.once('error', (err) => finish(err));

    client.once('end', () => {

        **if** (!isSettled) {

            finish(**new** Error('The FTP connection was closed before the transfer was completed.'));

        }

    });



    // Connection configuration

    **const** connectionConfig = {

        ...credentials,

        connTimeout: 60000,

        pasvTimeout: 60000,

        keepalive: 30000

    };

    console.log('Connecting to FTP server...');

    client.connect(connectionConfig);

});

}

Are you receiving any errors in your Wix Logs? If so, could you please post it :grinning_face:

It is possible you are running into a Compute or Data Limit related to how long the stream is running.

I sometimes receive an 504 error when I’m debugging from the wix studio editor. It’s peculiar, because I run the code once and it uploads the file successfully, but then I try again and it gives me this 504 error. I understand this error is cause by an excessive backend-runtime, but I run it again a day later and it runs fine, without changing any parameters.

When it runs via a job, using wix logs I don’t get any info of any error, it just doesn’t upload the file.

I’ve tried saving the file on a temp folder and uploading from the directoy but neither way works consistently.

You are correct that you are likely running into a timeout related to excessive use of the backend runtime.

I would recommend to take a look at the Compute Features to understand if what you are trying to implement is ideal or feasible based on our infrastructure.

Is there any way we can check that? I would be very interested in implementing this on my e-commerce, or is there any way to expand my backend timeout? What cofuses me, is that sometimes it runs fine, and sometimes it does not, it’s very inconsistent