Sfoglia il codice sorgente

s3: Use RequestClient.

Renée Kooi 7 anni fa
parent
commit
b275bfddaf
2 ha cambiato i file con 25 aggiunte e 38 eliminazioni
  1. 10 38
      src/plugins/AwsS3/Multipart.js
  2. 15 0
      src/server/RequestClient.js

+ 10 - 38
src/plugins/AwsS3/Multipart.js

@@ -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) {

+ 15 - 0
src/server/RequestClient.js

@@ -58,4 +58,19 @@ module.exports = class RequestClient {
       // @todo validate response status before calling json
       // @todo validate response status before calling json
       .then((res) => res.json())
       .then((res) => res.json())
   }
   }
+
+  delete (path, data) {
+    return fetch(`${this.hostname}/${path}`, {
+      method: 'post',
+      credentials: 'include',
+      headers: {
+        'Accept': 'application/json',
+        'Content-Type': 'application/json'
+      },
+      body: data ? JSON.stringify(data) : null
+    })
+      .then(this.onReceiveResponse)
+      // @todo validate response status before calling json
+      .then((res) => res.json())
+  }
 }
 }