Skip to content

Commit ff1ef27

Browse files
author
Charles Shin
authored
feat: exporting processFile to be used in studio (#1001)
* feat: exporting processFile to be used in studio * chore: increasing unit test timeout to 30m * Revert "chore: increasing unit test timeout to 30m" This reverts commit 416eb71.
1 parent 96b7b9d commit ff1ef27

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License").
5+
You may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { StorageManagerProps } from '@aws-amplify/ui-react-storage';
18+
import { STORAGE_FILE_ALGO_TYPE } from './constants';
19+
20+
export const processFile = async ({ file }: Parameters<NonNullable<StorageManagerProps['processFile']>>[0]) => {
21+
const fileExtension = file.name.split('.').pop();
22+
return file
23+
.arrayBuffer()
24+
.then((filebuffer) => window.crypto.subtle.digest(STORAGE_FILE_ALGO_TYPE, filebuffer))
25+
.then((hashBuffer) => {
26+
const hashArray = Array.from(new Uint8Array(hashBuffer));
27+
const hashHex = hashArray.map((a) => a.toString(16).padStart(2, '0')).join('');
28+
return { file, key: `${hashHex}.${fileExtension}` };
29+
});
30+
};

0 commit comments

Comments
 (0)