2021-01-29 20:45:11 +02:00
const BaseSyncTarget = require ( './BaseSyncTarget' ) . default ;
2020-11-05 18:58:23 +02:00
const { _ } = require ( './locale' ) ;
const Setting = require ( './models/Setting' ) . default ;
const { FileApi } = require ( './file-api.js' ) ;
const Synchronizer = require ( './Synchronizer' ) . default ;
const WebDavApi = require ( './WebDavApi' ) ;
const { FileApiDriverWebDav } = require ( './file-api-driver-webdav' ) ;
2023-02-22 15:12:53 +02:00
const checkProviderIsSupported = require ( './utils/webDAVUtils' ) . default ;
2018-02-02 01:40:05 +02:00
class SyncTargetWebDAV extends BaseSyncTarget {
static id ( ) {
return 6 ;
}
2018-02-06 20:59:36 +02:00
static supportsConfigCheck ( ) {
return true ;
2018-02-02 01:40:05 +02:00
}
static targetName ( ) {
2018-03-09 22:59:12 +02:00
return 'webdav' ;
2018-02-02 01:40:05 +02:00
}
static label ( ) {
2018-03-09 22:59:12 +02:00
return _ ( 'WebDAV' ) ;
2018-02-02 01:40:05 +02:00
}
2021-08-16 16:20:14 +02:00
static description ( ) {
return 'The WebDAV protocol allows users to create, change and move documents on a server. There are many WebDAV compatible servers, including SeaFile, Nginx or Apache.' ;
}
2018-03-26 19:33:55 +02:00
async isAuthenticated ( ) {
2018-02-02 01:40:05 +02:00
return true ;
}
2018-02-21 20:36:29 +02:00
static async newFileApi _ ( syncTargetId , options ) {
2018-02-06 20:59:36 +02:00
const apiOptions = {
2018-03-15 19:57:11 +02:00
baseUrl : ( ) => options . path ( ) ,
username : ( ) => options . username ( ) ,
password : ( ) => options . password ( ) ,
2021-04-25 10:50:52 +02:00
ignoreTlsErrors : ( ) => options . ignoreTlsErrors ( ) ,
2018-02-02 01:40:05 +02:00
} ;
2018-02-06 20:59:36 +02:00
const api = new WebDavApi ( apiOptions ) ;
2018-02-02 01:40:05 +02:00
const driver = new FileApiDriverWebDav ( api ) ;
2018-03-09 22:59:12 +02:00
const fileApi = new FileApi ( '' , driver ) ;
2018-02-15 20:01:05 +02:00
fileApi . setSyncTargetId ( syncTargetId ) ;
2018-02-06 20:59:36 +02:00
return fileApi ;
}
static async checkConfig ( options ) {
2018-02-21 20:36:29 +02:00
const fileApi = await SyncTargetWebDAV . newFileApi _ ( SyncTargetWebDAV . id ( ) , options ) ;
fileApi . requestRepeatCount _ = 0 ;
2019-07-29 15:43:53 +02:00
2018-02-06 20:59:36 +02:00
const output = {
ok : false ,
2018-03-09 22:59:12 +02:00
errorMessage : '' ,
2018-02-06 20:59:36 +02:00
} ;
2019-07-29 15:43:53 +02:00
2018-02-06 20:59:36 +02:00
try {
2023-02-22 15:12:53 +02:00
checkProviderIsSupported ( options . path ( ) ) ;
2018-03-09 22:59:12 +02:00
const result = await fileApi . stat ( '' ) ;
2019-09-19 23:51:18 +02:00
if ( ! result ) throw new Error ( ` WebDAV directory not found: ${ options . path ( ) } ` ) ;
2018-02-06 20:59:36 +02:00
output . ok = true ;
} catch ( error ) {
output . errorMessage = error . message ;
2019-09-19 23:51:18 +02:00
if ( error . code ) output . errorMessage += ` (Code ${ error . code } ) ` ;
2018-02-06 20:59:36 +02:00
}
return output ;
}
async initFileApi ( ) {
2018-02-21 20:36:29 +02:00
const fileApi = await SyncTargetWebDAV . newFileApi _ ( SyncTargetWebDAV . id ( ) , {
2018-03-15 19:57:11 +02:00
path : ( ) => Setting . value ( 'sync.6.path' ) ,
username : ( ) => Setting . value ( 'sync.6.username' ) ,
password : ( ) => Setting . value ( 'sync.6.password' ) ,
2021-04-25 10:50:52 +02:00
ignoreTlsErrors : ( ) => Setting . value ( 'net.ignoreTlsErrors' ) ,
2018-02-06 20:59:36 +02:00
} ) ;
2018-02-02 01:40:05 +02:00
fileApi . setLogger ( this . logger ( ) ) ;
2018-02-06 20:59:36 +02:00
2018-02-02 01:40:05 +02:00
return fileApi ;
}
async initSynchronizer ( ) {
2018-03-09 22:59:12 +02:00
return new Synchronizer ( this . db ( ) , await this . fileApi ( ) , Setting . value ( 'appType' ) ) ;
2018-02-02 01:40:05 +02:00
}
}
2019-07-29 15:43:53 +02:00
module . exports = SyncTargetWebDAV ;