|
@@ -1,4 +1,5 @@
|
|
const Plugin = require('../../core/Plugin')
|
|
const Plugin = require('../../core/Plugin')
|
|
|
|
+const RequestClient = require('../../server/RequestClient')
|
|
const UppySocket = require('../../core/UppySocket')
|
|
const UppySocket = require('../../core/UppySocket')
|
|
const {
|
|
const {
|
|
emitSocketProgress,
|
|
emitSocketProgress,
|
|
@@ -7,13 +8,6 @@ const {
|
|
} = require('../../core/Utils')
|
|
} = require('../../core/Utils')
|
|
const Uploader = require('./MultipartUploader')
|
|
const Uploader = require('./MultipartUploader')
|
|
|
|
|
|
-function handleResponse (response) {
|
|
|
|
- if (response.ok) return response.json()
|
|
|
|
- return response.json().then((err) => {
|
|
|
|
- throw err
|
|
|
|
- })
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Create a wrapper around an event emitter with a `remove` method to remove
|
|
* Create a wrapper around an event emitter with a `remove` method to remove
|
|
* all events that were added using the wrapped emitter.
|
|
* all events that were added using the wrapped emitter.
|
|
@@ -39,6 +33,7 @@ module.exports = class AwsS3Multipart extends Plugin {
|
|
this.type = 'uploader'
|
|
this.type = 'uploader'
|
|
this.id = 'AwsS3Multipart'
|
|
this.id = 'AwsS3Multipart'
|
|
this.title = 'AWS S3 Multipart'
|
|
this.title = 'AWS S3 Multipart'
|
|
|
|
+ this.server = new RequestClient(uppy, opts)
|
|
|
|
|
|
const defaultOptions = {
|
|
const defaultOptions = {
|
|
timeout: 30 * 1000,
|
|
timeout: 30 * 1000,
|
|
@@ -93,37 +88,24 @@ module.exports = class AwsS3Multipart extends Plugin {
|
|
createMultipartUpload (file) {
|
|
createMultipartUpload (file) {
|
|
this.assertHost()
|
|
this.assertHost()
|
|
|
|
|
|
- return fetch(`${this.opts.host}/s3/multipart`, {
|
|
|
|
- method: 'post',
|
|
|
|
- body: JSON.stringify({
|
|
|
|
- filename: file.name,
|
|
|
|
- type: file.type
|
|
|
|
- }),
|
|
|
|
- headers: {
|
|
|
|
- accept: 'application/json',
|
|
|
|
- 'content-type': 'application/json'
|
|
|
|
- }
|
|
|
|
- }).then(handleResponse)
|
|
|
|
|
|
+ return this.server.post('s3/multipart', {
|
|
|
|
+ filename: file.name,
|
|
|
|
+ type: file.type
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
listParts (file, { key, uploadId }) {
|
|
listParts (file, { key, uploadId }) {
|
|
this.assertHost()
|
|
this.assertHost()
|
|
|
|
|
|
const filename = encodeURIComponent(key)
|
|
const filename = encodeURIComponent(key)
|
|
- return fetch(`${this.opts.host}/s3/multipart/${uploadId}?key=${filename}`, {
|
|
|
|
- method: 'get',
|
|
|
|
- headers: { accept: 'application/json' }
|
|
|
|
- }).then(handleResponse)
|
|
|
|
|
|
+ return this.server.get(`s3/multipart/${uploadId}?key=${filename}`)
|
|
}
|
|
}
|
|
|
|
|
|
prepareUploadPart (file, { key, uploadId, number }) {
|
|
prepareUploadPart (file, { key, uploadId, number }) {
|
|
this.assertHost()
|
|
this.assertHost()
|
|
|
|
|
|
const filename = encodeURIComponent(key)
|
|
const filename = encodeURIComponent(key)
|
|
- return fetch(`${this.opts.host}/s3/multipart/${uploadId}/${number}?key=${filename}`, {
|
|
|
|
- method: 'get',
|
|
|
|
- headers: { accept: 'application/json' }
|
|
|
|
- }).then(handleResponse)
|
|
|
|
|
|
+ return this.server.get(`s3/multipart/${uploadId}/${number}?key=${filename}`)
|
|
}
|
|
}
|
|
|
|
|
|
completeMultipartUpload (file, { key, uploadId, parts }) {
|
|
completeMultipartUpload (file, { key, uploadId, parts }) {
|
|
@@ -131,14 +113,7 @@ module.exports = class AwsS3Multipart extends Plugin {
|
|
|
|
|
|
const filename = encodeURIComponent(key)
|
|
const filename = encodeURIComponent(key)
|
|
const uploadIdEnc = encodeURIComponent(uploadId)
|
|
const uploadIdEnc = encodeURIComponent(uploadId)
|
|
- return fetch(`${this.opts.host}/s3/multipart/${uploadIdEnc}/complete?key=${filename}`, {
|
|
|
|
- method: 'post',
|
|
|
|
- headers: {
|
|
|
|
- accept: 'application/json',
|
|
|
|
- 'content-type': 'application/json'
|
|
|
|
- },
|
|
|
|
- body: JSON.stringify({ parts })
|
|
|
|
- }).then(handleResponse)
|
|
|
|
|
|
+ return this.server.post(`s3/multipart/${uploadIdEnc}/complete?key=${filename}`, { parts })
|
|
}
|
|
}
|
|
|
|
|
|
abortMultipartUpload (file, { key, uploadId }) {
|
|
abortMultipartUpload (file, { key, uploadId }) {
|
|
@@ -146,10 +121,7 @@ module.exports = class AwsS3Multipart extends Plugin {
|
|
|
|
|
|
const filename = encodeURIComponent(key)
|
|
const filename = encodeURIComponent(key)
|
|
const uploadIdEnc = encodeURIComponent(uploadId)
|
|
const uploadIdEnc = encodeURIComponent(uploadId)
|
|
- return fetch(`${this.opts.host}/s3/multipart/${uploadIdEnc}?key=${filename}`, {
|
|
|
|
- method: 'delete',
|
|
|
|
- headers: { accept: 'application/json' }
|
|
|
|
- }).then(handleResponse)
|
|
|
|
|
|
+ return this.server.delete(`s3/multipart/${uploadIdEnc}?key=${filename}`)
|
|
}
|
|
}
|
|
|
|
|
|
uploadFile (file) {
|
|
uploadFile (file) {
|