Skip to content

Commit 36e1ee7

Browse files
authored
fix (Storage/FileUploader): FileUploader does not upload processed file contents in certain scenarios (#6050)
* fix: FileUploader should upload processed file regardless of key/path
1 parent 907d6d1 commit 36e1ee7

File tree

5 files changed

+70
-17
lines changed

5 files changed

+70
-17
lines changed

.changeset/mighty-ladybugs-chew.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/ui-react-storage': patch
3+
---
4+
5+
fix (Storage/FileUploader): FileUploader does not upload processed file contents in certain scenarios

examples/next/pages/ui/components/storage/file-uploader/process-file-access-level/index.page.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,24 @@ Amplify.configure(awsExports);
99
const processFile: FileUploaderProps['processFile'] = async ({ file }) => {
1010
const fileExtension = file.name.split('.').pop();
1111

12-
return file
12+
// pretend the input `file` has been compressed:
13+
const blob = new Blob(['Compressed data'], { type: 'text/plain' });
14+
const compressedFile = new File([blob], undefined, {
15+
type: 'text/plain',
16+
});
17+
18+
return compressedFile
1319
.arrayBuffer()
1420
.then((filebuffer) => window.crypto.subtle.digest('SHA-1', filebuffer))
1521
.then((hashBuffer) => {
1622
const hashArray = Array.from(new Uint8Array(hashBuffer));
1723
const hashHex = hashArray
1824
.map((a) => a.toString(16).padStart(2, '0'))
1925
.join('');
20-
return { file, key: `${hashHex}.${fileExtension}` };
26+
return {
27+
file: compressedFile,
28+
key: `${hashHex}.${fileExtension}`,
29+
};
2130
});
2231
};
2332

examples/next/pages/ui/components/storage/storage-manager/process-file-access-level/index.page.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,24 @@ Amplify.configure(awsExports);
1212
const processFile: StorageManagerProps['processFile'] = async ({ file }) => {
1313
const fileExtension = file.name.split('.').pop();
1414

15-
return file
15+
// pretend the input `file` has been compressed:
16+
const blob = new Blob(['Compressed data'], { type: 'text/plain' });
17+
const compressedFile = new File([blob], undefined, {
18+
type: 'text/plain',
19+
});
20+
21+
return compressedFile
1622
.arrayBuffer()
1723
.then((filebuffer) => window.crypto.subtle.digest('SHA-1', filebuffer))
1824
.then((hashBuffer) => {
1925
const hashArray = Array.from(new Uint8Array(hashBuffer));
2026
const hashHex = hashArray
2127
.map((a) => a.toString(16).padStart(2, '0'))
2228
.join('');
23-
return { file, key: `${hashHex}.${fileExtension}` };
29+
return {
30+
file: compressedFile,
31+
key: `${hashHex}.${fileExtension}`,
32+
};
2433
});
2534
};
2635

packages/react-storage/src/components/FileUploader/utils/__tests__/getInput.spec.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,39 +148,69 @@ describe('getInput', () => {
148148
expect(output).toStrictEqual(expected);
149149
});
150150

151-
it('includes additional values returned from `processFile` in `options`', async () => {
151+
it('correctly parses values returned from `processFile`', async () => {
152152
const contentDisposition = 'attachment';
153153
const metadata = { key };
154+
const processedFile = new File([''], `myfile.txt`);
154155

155-
const expected: UploadDataWithPathInput = {
156-
data: file,
156+
const input = getInput({
157+
...pathStringInput,
158+
processFile: ({ key, ...rest }) => ({
159+
...rest,
160+
key: `${processFilePrefix}${key}`,
161+
file: processedFile,
162+
metadata,
163+
contentDisposition,
164+
}),
165+
});
166+
167+
const output = await input();
168+
169+
expect(output).toMatchObject({
170+
data: expect.any(File),
157171
options: {
158172
contentDisposition,
159173
contentType: file.type,
160174
metadata,
161-
onProgress,
175+
onProgress: expect.any(Function),
162176
useAccelerateEndpoint: undefined,
163177
},
164178
path: `${stringPath}${processFilePrefix}${key}`,
165-
};
179+
});
180+
expect(output.data).toBe(processedFile);
181+
});
182+
183+
it('correctly parses values returned from `processFile` when in accessLevel mode', async () => {
184+
const contentDisposition = 'attachment';
185+
const metadata = { key };
186+
const processedFile = new File([], `myfile.txt`);
166187

167188
const input = getInput({
168-
...pathStringInput,
189+
...accessLevelWithoutPathInput,
169190
processFile: ({ key, ...rest }) => ({
191+
...rest,
170192
key: `${processFilePrefix}${key}`,
171193
metadata,
172194
contentDisposition,
173-
...rest,
195+
file: processedFile,
174196
}),
175197
});
176198

177199
const output = await input();
178200

179-
expect(output).toStrictEqual(expected);
180-
expect(output.options?.metadata).toStrictEqual(metadata);
181-
expect(output.options?.contentDisposition).toStrictEqual(
182-
contentDisposition
183-
);
201+
expect(output).toMatchObject({
202+
data: expect.any(File),
203+
options: {
204+
accessLevel,
205+
contentDisposition,
206+
contentType: file.type,
207+
metadata,
208+
onProgress: expect.any(Function),
209+
useAccelerateEndpoint: undefined,
210+
},
211+
});
212+
213+
expect(output.data).toBe(processedFile);
184214
});
185215

186216
it('defaults `options.contentType` to "binary/octet-stream" when no file type is provided', async () => {

packages/react-storage/src/components/FileUploader/utils/getInput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const getInput = ({
6161
hasCallbackPath ? path({ identityId }) : path
6262
}${processedKey}`;
6363

64-
inputResult = { data: file, path: resolvedPath, options };
64+
inputResult = { data, path: resolvedPath, options };
6565
}
6666

6767
return inputResult;

0 commit comments

Comments
 (0)