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

[question] File upload without useTempFile results in empty files #118

Closed
kittaakos opened this issue Feb 11, 2019 · 7 comments
Closed

[question] File upload without useTempFile results in empty files #118

kittaakos opened this issue Feb 11, 2019 · 7 comments

Comments

@kittaakos
Copy link

My files are created but the content is empty after calling mv on the UploadedFile. I am using "express-fileupload": "^1.1.1-alpha.2".

// @ts-check

const fs = require('fs');
const temp = require('temp');
const path = require('path');
const express = require('express');
const fileupload = require('express-fileupload');

const track = temp.track();
const uploadRoot = track.mkdirSync();

const port = process.argv[2] || 8080;
const app = express();
app.use(fileupload());

app.post('/upload', (req, res) => {
    if (!req.files) {
        return res.status(400).send(`No 'files' were selected for upload.`);
    }
    const keys = Object.keys(req.files);
    if (keys.length === 0) {
        return res.status(400).send(`No 'files' were selected for upload.`);
    }
    for (const key of keys) {
        const files = req.files[key];
        const uploads = Array.isArray(files) ? files : [files];
        for (const upload of uploads) {
            const { name } = upload;
            const toPath = path.join(uploadRoot, name);
            upload.mv(toPath, error => {
                if (error) {
                    return res.status(500).send(`Failed to upload ${name}.`);
                }
                console.log(`File ${fs.existsSync(toPath) ? 'exists' : 'does NOT exist'} under ${toPath}.`);
                if (fs.existsSync(toPath)) {
                    console.log(`Content of ${toPath}:`);
                    console.log(fs.readFileSync(toPath).toString());
                }
            });
        }
    }
    res.status(200).send();
});

app.listen(port, () => {
    console.log(`Server is listening on ${port}...`);
    console.log(`Upload functionality is rooted to ${uploadRoot}`);
});

The files I am trying to upload.

$ pwd && echo foo > foo.txt && echo bar > bar.txt && cat foo.txt && cat bar.txt
/Users/akos.kitta/Desktop
foo
bar
$ curl -F 'foo.txt=@/Users/akos.kitta/Desktop/foo.txt' -F 'bar.txt=@/Users/akos.kitta/Desktop/bar.txt' http://localhost:8080/upload

The buffer-based move logic never runs because the !options.tempFilePath is false although I set neither the useTempFiles nor the tempFileDir properties in the options. Perhaps this change is related. If in debug mode, I explicitly unset the options.tempFilePath and the moveFromBuffer is called, then it works as expected.

Thank you!

@RomanBurunkov
Copy link
Collaborator

Looks you are right.

this tempFilePath allways has a value, probably it is a not best way for such check.

I'm currently working on some changes for this module and they also will fix this issue.

@kittaakos
Copy link
Author

kittaakos commented Feb 11, 2019

I'm currently working on some changes for this module and they also will fix this issue.

Awesome. ❤️ Let me know if there's anything I can assist you.

As a workaround, I did this for now:

const { name, data } = upload;
const toPath = path.join(uploadRoot, name);
upload.mv(toPath, uploadErr => {
    if (uploadErr) {
        return res.status(500).send(`Failed to upload ${name}.`);
    }
    const stream = fs.createWriteStream(toPath, { encoding: 'utf8' });
    stream.once('open', () => {
        stream.write(data, writeErr => {
            if (writeErr) {
                return res.status(500).send(`Failed to write content ${name}.`);
            }
            stream.close();
            console.log(`File ${fs.existsSync(toPath) ? 'exists' : 'does NOT exist'} under ${toPath}.`);
            if (fs.existsSync(toPath)) {
                console.log(`Content of ${toPath}:`);
                console.log(fs.readFileSync(toPath).toString());
            }
        })
    });
});

@RomanBurunkov
Copy link
Collaborator

Hi, @kittaakos , @richardgirges

I've created some changes which will fix that issue. please see PR #119

@kittaakos
Copy link
Author

@RomanBurunkov, I have checked out the master of your fork; unfortunately, I had test failures. I am more than happy to verify your changes once more when the build is fixed.

@RomanBurunkov
Copy link
Collaborator

@kittaakos Fixed some typos and erros. All tests passed ;)

@kittaakos
Copy link
Author

All tests passed ;)

Nice. I can confirm, my example from above works with your changes 🎉

@richardgirges
Copy link
Owner

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

3 participants