Skip to content

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

@alirezavafaee

Description

@alirezavafaee

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);
        });
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions