-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGET.handler.ts
More file actions
85 lines (72 loc) · 2.89 KB
/
Copy pathGET.handler.ts
File metadata and controls
85 lines (72 loc) · 2.89 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
import { WebDavMethodHandler } from '../../types/webdav.types';
import { Request, Response } from 'express';
import { WebDavUtils } from '../../utils/webdav.utils';
import { DriveFileService } from '../../services/drive/drive-file.service';
import { NetworkFacade } from '../../services/network/network-facade.service';
import { DownloadService } from '../../services/network/download.service';
import { CryptoService } from '../../services/crypto.service';
import { AuthService } from '../../services/auth.service';
import { NotFoundError } from '../../utils/errors.utils';
import { webdavLogger } from '../../utils/logger.utils';
import { NetworkUtils } from '../../utils/network.utils';
export class GETRequestHandler implements WebDavMethodHandler {
constructor(
private readonly dependencies: {
driveFileService: DriveFileService;
downloadService: DownloadService;
cryptoService: CryptoService;
authService: AuthService;
networkFacade: NetworkFacade;
},
) {}
handle = async (req: Request, res: Response) => {
const { driveFileService, authService, networkFacade } = this.dependencies;
const resource = await WebDavUtils.getRequestedResource(req.url);
if (resource.name.startsWith('._')) throw new NotFoundError('File not found');
webdavLogger.info(`[GET] Request received item at ${resource.url}`);
const driveFile = await WebDavUtils.getDriveFileFromResource({
url: resource.url,
driveFileService,
});
if (!driveFile) {
throw new NotFoundError(
`Resource not found on Internxt Drive at ${resource.url}, if trying to access a folder use PROPFIND instead.`,
);
}
webdavLogger.info(`[GET] [${driveFile.uuid}] Found Drive File`);
const { user } = await authService.getAuthDetails();
webdavLogger.info(`[GET] [${driveFile.uuid}] Network ready for download`);
const range = req.headers['range'];
const rangeOptions = NetworkUtils.parseRangeHeader({
range,
totalFileSize: driveFile.size,
});
let contentLength = driveFile.size;
if (rangeOptions) {
webdavLogger.info(`[GET] [${driveFile.uuid}] Range request received:`, { rangeOptions });
contentLength = rangeOptions.rangeSize;
}
res.header('Content-Type', 'application/octet-stream');
res.header('Content-length', contentLength.toString());
const writable = new WritableStream({
write(chunk) {
res.write(chunk);
},
close() {
res.end();
},
});
const [executeDownload] = await networkFacade.downloadToStream(
driveFile.bucket,
user.mnemonic,
driveFile.fileId,
contentLength,
writable,
rangeOptions,
);
webdavLogger.info(`[GET] [${driveFile.uuid}] Download prepared, executing...`);
res.status(200);
await executeDownload;
webdavLogger.info(`[GET] [${driveFile.uuid}] ✅ Download ready, replying to client`);
};
}