1- import { apiRequest } from './apiRequest' ;
2- import { errorDetails } from '../util/errorDetails' ;
3-
4- // TO DO - convert to typescript, use defintion of `Upload` at public/video-ui/src/components/VideoUpload/VideoAsset.tsx
5-
6- // See http://andrewhfarmer.com/aws-sdk-with-webpack/ for why this is strange
7- import 'aws-sdk/dist/aws-sdk' ;
8- const AWS = window . AWS ;
9-
10- // The timeout for individual upload requests. Defaults to 120s. This
11- // default causes problems on slow connections – for example, w/ a 0.5mbps
12- // upload speed (3.75MB/minute), uploads for 8mb chunks will never complete.
13- AWS . config . httpOptions . timeout = 240_000 ; // in ms
14- const httpOptions = {
15- // The number of multipart uploads to run concurrently. Defaults to 4,
16- // which has caused problems with slow connections timing out requests
17- // prematurely. We judge allowing uploads on slow connections to be
18- // more valuable than a minor boost in upload speed due to concurrent uploads.
19- queueSize : 1
20- } ;
1+ import { apiRequest } from './apiRequest' ;
2+ import { errorDetails } from '../util/errorDetails' ;
3+ import { S3 } from '@aws-sdk/client-s3' ;
4+ import { XhrHttpHandler } from "@aws-sdk/xhr-http-handler" ;
5+
6+
7+ // TO DO - convert to typescript, use definition of `Upload` at public/video-ui/src/components/VideoUpload/VideoAsset.tsx
218
229/**
23- *
24- * @param {string } atomId
25- * @returns
10+ *
11+ * @param atomId {string}
12+ * @returns { Promise<unknown> }
2613 */
2714export function getUploads ( atomId ) {
2815 return apiRequest ( {
2916 url : `/api/uploads?atomId=${ atomId } `
3017 } ) ;
3118}
3219
20+ /**
21+ *
22+ * @param atomId {string}
23+ * @param file {File}
24+ * @param selfHost {boolean}
25+ * @returns {Promise<unknown> }
26+ */
3327export function createUpload ( atomId , file , selfHost ) {
3428 return apiRequest ( {
3529 url : `/api/uploads` ,
@@ -56,49 +50,73 @@ function getCredentials(id, key) {
5650 } ) ;
5751}
5852
59- function getS3 ( bucket , region , credentials ) {
53+ /**
54+ *
55+ * @param region {string}
56+ * @param credentials {any}
57+ * @returns {S3 }
58+ */
59+ function getS3 ( region , credentials ) {
6060 const { temporaryAccessId, temporarySecretKey, sessionToken } = credentials ;
61- const awsCredentials = new AWS . Credentials (
62- temporaryAccessId ,
63- temporarySecretKey ,
64- sessionToken
65- ) ;
66-
67- return new AWS . S3 ( {
68- apiVersion : '2006-03-01' ,
61+
62+ const awsCredentials = {
63+ accessKeyId : temporaryAccessId ,
64+ secretAccessKey : temporarySecretKey ,
65+ sessionToken : sessionToken
66+ } ;
67+
68+ return new S3 ( {
6969 credentials : awsCredentials ,
70- params : { Bucket : bucket } ,
70+ requestHandler : XhrHttpHandler . create ( {
71+ requestTimeout : 240_000
72+ } ) ,
7173 region : region ,
7274 useAccelerateEndpoint : true
7375 } ) ;
7476}
7577
78+ /**
79+ * Upload single part of file
80+ *
81+ * @param upload {Upload}
82+ * @param part {typeof Upload['parts'][number]}
83+ * @param file {File}
84+ * @param progressFn {(completed: number) => any}
85+ * @returns {Promise<unknown> }
86+ */
7687function uploadPart ( upload , part , file , progressFn ) {
7788 const slice = file . slice ( part . start , part . end ) ;
7889
7990 return getCredentials ( upload . id , part . key ) . then ( credentials => {
8091 const s3 = getS3 (
81- upload . metadata . bucket ,
8292 upload . metadata . region ,
8393 credentials
8494 ) ;
8595
86- const params = {
96+ const request = slice . arrayBuffer ( ) . then ( body => s3 . putObject ( {
97+ Bucket : upload . metadata . bucket ,
8798 Key : part . key ,
88- Body : slice ,
89- ACL : 'private' ,
90- Metadata : { original : file . name }
91- } ;
92- const request = s3 . upload ( params , httpOptions ) ;
93-
94- request . on ( 'httpUploadProgress' , event => {
95- progressFn ( part . start + event . loaded ) ;
99+ Metadata : { original : file . name } ,
100+ Body : body
101+ } ) ) ;
102+
103+ request . then ( ( ) => {
104+ progressFn ( part . end ) ;
96105 } ) ;
97106
98- return request . promise ( ) ;
107+ return request ;
99108 } ) ;
100109}
101110
111+ /**
112+ * Recursively upload all parts of file
113+ *
114+ * @param upload {Upload}
115+ * @param parts {typeof Upload['parts']}
116+ * @param file {File}
117+ * @param progressFn {(completed: number) => any}
118+ * @returns {Promise<boolean> }
119+ */
102120export function uploadParts ( upload , parts , file , progressFn ) {
103121 return new Promise ( ( resolve , reject ) => {
104122 function uploadPartRecursive ( parts ) {
0 commit comments