Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,24 @@ describe('UploadInputV4', () => {
expect(await screen.findByText('File type not allowed')).toBeInTheDocument();
expect(screen.getByText('video.png')).toBeInTheDocument();
expect(screen.queryByText(/Uploading/)).not.toBeInTheDocument();
expect(onError).toHaveBeenCalledWith(expect.any(Error), 412, 'test-upload');
expect(onError).toHaveBeenCalledWith(expect.any(Error), 412, 'test-upload', expect.any(File));
expect(api.puts()).toHaveLength(0);
});

test('onError passes the File that failed, not a sibling still uploading', async () => {
api.respond('PUT', /bucket\.test\/.*bad\.png/, () => api.networkError());
holdPuts();
const onError = jest.fn();
const { container } = render(<UploadInputV4 {...defaultProps} maxFiles={2} onError={onError} />);
const good = fileOf('good.png', 2048, 'image/png');
const bad = fileOf('bad.png', 2048, 'image/png');
select(container, good, bad);

await screen.findByTitle('Retry');
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][3]).toBe(bad);
});

test('a connection failure during the PUT shows the readable network message', async () => {
api.respond('PUT', /bucket\.test/, () => api.networkError());
const { container } = render(<UploadInputV4 {...defaultProps} />);
Expand Down Expand Up @@ -376,6 +390,30 @@ describe('UploadInputV4', () => {
await waitFor(() => expect(api.calls('DELETE', /.*/).map(r => r.path)).toEqual(['/uploads/t1']));
});

test('dismissing an error row calls onRemove with the File that failed', async () => {
api.respond('PUT', /bucket\.test/, () => api.networkError());
const onRemove = jest.fn();
const { container } = render(<UploadInputV4 {...defaultProps} onRemove={onRemove} />);
const file = fileOf('video.png', 2048, 'image/png');
select(container, file);
await screen.findByTitle('Retry');

act(() => { fireEvent.click(screen.getByRole('button', { name: 'Dismiss video.png' })); });

expect(onRemove).toHaveBeenCalledTimes(1);
expect(onRemove.mock.calls[0][0]).toBe(file);
});

test('dismissing a row rejected before upload does not call onRemove', async () => {
const onRemove = jest.fn();
const { container } = render(<UploadInputV4 {...defaultProps} onRemove={onRemove} />);
select(container, fileOf('tool.exe', 2048, 'application/octet-stream'));

act(() => { fireEvent.click(screen.getByRole('button', { name: 'Dismiss tool.exe' })); });

expect(onRemove).not.toHaveBeenCalled();
});

test('retry uploads a failed file again and delivers it', async () => {
api.respond('PUT', /bucket\.test/, () => api.networkError(), { times: 1 });
const onUploadComplete = jest.fn();
Expand Down
17 changes: 14 additions & 3 deletions src/components/inputs/upload-input-v4/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ const resolveErrorMessage = (err) => {
* (`${FILE_UPLOAD_API_BASE_URL}/api/v1/files`). Dropzone's `timeOut`,
* `parallelChunkUploads` and `maxConcurrentChunks` are accepted and ignored: Uppy
* retries each request on its own and limits concurrency across all files.
*
* Every File reported through `onUploadStart` is later reported once through
* `onUploadComplete`, `onError(err, status, id, file)` or `onRemove(file)`, and a
* retry starts it again. Dismissing its error row calls `onRemove`, so a parent can
* track exactly which files are running or failed.
Comment on lines +91 to +94

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomrndom Agreed, and the fix should go one step further: onUploadComplete still does not say which File finished, so a parent cannot track exactly which files are running, which is what this doc block promises.

Completion is reported as (body, id, data), so the CFP form has to match the finished upload by original_name and size (isSameFile in call-for-presentations #106). Uppy builds the file id with lastModified (generateFileID in @uppy/utils), so two different files with the same name and size can upload in the same input, and the form can then drop the wrong running entry: the same stale-entry case this PR is meant to close. And as noted above, a failed file is reported twice (onError, then onRemove when its row is dismissed), so "reported once" does not hold either.

Suggested fix: pass the File on completion too. It only adds an argument, so Contract A (onUploadComplete(response, id, data) in sds/direct-to-storage-uploads-uppy-s3-multipart.md) stays intact:

// handleDelivered
if (callback) callback(body, inputId, { media_type: type, media_upload: current }, file.data);

and describe what actually happens:

 * Each attempt of a File reported through `onUploadStart` ends in
 * `onUploadComplete(body, id, data, file)` or `onError(err, status, id, file)`, unless
 * its row is deleted first, which reports `onRemove(file)`. Deleting a delivered row or
 * dismissing an error row also reports `onRemove(file)`, and a retry reports
 * `onUploadStart` again.

A test that runs retry, fail again and dismiss, and checks the callback sequence, would pin this contract down.

*/
const UploadInputV4 = ({
value = [],
Expand Down Expand Up @@ -221,7 +226,8 @@ const UploadInputV4 = ({
{ id: file.id, name: file.name, size: file.size, message: resolveErrorMessage(err) },
]);
const { onError: callback, id: inputId } = propsRef.current;
if (callback) callback(err, err?.status, inputId);
// the File reported by onUploadStart, so a parent can tell which upload failed
if (callback) callback(err, err?.status, inputId, file.data);
};
const onFileRemoved = (file) => {
// A delivered row stays until the parent confirms it through `value`.
Expand Down Expand Up @@ -322,10 +328,15 @@ const UploadInputV4 = ({
if (onRemove) onRemove(uppyFile?.data ?? { name: file.name, size: file.size });
}, [uppy, dropUploadingRow, onRemove]);

// A row rejected before Uppy took the file never reported onUploadStart, so it
// reports nothing here either.
const handleDismissError = useCallback((file) => {
setErrorFiles(prev => prev.filter(f => f.id !== file.id));
if (uppy?.getFile(file.id)) uppy.removeFile(file.id);
}, [uppy]);
const uppyFile = uppy?.getFile(file.id);
if (!uppyFile) return;
uppy.removeFile(file.id);
if (onRemove) onRemove(uppyFile.data);
}, [uppy, onRemove]);

const handleRetryError = useCallback((file) => {
setErrorFiles(prev => prev.filter(f => f.id !== file.id));
Expand Down
Loading