@@ -59,6 +59,8 @@ var getDropboxPath = function (path) {
59
59
return cleanPath ( PATH_PREFIX + '/' + path ) . replace ( / \/ $ / , '' ) ;
60
60
} ;
61
61
62
+ const isPublicPath = path => path . match ( / ^ \/ p u b l i c \/ .* [ ^ / ] $ / ) ;
63
+
62
64
var compareApiError = function ( response , expect ) {
63
65
return new RegExp ( '^' + expect . join ( '\\/' ) + '(\\/|$)' ) . test ( response . error_summary ) ;
64
66
} ;
@@ -575,26 +577,52 @@ Dropbox.prototype = {
575
577
return this . _deleteSimple ( path ) ;
576
578
} ,
577
579
580
+ /**
581
+ * Retrieve full, absolute URL of an item. Items which are non-public or do
582
+ * not exist always resolve to undefined.
583
+ *
584
+ * @returns {Promise } - resolves to an absolute URL of the item
585
+ *
586
+ * @protected
587
+ */
588
+ getItemURL : function ( path ) {
589
+ if ( ! isPublicPath ( path ) ) {
590
+ return Promise . resolve ( undefined ) ;
591
+ }
592
+
593
+ let url = this . _itemRefs [ path ] ;
594
+ if ( url !== undefined ) {
595
+ return Promise . resolve ( url ) ;
596
+ }
597
+
598
+ return this . _getSharedLink ( path ) . then ( ( link ) => {
599
+ if ( link !== undefined ) {
600
+ return link ;
601
+ }
602
+ return this . _share ( path ) ;
603
+ } ) ;
604
+ } ,
605
+
578
606
/**
579
607
* Calls share, if the provided path resides in a public folder.
580
608
*
581
609
* @private
582
610
*/
583
611
_shareIfNeeded : function ( path ) {
584
- if ( path . match ( / ^ \/ p u b l i c \/ . * [ ^ / ] $ / ) && this . _itemRefs [ path ] === undefined ) {
585
- this . share ( path ) ;
612
+ if ( isPublicPath ( path ) && this . _itemRefs [ path ] === undefined ) {
613
+ this . _share ( path ) ;
586
614
}
587
615
} ,
588
616
589
617
/**
590
618
* Gets a publicly-accessible URL for the path from Dropbox and stores it
591
- * in ``_itemRefs``.
619
+ * in ``_itemRefs``. Resolves to undefined if the path does not exist.
592
620
*
593
621
* @return {Promise } a promise for the URL
594
622
*
595
623
* @private
596
624
*/
597
- share : function ( path ) {
625
+ _share : function ( path ) {
598
626
var url = 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings' ;
599
627
var options = {
600
628
body : { path : getDropboxPath ( path ) }
@@ -617,6 +645,9 @@ Dropbox.prototype = {
617
645
if ( compareApiError ( body , [ 'shared_link_already_exists' ] ) ) {
618
646
return this . _getSharedLink ( path ) ;
619
647
}
648
+ if ( compareApiError ( body , [ 'path' , 'not_found' ] ) ) {
649
+ return Promise . resolve ( undefined ) ;
650
+ }
620
651
621
652
return Promise . reject ( new Error ( 'API error: ' + body . error_summary ) ) ;
622
653
}
@@ -980,7 +1011,8 @@ Dropbox.prototype = {
980
1011
} ,
981
1012
982
1013
/**
983
- * Requests the link for an already-shared file or folder.
1014
+ * Requests the link for a shared file or folder. Resolves to undefined if
1015
+ * the requested file or folder has not bee shared.
984
1016
*
985
1017
* @param {string } path - path to the file or folder
986
1018
*
@@ -1002,7 +1034,8 @@ Dropbox.prototype = {
1002
1034
return Promise . reject ( new Error ( 'Invalid response status: ' + response . status ) ) ;
1003
1035
}
1004
1036
1005
- var body ;
1037
+ let body ;
1038
+ let link ;
1006
1039
1007
1040
try {
1008
1041
body = JSON . parse ( response . responseText ) ;
@@ -1011,14 +1044,16 @@ Dropbox.prototype = {
1011
1044
}
1012
1045
1013
1046
if ( response . status === 409 ) {
1047
+ if ( compareApiError ( body , [ 'path' , 'not_found' ] ) ) {
1048
+ return Promise . resolve ( undefined ) ;
1049
+ }
1014
1050
return Promise . reject ( new Error ( 'API error: ' + response . error_summary ) ) ;
1015
1051
}
1016
1052
1017
- if ( ! body . links . length ) {
1018
- return Promise . reject ( new Error ( 'No links returned' ) ) ;
1053
+ if ( body . links . length ) {
1054
+ link = body . links [ 0 ] . url ;
1019
1055
}
1020
-
1021
- return Promise . resolve ( body . links [ 0 ] . url ) ;
1056
+ return Promise . resolve ( link ) ;
1022
1057
} , ( error ) => {
1023
1058
error . message = 'Could not get link to a shared file or folder ("' + path + '"): ' + error . message ;
1024
1059
return Promise . reject ( error ) ;
@@ -1066,9 +1101,7 @@ function unHookSync(rs) {
1066
1101
function hookGetItemURL ( rs ) {
1067
1102
if ( rs . _origBaseClientGetItemURL ) { return ; }
1068
1103
rs . _origBaseClientGetItemURL = BaseClient . prototype . getItemURL ;
1069
- BaseClient . prototype . getItemURL = function ( /*path*/ ) {
1070
- throw new Error ( 'getItemURL is not implemented for Dropbox yet' ) ;
1071
- } ;
1104
+ BaseClient . prototype . getItemURL = rs . dropbox . getItemURL . bind ( rs . dropbox ) ;
1072
1105
}
1073
1106
1074
1107
/**
0 commit comments