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