-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathwebdav.utils.ts
More file actions
130 lines (112 loc) · 4.54 KB
/
Copy pathwebdav.utils.ts
File metadata and controls
130 lines (112 loc) · 4.54 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
import path from 'node:path';
import { createHash } from 'node:crypto';
import { WebDavRequestedResource } from '../types/webdav.types';
import { DriveFileItem, DriveFolderItem, DriveItem } from '../types/drive.types';
import { DriveItemService } from '../services/drive/drive-item.service';
import { webdavLogger } from './logger.utils';
import { ConfigService } from '../services/config.service';
import { TrashService } from '../services/drive/trash.service';
import { FormatUtils } from './format.utils';
import { DriveItemRepository } from '../services/database/drive-item/drive-item.repository';
export class WebDavUtils {
static joinURL(...pathComponents: string[]): string {
return path.posix.join(...pathComponents);
}
static removeHostFromURL(completeURL: string) {
// add a temp http schema if its not present
if (!completeURL.startsWith('/') && !/^https?:\/\//i.test(completeURL)) {
completeURL = 'https://' + completeURL;
}
const parsedUrl = new URL(completeURL);
let url = parsedUrl.href.replace(parsedUrl.origin + '/', '');
if (!url.startsWith('/')) url = '/'.concat(url);
return url;
}
static decodeUrl(requestUrl: string, decodeUri = true): string {
return (decodeUri ? decodeURIComponent(requestUrl) : requestUrl).replaceAll('/./', '/');
}
static normalizeFolderPath(path: string): string {
let normalizedPath = path;
if (!normalizedPath.startsWith('/')) {
normalizedPath = `/${normalizedPath}`;
}
if (!normalizedPath.endsWith('/')) {
normalizedPath = `${normalizedPath}/`;
}
return normalizedPath;
}
static async getRequestedResource(requestUrl: string, decodeUri = true): Promise<WebDavRequestedResource> {
const decodedUrl = this.decodeUrl(requestUrl, decodeUri);
const parsedPath = path.parse(decodedUrl);
const parentPath = this.normalizeFolderPath(path.dirname(decodedUrl));
return {
url: decodedUrl,
name: parsedPath.base,
path: parsedPath,
parentPath,
};
}
static async getDriveFileFromResource(url: string): Promise<DriveFileItem | undefined> {
try {
return await DriveItemService.instance.getFileByPath(url);
} catch {
// no op
}
}
static async getDriveFolderFromResource(url: string): Promise<DriveFolderItem | undefined> {
try {
return await DriveItemService.instance.getFolderByPath(url);
} catch {
// no op
}
}
static async getDriveItemFromResource(resource: WebDavRequestedResource): Promise<DriveItem | undefined> {
let item: DriveItem | undefined = undefined;
const isFolder = resource.url.endsWith('/');
try {
if (isFolder) {
item = await DriveItemService.instance.getFolderByPath(resource.url);
} else {
try {
item = await DriveItemService.instance.getFileByPath(resource.url);
} catch {
item = await DriveItemService.instance.getFolderByPath(resource.url);
}
}
} catch {
//no op
}
return item;
}
static async deleteOrTrashItem<T extends { itemType: 'file' | 'folder'; uuid: string }>(driveItem: T) {
const configs = await ConfigService.instance.readWebdavConfig();
const type = FormatUtils.capitalizeFirstLetter(driveItem.itemType);
if (configs.deleteFilesPermanently) {
webdavLogger.info(`[DELETE] [${driveItem.uuid}] Deleting permanently ${driveItem.itemType}`);
await TrashService.instance.deleteItemPermanently(driveItem.itemType, driveItem.uuid);
webdavLogger.info(`[DELETE] [${driveItem.uuid}] ${type} deleted permanently successfully`);
} else {
webdavLogger.info(`[DELETE] [${driveItem.uuid}] Trashing ${driveItem.itemType}`);
await TrashService.instance.trashItems({
items: [{ type: driveItem.itemType, uuid: driveItem.uuid }],
});
webdavLogger.info(`[DELETE] [${driveItem.uuid}] ${type} trashed successfully`);
}
await DriveItemRepository.instance.delete([driveItem.uuid]);
}
static generateETag(parts: Array<string | number | Date | null | undefined>): string {
const normalized = parts.map((part) => (part instanceof Date ? part.getTime() : (part ?? '')));
const hash = createHash('sha256').update(normalized.join('|')).digest('hex');
return `"${hash}"`;
}
static getItemETag(driveItem: DriveFileItem | DriveFolderItem): string {
return this.generateETag([
driveItem.uuid,
driveItem.itemType === 'file' ? driveItem.size : undefined,
driveItem.createdAt,
driveItem.updatedAt,
driveItem.creationTime,
driveItem.modificationTime,
]);
}
}