|
502 | 502 | return request._thenRunCallbacks(options); |
503 | 503 | }, |
504 | 504 |
|
| 505 | + /** |
| 506 | + * Request Qiniu upload token |
| 507 | + * @param {string} type |
| 508 | + * @return {AV.Promise} Resolved with the response |
| 509 | + * @private |
| 510 | + */ |
| 511 | + _qiniuToken: function(type) { |
| 512 | + var self = this; |
| 513 | + //Create 16-bits uuid as qiniu key. |
| 514 | + var extName; |
| 515 | + if (AV._isNode) { |
| 516 | + var path = require('path'); |
| 517 | + extName = path.extname(self._name); |
| 518 | + } else { |
| 519 | + var nameParts = self._name.split('.'); |
| 520 | + extName = '.' + nameParts[nameParts.length - 1]; |
| 521 | + } |
| 522 | + var hexOctet = function() { |
| 523 | + return Math.floor((1+Math.random())*0x10000).toString(16).substring(1); |
| 524 | + }; |
| 525 | + var key = hexOctet() + hexOctet() + hexOctet() + hexOctet() + hexOctet() |
| 526 | + + extName; |
| 527 | + |
| 528 | + var data = { |
| 529 | + key: key, |
| 530 | + ACL: self._acl, |
| 531 | + name:self._name, |
| 532 | + mime_type: type, |
| 533 | + metaData: self._metaData |
| 534 | + }; |
| 535 | + if(type && self._metaData.mime_type == null) |
| 536 | + self._metaData.mime_type = type; |
| 537 | + self._qiniu_key = key; |
| 538 | + return AV._request("qiniu", null, null, 'POST', data); |
| 539 | + }, |
| 540 | + |
505 | 541 | /** |
506 | 542 | * @callback UploadProgressCallback |
507 | 543 | * @param {XMLHttpRequestProgressEvent} event - The progress event with 'loaded' and 'total' attributes |
|
526 | 562 | if (!self._previousSave) { |
527 | 563 | if(self._source){ |
528 | 564 | if(AV._isNode){ |
529 | | - //Use qiniu sdk to upload files to qiniu. |
530 | | - var qiniu = require('qiniu'); |
531 | | - var path = require('path'); |
532 | 565 | self._previousSave = self._source.then(function(base64, type) { |
533 | | - //Create 16-bits uuid as qiniu key. |
534 | | - var hexOctet = function() { |
535 | | - return Math.floor((1+Math.random())*0x10000).toString(16).substring(1); |
536 | | - }; |
537 | | - var key = hexOctet() + hexOctet() + hexOctet() + hexOctet() |
538 | | - + path.extname(self._name); |
539 | | - var data = { |
540 | | - key: key, |
541 | | - ACL: self._acl, |
542 | | - name:self._name, |
543 | | - mime_type: type, |
544 | | - metaData: self._metaData |
545 | | - }; |
546 | | - if(type && self._metaData.mime_type == null) |
547 | | - self._metaData.mime_type = type; |
548 | | - self._qiniu_key = key; |
549 | 566 | self._base64 = base64; |
550 | | - return AV._request("qiniu", null, null, 'POST', data); |
| 567 | + return self._qiniuToken(type); |
551 | 568 | }).then(function(response) { |
| 569 | + //Use qiniu sdk to upload files to qiniu. |
| 570 | + var qiniu = require('qiniu'); |
552 | 571 | self._url = response.url; |
553 | 572 | self._bucket = response.bucket; |
554 | 573 | self.id = response.objectId; |
|
573 | 592 | return promise; |
574 | 593 | }); |
575 | 594 | } else if (this._isBlob) { |
576 | | - |
577 | 595 | self._previousSave = self._source.then(function(blob, type) { |
578 | | - //Create 16-bits uuid as qiniu key. |
579 | | - var hexOctet = function() { |
580 | | - return Math.floor((1+Math.random())*0x10000).toString(16).substring(1); |
581 | | - }; |
582 | | - var nameParts = self._name.split('.'); |
583 | | - var key = hexOctet() + hexOctet() + hexOctet() + hexOctet() + hexOctet() |
584 | | - + '.' + nameParts[nameParts.length - 1]; |
585 | | - var data = { |
586 | | - key: key, |
587 | | - ACL: self._acl, |
588 | | - name:self._name, |
589 | | - mime_type: type, |
590 | | - metaData: self._metaData |
591 | | - }; |
592 | | - if(type && self._metaData.mime_type == null) |
593 | | - self._metaData.mime_type = type; |
594 | | - self._qiniu_key = key; |
595 | 596 | self._blob = blob; |
596 | | - return AV._request("qiniu", null, null, 'POST', data); |
| 597 | + return self._qiniuToken(type); |
597 | 598 | }).then(function(response) { |
598 | 599 | self._url = response.url; |
599 | 600 | self._bucket = response.bucket; |
|
0 commit comments