Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempting an infinite loop when the write stream encounters an error. #375

Open
alirezavafaee opened this issue Jun 26, 2024 · 0 comments

Comments

@alirezavafaee
Copy link

I created a CustomFileSystem
When writing method throws an error, FileZilla tries to upload the file again in an Unlimited LOOP.
I used the error event for stream, but it doesn't help.
Can anybody help to solve the problem?
Thanks

    async write(path: string, { append = false, start = undefined }) {
        return new Promise((resolve, reject) => {
           
            const fileName = path;

            const hashName = uniqueStringId();
            const { fsPath } = this.resolvePath(path);
            console.log('Write', { fsPath, hashName });

            const writable = new Writable();

            const chunks: Buffer[] = [];
            writable.on('error', (err) => {
                let filePath = fsPath + sep + hashName;
                unlink(filePath)
                    .catch((err) => console.error('Error delete file in writing', err.message));
                return reject(err);
            });
            writable._write = (chunk, encoding, done) => {
                chunks.push(chunk);
                done();
            };

           /**
           *  Stream write error simulation
           */
            setTimeout(() => {
                writable.emit('error', new Error('.......Stream write error simulation........'));
            }, 500);

            writable.on('finish', async () => {
                let filePath = fsPath + sep + hashName;
                try {
                    await writeFile(filePath, Buffer.concat(chunks));
                    let mimeType = lookup(fileName);
                    if (!mimeType) {
                        this.connection.reply(550, 'File type is not correct!');
                    }
                    const fileStat = await stat(filePath);
                    await this.api.post('/ftp/upload',
                        {
                            name: fileName,
                            mimeType: mimeType,
                            size: fileStat.size,
                            disk: this.disk,
                            dirName: this.dirName
                        });
                } catch (error) {
                    if (error.response?.status === 401) {
                        this.connection.reply(550, 'Token has been expired, please login again.');
                    }
                    console.error('Error writing file:', error.response?.data?.message || error.response?.statusText || error.message);
                    this.connection.reply(550, 'Something went wrong! Please inform us by sending an email to support @fikper.com!');
                    return reject(error);
                }
            });
            resolve(writable);
        });
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant