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

How reject uploading after checking mimetype #64

Closed
ColorBuffer opened this issue Apr 1, 2018 · 5 comments · Fixed by #81
Closed

How reject uploading after checking mimetype #64

ColorBuffer opened this issue Apr 1, 2018 · 5 comments · Fixed by #81

Comments

@ColorBuffer
Copy link

ColorBuffer commented Apr 1, 2018

Hi, Thanks for Repository.

What is the best efficient way to prevent uploading by checking mimetype (or even filename)?

This is my resolver.

resolve: async (obj, {avatar}, ctx) => {
        try {
            const {stream, filename, mimetype, encoding} = await avatar;
            if (mimetype !== 'image/png') {
                console.log('try to close');
                stream.emit('finish');
                return {};
            }
            let { id, path } = await store.itNow({stream, filename});
            return {id, path, filename, mimetype, encoding};
        }
        catch (e) {
            console.log('error', e);
            return {};
        }
    },

After uploading a non 'image/png', I got no response.
Im using apolloUploadKoa

@jaydenseric
Copy link
Owner

jaydenseric commented Apr 3, 2018

It's a good question, and the answer depends on the situation. If you have multiple files and you don't want one bad one to cancel the entire request, you can use stream.resume() to waste the file.

I'm not sure the best way to immediately cancel the uploads to make an early response, but I imagine it may differ depending if you use Koa or Express. There should be a good way to do it though; this was one of the strong use cases for moving to a stream based API.

Perhaps someone can help? Eventually I'll have to work this answer out for myself; once I start implementing file uploads in my current project.

@hkjeffchan
Copy link

I am using express and I try to stream.destroy() but it failed, busboy keeps working on the remaining data and cause an error. (push after EOF, something like that). Also tried stream.pause before destroy, same result.

It will be great if it can stop the upload immd.

@ColorBuffer
Copy link
Author

any new IDEA? :)

@nfantone
Copy link

Also having this problem. I'd like to bail out without consuming the stream if the mimetype does not match an expected set of types.

@mike-marcacci
Copy link
Collaborator

Hey @nicoosokhan, this will be fixed when #81 gets finished. In a nutshell, we can't respond in HTTP 1.1 until the request is finished, so each file stream needs to be fully consumed.

The approach I'm taking in #81 is to buffer all uploads to the filesystem (using temp files) instead of propagating backpressure up to the network. This will also make it much simpler, as you can ignore one upload without blocking another one.

@nicoosokhan once this is merged, you can simply ignore the stream, and the temp files will get cleaned up eventually. If you want to prevent an accumulation of temp files, you can simply call:

stream.destroy()`

where you have stream.emit('finish');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants