@@ -78,58 +78,65 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
7878		return  super . fileApi ( ) ; 
7979	} 
8080
81- 	public  static  async  checkConfig ( options : FileApiOptions ,  syncTargetId : number  =  null )  { 
81+ 	public  static  async  checkConfig ( options : FileApiOptions ,  syncTargetId : number  =  null ,   fileApi :  FileApi | null   =   null )  { 
8282		const  output  =  { 
8383			ok : false , 
8484			errorMessage : '' , 
8585		} ; 
8686
8787		syncTargetId  =  syncTargetId  ===  null  ? this . id ( )  : syncTargetId ; 
8888
89- 		let   fileApi   =   null ; 
90- 		try  { 
91- 			fileApi  =  await  newFileApi ( syncTargetId ,  options ) ; 
92- 			fileApi . requestRepeatCount_   =   0 ; 
93- 		}   catch   ( error )   { 
94- 			// If there's an error it's probably an application error, but we  
95- 			// can't proceed anyway, so exit. 
96- 			output . errorMessage  =   error . message ; 
97- 			if   ( error . code )   output . errorMessage   +=   ` (Code  ${ error . code } )` ; 
98- 			return   output ; 
89+ 		if   ( ! fileApi )   { 
90+ 			 try  { 
91+ 				 fileApi  =  await  newFileApi ( syncTargetId ,  options ) ; 
92+ 			}   catch   ( error )   { 
93+ 				 // If there's an  error it's probably an application error, but we 
94+ 				 // can't proceed anyway, so exit.  
95+ 				 output . errorMessage   =   error . message ; 
96+ 				 if   ( error . code )   output . errorMessage  +=   ` (Code  ${ error . code } )` ; 
97+ 				 return   output ; 
98+ 			} 
9999		} 
100100
101- 		// First we try to fetch info.json. It may not be present if it's a new 
102- 		// sync target but otherwise, if it is, and it's valid, we know the 
103- 		// credentials are valid. We do this test first because it will work 
104- 		// even if account upload is disabled. And we need such account to 
105- 		// successfully login so that they can fix it by deleting extraneous 
106- 		// notes or resources. 
101+ 		const  previousRequestRepeatCount  =  fileApi . requestRepeatCount_ ; 
102+ 		fileApi . requestRepeatCount_  =  0 ; 
103+ 
107104		try  { 
108- 			const  r  =  await  fileApi . get ( 'info.json' ) ; 
109- 			if  ( r )  { 
110- 				const  parsed  =  JSON . parse ( r ) ; 
111- 				if  ( parsed )  { 
112- 					output . ok  =  true ; 
113- 					return  output ; 
105+ 			// First we try to fetch info.json. It may not be present if it's a new 
106+ 			// sync target but otherwise, if it is, and it's valid, we know the 
107+ 			// credentials are valid. We do this test first because it will work 
108+ 			// even if account upload is disabled. And we need such account to 
109+ 			// successfully login so that they can fix it by deleting extraneous 
110+ 			// notes or resources. 
111+ 			try  { 
112+ 				const  r  =  await  fileApi . get ( 'info.json' ) ; 
113+ 				if  ( r )  { 
114+ 					const  parsed  =  JSON . parse ( r ) ; 
115+ 					if  ( parsed )  { 
116+ 						output . ok  =  true ; 
117+ 						return  output ; 
118+ 					} 
114119				} 
120+ 			}  catch  ( error )  { 
121+ 				// Ignore because we'll use the next test to check for sure if it 
122+ 				// works or not. 
123+ 				staticLogger . warn ( 'Could not fetch or parse info.json:' ,  error ) ; 
115124			} 
116- 		}  catch  ( error )  { 
117- 			// Ignore because we'll use the next test to check for sure if it 
118- 			// works or not. 
119- 			staticLogger . warn ( 'Could not fetch or parse info.json:' ,  error ) ; 
120- 		} 
121125
122- 		// This is a more generic test, which writes a file and tries to read it 
123- 		// back. 
124- 		try  { 
125- 			await  fileApi . put ( 'testing.txt' ,  'testing' ) ; 
126- 			const  result  =  await  fileApi . get ( 'testing.txt' ) ; 
127- 			if  ( result  !==  'testing' )  throw  new  Error ( `Could not access data on server "${ options . path ( ) }  "` ) ; 
128- 			await  fileApi . delete ( 'testing.txt' ) ; 
129- 			output . ok  =  true ; 
130- 		}  catch  ( error )  { 
131- 			output . errorMessage  =  error . message ; 
132- 			if  ( error . code )  output . errorMessage  +=  ` (Code ${ error . code }  )` ; 
126+ 			// This is a more generic test, which writes a file and tries to read it 
127+ 			// back. 
128+ 			try  { 
129+ 				await  fileApi . put ( 'testing.txt' ,  'testing' ) ; 
130+ 				const  result  =  await  fileApi . get ( 'testing.txt' ) ; 
131+ 				if  ( result  !==  'testing' )  throw  new  Error ( `Could not access data on server "${ options . path ( ) }  "` ) ; 
132+ 				await  fileApi . delete ( 'testing.txt' ) ; 
133+ 				output . ok  =  true ; 
134+ 			}  catch  ( error )  { 
135+ 				output . errorMessage  =  error . message ; 
136+ 				if  ( error . code )  output . errorMessage  +=  ` (Code ${ error . code }  )` ; 
137+ 			} 
138+ 		}  finally  { 
139+ 			fileApi . requestRepeatCount_  =  previousRequestRepeatCount ; 
133140		} 
134141
135142		return  output ; 
0 commit comments