-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathwebdav.js
More file actions
895 lines (776 loc) · 30.7 KB
/
webdav.js
File metadata and controls
895 lines (776 loc) · 30.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/**
* WebDAV 工具类
*
* 该模块提供了完整的 WebDAV 客户端功能,支持文件和目录的各种操作。
* 基于 axios 实现,支持身份验证、SSL 配置等高级功能。
*
* 主要功能:
* - WebDAV 服务器连接和身份验证
* - 目录浏览和创建
* - 文件上传、下载、删除
* - 文件和目录信息获取
* - 移动和复制操作
*
* @author drpy-node
* @version 1.0.0
*/
import axios from 'axios';
import https from 'https';
import http from 'http';
import {createReadStream, createWriteStream} from 'fs';
import {pipeline} from 'stream/promises';
import path from 'path';
import * as cheerio from 'cheerio';
/**
* WebDAV 客户端类
*/
export class WebDAVClient {
/**
* 构造函数
* @param {Object} config - 配置对象
* @param {string} config.baseURL - WebDAV 服务器基础 URL
* @param {string} [config.username] - 用户名
* @param {string} [config.password] - 密码
* @param {boolean} [config.rejectUnauthorized=false] - 是否验证 SSL 证书
* @param {number} [config.timeout=30000] - 请求超时时间(毫秒)
* @param {Object} [config.headers] - 自定义请求头
*/
constructor(config) {
this.config = {
rejectUnauthorized: false,
timeout: 30000,
...config
};
// 创建 axios 实例
this.client = axios.create({
baseURL: this.config.baseURL,
timeout: this.config.timeout,
httpsAgent: new https.Agent({
keepAlive: true,
rejectUnauthorized: this.config.rejectUnauthorized
}),
httpAgent: new http.Agent({
keepAlive: true
}),
headers: {
'User-Agent': 'drpy-node-webdav/1.0.0',
...this.config.headers
}
});
// 设置身份验证
if (this.config.username && this.config.password) {
this.client.defaults.auth = {
username: this.config.username,
password: this.config.password
};
}
// 添加响应拦截器处理错误
this.client.interceptors.response.use(
response => response,
error => {
if (error.response) {
const {status, statusText, data} = error.response;
throw new Error(`WebDAV Error ${status}: ${statusText}${data ? ` - ${data}` : ''}`);
}
throw error;
}
);
}
/**
* 测试连接
* @returns {Promise<boolean>} 连接是否成功
*/
async testConnection() {
try {
await this.client.request({
method: 'OPTIONS',
url: '/'
});
return true;
} catch (error) {
console.error('WebDAV connection test failed:', error.message);
return false;
}
}
/**
* 获取目录内容
* @param {string} [remotePath='/'] - 远程目录路径
* @param {number} [depth=1] - 查询深度(0=仅属性,1=直接子项,infinity=所有子项)
* @returns {Promise<Array>} 目录内容列表
*/
async listDirectory(remotePath = '/', depth = 1) {
try {
// log('remotePath:',remotePath);
const normalizedPath = this._normalizePath(remotePath);
// log('normalizedPath:',normalizedPath)
const response = await this.client.request({
method: 'PROPFIND',
url: normalizedPath,
headers: {
'Depth': depth === Infinity ? 'infinity' : depth.toString(),
'Content-Type': 'application/xml'
},
data: `<?xml version="1.0" encoding="utf-8" ?>
<D:propfind xmlns:D="DAV:">
<D:allprop/>
</D:propfind>`
});
return this._parseMultiStatus(response.data, normalizedPath);
} catch (error) {
throw new Error(`Failed to list directory ${remotePath}: ${error.message}`);
}
}
/**
* 获取文件或目录信息
* @param {string} remotePath - 远程路径
* @returns {Promise<Object>} 文件/目录信息
*/
async getInfo(remotePath) {
try {
const response = await this.client.request({
method: 'PROPFIND',
url: this._normalizePath(remotePath),
headers: {
'Depth': '0',
'Content-Type': 'application/xml'
},
data: `<?xml version="1.0" encoding="utf-8" ?>
<D:propfind xmlns:D="DAV:">
<D:allprop/>
</D:propfind>`
});
// 添加调试信息
if (Number(process.env.WEBDAV_DEBUG)) {
console.log('WebDAV PROPFIND response for', remotePath, ':', response.data);
}
const items = this._parseMultiStatus(response.data);
if (items.length === 0) {
throw new Error(`Resource not found or no accessible properties: ${remotePath}`);
}
return items[0];
} catch (error) {
if (error.response && error.response.status === 404) {
throw new Error(`Resource not found: ${remotePath}`);
}
throw new Error(`Failed to get info for ${remotePath}: ${error.message}`);
}
}
/**
* 检查文件或目录是否存在
* @param {string} remotePath - 远程路径
* @returns {Promise<boolean>} 是否存在
*/
async exists(remotePath) {
try {
await this.getInfo(remotePath);
return true;
} catch (error) {
return false;
}
}
/**
* 创建目录
* @param {string} remotePath - 远程目录路径
* @param {boolean} [recursive=false] - 是否递归创建父目录
* @returns {Promise<boolean>} 创建是否成功
*/
async createDirectory(remotePath, recursive = false) {
try {
const normalizedPath = this._normalizePath(remotePath);
if (recursive) {
const pathParts = normalizedPath.split('/').filter(part => part);
let currentPath = '';
for (const part of pathParts) {
currentPath += '/' + part;
if (!(await this.exists(currentPath))) {
await this.client.request({
method: 'MKCOL',
url: currentPath
});
}
}
} else {
await this.client.request({
method: 'MKCOL',
url: normalizedPath
});
}
return true;
} catch (error) {
throw new Error(`Failed to create directory ${remotePath}: ${error.message}`);
}
}
/**
* 上传文件
* @param {string|Buffer|ReadableStream} source - 本地文件路径、Buffer 或可读流
* @param {string} remotePath - 远程文件路径
* @param {Object} [options] - 上传选项
* @param {boolean} [options.overwrite=true] - 是否覆盖已存在的文件
* @param {string} [options.contentType] - 内容类型
* @param {Function} [options.onProgress] - 进度回调函数
* @returns {Promise<boolean>} 上传是否成功
*/
async uploadFile(source, remotePath, options = {}) {
const {overwrite = true, contentType, onProgress} = options;
try {
const normalizedPath = this._normalizePath(remotePath);
// 检查文件是否存在
if (!overwrite && await this.exists(normalizedPath)) {
throw new Error(`File ${remotePath} already exists and overwrite is disabled`);
}
let data;
let headers = {};
if (typeof source === 'string') {
// 本地文件路径
data = createReadStream(source);
if (!contentType) {
headers['Content-Type'] = this._getContentType(source);
}
} else if (Buffer.isBuffer(source)) {
// Buffer
data = source;
headers['Content-Length'] = source.length;
} else {
// 可读流
data = source;
}
if (contentType) {
headers['Content-Type'] = contentType;
}
await this.client.request({
method: 'PUT',
url: normalizedPath,
data,
headers,
onUploadProgress: onProgress
});
return true;
} catch (error) {
throw new Error(`Failed to upload file to ${remotePath}: ${error.message}`);
}
}
/**
* 下载文件
* @param {string} remotePath - 远程文件路径
* @param {string} [localPath] - 本地保存路径,如果不提供则返回 Buffer
* @param {Object} [options] - 下载选项
* @param {Function} [options.onProgress] - 进度回调函数
* @returns {Promise<Buffer|boolean>} 如果提供 localPath 返回 boolean,否则返回 Buffer
*/
async downloadFile(remotePath, localPath, options = {}) {
const {onProgress} = options;
try {
const normalizedPath = this._normalizePath(remotePath);
const response = await this.client.request({
method: 'GET',
url: normalizedPath,
responseType: localPath ? 'stream' : 'arraybuffer',
onDownloadProgress: onProgress
});
if (localPath) {
// 保存到本地文件
const writeStream = createWriteStream(localPath);
await pipeline(response.data, writeStream);
return true;
} else {
// 返回 Buffer
return Buffer.from(response.data);
}
} catch (error) {
throw new Error(`Failed to download file ${remotePath}: ${error.message}`);
}
}
/**
* 获取文件流(用于直链服务)
* @param {string} remotePath - 远程文件路径
* @param {Object} [options] - 选项
* @param {Object} [options.headers] - 额外的请求头(如 Range)
* @returns {Promise<{stream: ReadableStream, headers: Object, size: number}>} 文件流和相关信息
*/
async getFileStream(remotePath, options = {}) {
try {
const normalizedPath = this._normalizePath(remotePath);
const {headers: extraHeaders = {}} = options;
// 首先获取文件信息
const fileInfo = await this.getInfo(normalizedPath);
if (!fileInfo || fileInfo.isDirectory) {
throw new Error(`File not found or is a directory: ${remotePath}`);
}
// 准备请求头
const requestHeaders = {...extraHeaders};
const response = await this.client.request({
method: 'GET',
url: normalizedPath,
responseType: 'stream',
headers: requestHeaders
});
return {
stream: response.data,
headers: response.headers,
size: fileInfo.size,
contentType: fileInfo.contentType || 'application/octet-stream',
lastModified: fileInfo.lastModified,
etag: fileInfo.etag
};
} catch (error) {
throw new Error(`Failed to get file stream for ${remotePath}: ${error.message}`);
}
}
/**
* 删除文件或目录
* @param {string} remotePath - 远程路径
* @returns {Promise<boolean>} 删除是否成功
*/
async delete(remotePath) {
try {
await this.client.request({
method: 'DELETE',
url: this._normalizePath(remotePath)
});
return true;
} catch (error) {
throw new Error(`Failed to delete ${remotePath}: ${error.message}`);
}
}
/**
* 移动文件或目录
* @param {string} sourcePath - 源路径
* @param {string} destinationPath - 目标路径
* @param {boolean} [overwrite=false] - 是否覆盖目标
* @returns {Promise<boolean>} 移动是否成功
*/
async move(sourcePath, destinationPath, overwrite = false) {
try {
const headers = {
'Destination': this._getAbsoluteUrl(destinationPath)
};
if (overwrite) {
headers['Overwrite'] = 'T';
}
await this.client.request({
method: 'MOVE',
url: this._normalizePath(sourcePath),
headers
});
return true;
} catch (error) {
throw new Error(`Failed to move ${sourcePath} to ${destinationPath}: ${error.message}`);
}
}
/**
* 复制文件或目录
* @param {string} sourcePath - 源路径
* @param {string} destinationPath - 目标路径
* @param {boolean} [overwrite=false] - 是否覆盖目标
* @returns {Promise<boolean>} 复制是否成功
*/
async copy(sourcePath, destinationPath, overwrite = false) {
try {
// 首先检查源文件是否存在
const sourceExists = await this.exists(sourcePath);
if (!sourceExists) {
throw new Error(`Source file does not exist: ${sourcePath}`);
}
// 规范化路径
const normalizedSource = this._normalizePath(sourcePath);
const normalizedDestination = this._normalizePath(destinationPath);
// 构建目标 URL - 使用完整的绝对 URL
const destinationUrl = this._getAbsoluteUrl(destinationPath);
const headers = {
'Destination': destinationUrl
};
if (overwrite) {
headers['Overwrite'] = 'T';
} else {
headers['Overwrite'] = 'F';
}
// 添加调试信息
if (Number(process.env.WEBDAV_DEBUG)) {
console.log('WebDAV COPY operation:');
console.log(' Source:', normalizedSource);
console.log(' Destination URL:', destinationUrl);
console.log(' Headers:', headers);
}
const response = await this.client.request({
method: 'COPY',
url: normalizedSource,
headers
});
// 检查响应状态
if (response.status >= 200 && response.status < 300) {
return true;
} else {
throw new Error(`Unexpected response status: ${response.status}`);
}
} catch (error) {
if (error.response) {
const status = error.response.status;
const statusText = error.response.statusText || 'Unknown Error';
throw new Error(`Failed to copy ${sourcePath} to ${destinationPath}: WebDAV Error ${status}: ${statusText}`);
}
throw new Error(`Failed to copy ${sourcePath} to ${destinationPath}: ${error.message}`);
}
}
/**
* 获取文件内容(文本)
* @param {string} remotePath - 远程文件路径
* @param {string} [encoding='utf8'] - 文本编码
* @returns {Promise<string>} 文件内容
*/
async getFileContent(remotePath, encoding = 'utf8') {
try {
const buffer = await this.downloadFile(remotePath);
return buffer.toString(encoding);
} catch (error) {
throw new Error(`Failed to get file content ${remotePath}: ${error.message}`);
}
}
/**
* 写入文件内容(文本)
* @param {string} remotePath - 远程文件路径
* @param {string} content - 文件内容
* @param {string} [encoding='utf8'] - 文本编码
* @param {boolean} [overwrite=true] - 是否覆盖已存在的文件
* @returns {Promise<boolean>} 写入是否成功
*/
async putFileContent(remotePath, content, encoding = 'utf8', overwrite = true) {
try {
const buffer = Buffer.from(content, encoding);
return await this.uploadFile(buffer, remotePath, {overwrite});
} catch (error) {
throw new Error(`Failed to put file content ${remotePath}: ${error.message}`);
}
}
// 私有方法
/**
* 标准化路径
* 将输入路径转换为相对于 WebDAV 基础路径的路径
* @private
*/
_normalizePath(remotePath) {
if (!remotePath) return '/';
// 确保路径以 / 开头
if (!remotePath.startsWith('/')) {
remotePath = '/' + remotePath;
}
// 清理多余的斜杠
remotePath = remotePath.replace(/\/+/g, '/');
// 获取 WebDAV 基础路径
const baseUrl = new URL(this.config.baseURL);
const basePath = baseUrl.pathname;
// 解码基础路径和输入路径以进行正确比较
const decodedBasePath = decodeURIComponent(basePath);
const decodedRemotePath = decodeURIComponent(remotePath);
// 如果输入路径包含基础路径,则移除基础路径前缀
if (decodedRemotePath.startsWith(decodedBasePath)) {
let relativePath = decodedRemotePath.substring(decodedBasePath.length);
// 确保相对路径以 / 开头
if (!relativePath.startsWith('/')) {
relativePath = '/' + relativePath;
}
// 如果结果只是 /,保持为 /
return relativePath || '/';
}
// 如果输入路径不包含基础路径,直接返回(假设它已经是相对路径)
return remotePath;
}
/**
* 规范化从 WebDAV 响应中获取的路径
* 移除 WebDAV 基础路径前缀,返回相对路径
* @private
*/
_normalizeResponsePath(responsePath) {
if (!responsePath) return '';
// 获取 WebDAV 基础路径(从 baseURL 中提取)
const baseUrl = new URL(this.config.baseURL);
let basePath = baseUrl.pathname;
// 解码 basePath 以便正确比较
try {
basePath = decodeURIComponent(basePath);
} catch (e) {
console.warn('Failed to decode basePath:', basePath);
}
// 添加调试信息
if (Number(process.env.WEBDAV_DEBUG)) {
console.log('_normalizeResponsePath debug:');
console.log(' Response path:', responsePath);
console.log(' Base path:', basePath);
}
// 如果响应路径以基础路径开头,移除它
if (responsePath.startsWith(basePath)) {
let normalizedPath = responsePath.substring(basePath.length);
// 确保路径以 / 开头
if (!normalizedPath.startsWith('/')) {
normalizedPath = '/' + normalizedPath;
}
// 如果结果只是 /,表示这是基础路径本身,返回空字符串
if (normalizedPath === '/') {
normalizedPath = '';
}
if (Number(process.env.WEBDAV_DEBUG)) {
console.log(' Normalized path:', normalizedPath);
}
return normalizedPath;
}
// 如果不以基础路径开头,直接返回原路径
if (Number(process.env.WEBDAV_DEBUG)) {
console.log(' No normalization needed, returning:', responsePath);
}
return responsePath;
}
/**
* 获取绝对 URL
* @private
*/
_getAbsoluteUrl(remotePath) {
const normalizedPath = this._normalizePath(remotePath);
// 确保 baseURL 以斜杠结尾
let baseURL = this.config.baseURL;
if (!baseURL.endsWith('/')) {
baseURL += '/';
}
// 移除 normalizedPath 开头的斜杠,避免双斜杠
const cleanPath = normalizedPath.startsWith('/') ? normalizedPath.substring(1) : normalizedPath;
const absoluteUrl = new URL(cleanPath, baseURL).href;
// 添加调试信息
if (Number(process.env.WEBDAV_DEBUG)) {
console.log('_getAbsoluteUrl debug:');
console.log(' Input remotePath:', remotePath);
console.log(' Normalized path:', normalizedPath);
console.log(' Base URL:', baseURL);
console.log(' Clean path:', cleanPath);
console.log(' Final absolute URL:', absoluteUrl);
}
return absoluteUrl;
}
/**
* 根据文件扩展名获取内容类型
* @private
*/
_getContentType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const mimeTypes = {
'.txt': 'text/plain',
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.xml': 'application/xml',
'.pdf': 'application/pdf',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.mp4': 'video/mp4',
'.mp3': 'audio/mpeg',
'.zip': 'application/zip'
};
return mimeTypes[ext] || 'application/octet-stream';
}
/**
* 解析 WebDAV PROPFIND 响应
* @private
*/
_parseMultiStatus(xmlData, requestPath = null) {
const items = [];
try {
// 使用 cheerio 解析 XML,设置 XML 模式
const $ = cheerio.load(xmlData, {
xmlMode: true,
decodeEntities: true,
lowerCaseAttributeNames: false
});
// 查找所有 response 元素(支持不同的命名空间)
const responseElements = $('response, d\\:response, D\\:response').toArray();
if (responseElements.length === 0) {
console.warn('No response elements found in WebDAV XML');
return this._parseMultiStatusFallback(xmlData);
}
responseElements.forEach((element) => {
const $response = $(element);
// 提取 href(支持不同的命名空间)
let href = $response.find('href, d\\:href, D\\:href').first().text().trim();
if (!href) {
console.warn('No href found in response element');
return;
}
// 解码 URL 编码的路径
try {
href = decodeURIComponent(href);
} catch (e) {
console.warn('Failed to decode href:', href);
}
// 规范化路径 - 移除 WebDAV 基础路径前缀
let normalizedPath = this._normalizeResponsePath(href);
// 如果规范化后的路径为空或者是根路径本身,跳过这个项目
if (!normalizedPath || normalizedPath === '/' || normalizedPath === '') {
return;
}
// 如果有请求路径,排除与请求路径相同的项目(即目录本身)
if (requestPath) {
// 将请求路径也进行相同的规范化处理,以便正确比较
const normalizedRequestPath = this._normalizePath(requestPath);
if (normalizedPath === normalizedRequestPath) {
return;
}
}
// 提取文件/目录名称
let name = path.basename(normalizedPath);
if (!name || name === '/') {
// 对于根目录或空名称,使用路径的最后部分
const pathParts = normalizedPath.split('/').filter(part => part.length > 0);
name = pathParts.length > 0 ? pathParts[pathParts.length - 1] : '/';
}
// 创建基本项目对象
const item = {
name,
path: normalizedPath,
isDirectory: false,
size: 0,
lastModified: null,
contentType: null,
etag: null,
creationDate: null
};
// 查找 propstat 元素(支持不同的命名空间)
const $propstat = $response.find('propstat, d\\:propstat, D\\:propstat').first();
if ($propstat.length === 0) {
console.warn('No propstat found for', href);
return;
}
// 检查状态码
const status = $propstat.find('status, d\\:status, D\\:status').text().trim();
if (status && !status.includes('200')) {
console.warn('Non-200 status for', href, ':', status);
return;
}
const $prop = $propstat.find('prop, d\\:prop, D\\:prop').first();
if ($prop.length === 0) {
console.warn('No prop found for', href);
return;
}
// 检查是否为目录(支持不同的命名空间)
const $resourcetype = $prop.find('resourcetype, d\\:resourcetype, D\\:resourcetype').first();
if ($resourcetype.find('collection, d\\:collection, D\\:collection').length > 0) {
item.isDirectory = true;
}
// 提取文件大小
const contentLength = $prop.find('getcontentlength, d\\:getcontentlength, D\\:getcontentlength').text().trim();
if (contentLength) {
const size = parseInt(contentLength, 10);
if (!isNaN(size)) {
item.size = size;
}
}
// 提取最后修改时间
const lastModified = $prop.find('getlastmodified, d\\:getlastmodified, D\\:getlastmodified').text().trim();
if (lastModified) {
const date = new Date(lastModified);
if (!isNaN(date.getTime())) {
item.lastModified = date;
}
}
// 提取创建时间
const creationDate = $prop.find('creationdate, d\\:creationdate, D\\:creationdate').text().trim();
if (creationDate) {
const date = new Date(creationDate);
if (!isNaN(date.getTime())) {
item.creationDate = date;
}
}
// 提取内容类型
const contentType = $prop.find('getcontenttype, d\\:getcontenttype, D\\:getcontenttype').text().trim();
if (contentType) {
item.contentType = contentType;
}
// 提取 ETag
const etag = $prop.find('getetag, d\\:getetag, D\\:getetag').text().trim();
if (etag) {
item.etag = etag.replace(/"/g, ''); // 移除引号
}
items.push(item);
});
} catch (error) {
console.warn('Failed to parse WebDAV XML response with cheerio, falling back to regex:', error.message);
// 回退到正则表达式解析
return this._parseMultiStatusFallback(xmlData);
}
return items;
}
/**
* 回退的 XML 解析方法(使用正则表达式)
* @private
*/
_parseMultiStatusFallback(xmlData) {
const items = [];
const responseRegex = /<(?:D:)?response[^>]*>([\s\S]*?)<\/(?:D:)?response>/gi;
let match;
while ((match = responseRegex.exec(xmlData)) !== null) {
const responseXml = match[1];
// 提取 href
const hrefMatch = /<(?:D:)?href[^>]*>(.*?)<\/(?:D:)?href>/i.exec(responseXml);
if (!hrefMatch) continue;
const href = decodeURIComponent(hrefMatch[1]);
const name = path.basename(href) || '/';
// 提取属性
const item = {
name,
path: href,
isDirectory: false,
size: 0,
lastModified: null,
contentType: null,
etag: null,
creationDate: null
};
// 检查是否为目录
if (/<(?:D:)?resourcetype[^>]*>[\s\S]*?<(?:D:)?collection[^>]*\/?>[\s\S]*?<\/(?:D:)?resourcetype>/i.test(responseXml)) {
item.isDirectory = true;
}
// 提取文件大小
const sizeMatch = /<(?:D:)?getcontentlength[^>]*>(\d+)<\/(?:D:)?getcontentlength>/i.exec(responseXml);
if (sizeMatch) {
item.size = parseInt(sizeMatch[1], 10);
}
// 提取最后修改时间
const lastModifiedMatch = /<(?:D:)?getlastmodified[^>]*>(.*?)<\/(?:D:)?getlastmodified>/i.exec(responseXml);
if (lastModifiedMatch) {
const date = new Date(lastModifiedMatch[1]);
if (!isNaN(date.getTime())) {
item.lastModified = date;
}
}
// 提取创建时间
const creationDateMatch = /<(?:D:)?creationdate[^>]*>(.*?)<\/(?:D:)?creationdate>/i.exec(responseXml);
if (creationDateMatch) {
const date = new Date(creationDateMatch[1]);
if (!isNaN(date.getTime())) {
item.creationDate = date;
}
}
// 提取内容类型
const contentTypeMatch = /<(?:D:)?getcontenttype[^>]*>(.*?)<\/(?:D:)?getcontenttype>/i.exec(responseXml);
if (contentTypeMatch) {
item.contentType = contentTypeMatch[1];
}
// 提取 ETag
const etagMatch = /<(?:D:)?getetag[^>]*>(.*?)<\/(?:D:)?getetag>/i.exec(responseXml);
if (etagMatch) {
item.etag = etagMatch[1].replace(/"/g, '');
}
items.push(item);
}
return items;
}
}
/**
* 创建 WebDAV 客户端实例
* @param {Object} config - 配置对象
* @returns {WebDAVClient} WebDAV 客户端实例
*/
export function createWebDAVClient(config) {
return new WebDAVClient(config);
}
// 默认导出
export default WebDAVClient;