-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { UploadxOptions } from './interfaces'; | ||
import { Uploader } from './uploader'; | ||
import { b64, resolveUrl } from './utils'; | ||
|
||
export class Tus extends Uploader { | ||
constructor(readonly file: File, options: UploadxOptions) { | ||
super(file, options); | ||
this.headers['Tus-Resumable'] = '1.0.0'; | ||
} | ||
|
||
async getFileUrl(): Promise<string> { | ||
const encodedMetaData = b64.serialize(this.metadata); | ||
const headers = { | ||
'Upload-Length': `${this.size}`, | ||
'Upload-Metadata': `${encodedMetaData}` | ||
}; | ||
|
||
await this.request({ | ||
method: 'POST', | ||
url: this.endpoint, | ||
headers | ||
}); | ||
const location = this.getValueFromResponse('location'); | ||
if (!location) { | ||
throw new Error('Invalid or missing Location header'); | ||
} | ||
this.offset = this.responseStatus === 201 ? 0 : undefined; | ||
return resolveUrl(location, this.endpoint); | ||
} | ||
|
||
async sendFileContent(): Promise<number | undefined> { | ||
const { body } = this.getChunk(); | ||
const headers = { | ||
'Content-Type': 'application/offset+octet-stream', | ||
'Upload-Offset': `${this.offset}` | ||
}; | ||
await this.request({ | ||
method: 'PATCH', | ||
body, | ||
headers | ||
}); | ||
return this.getOffsetFromResponse(); | ||
} | ||
|
||
async getOffset(): Promise<number | undefined> { | ||
await this.request({ method: 'HEAD' }); | ||
return this.getOffsetFromResponse(); | ||
} | ||
|
||
protected getOffsetFromResponse(): number | undefined { | ||
const offsetStr = this.getValueFromResponse('Upload-Offset'); | ||
return offsetStr ? parseInt(offsetStr, 10) : undefined; | ||
} | ||
|
||
protected setAuth(token: string) { | ||
this.headers.Authorization = `Bearer ${token}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters