Skip to content

Commit b7f373d

Browse files
committed
chore(backend:files): improve downloadFile typing with getContentInfo overload
1 parent 47af28b commit b7f373d

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { SpaceEnv } from '../../spaces/models/space-env.model'
2+
3+
export interface DownloadFileContentInfo {
4+
contentLength: number | null
5+
contentType: string
6+
lastModified: string | undefined
7+
}
8+
9+
export interface DownloadFileOptions {
10+
space?: SpaceEnv
11+
getContentInfo?: boolean
12+
}

backend/src/applications/files/utils/download-file.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { HttpStatus } from '@nestjs/common'
77
import { FileTaskEvent } from '../events/file-events'
88
import { FILE_OPERATION } from '../constants/operations'
99
import { writeFromStream } from './files'
10-
import { DownloadFileDto } from '../dto/file-operations.dto'
10+
import type { DownloadFileDto } from '../dto/file-operations.dto'
11+
import type { DownloadFileContentInfo, DownloadFileOptions } from '../interfaces/download-file.interface'
1112

1213
const parts = [
1314
// IPv4 loopback (127.0.0.0/8)
@@ -39,8 +40,20 @@ export async function downloadFile(
3940
http: HttpService,
4041
downloadDto: DownloadFileDto,
4142
dstPath: string,
42-
options?: { space?: SpaceEnv; getContentInfo?: boolean }
43-
) {
43+
options: { space?: SpaceEnv; getContentInfo: true }
44+
): Promise<DownloadFileContentInfo>
45+
export async function downloadFile(
46+
http: HttpService,
47+
downloadDto: DownloadFileDto,
48+
dstPath: string,
49+
options?: { space?: SpaceEnv; getContentInfo?: false | undefined }
50+
): Promise<void>
51+
export async function downloadFile(
52+
http: HttpService,
53+
downloadDto: DownloadFileDto,
54+
dstPath: string,
55+
options?: DownloadFileOptions
56+
): Promise<void | DownloadFileContentInfo> {
4457
// dto must be validated by the caller
4558
const headRes: AxiosResponse = await http.axiosRef({ method: HTTP_METHOD.HEAD, url: downloadDto.url, maxRedirects: 1 })
4659
if (regExpPrivateIP.test(headRes.request.socket.remoteAddress)) {
@@ -51,7 +64,11 @@ export async function downloadFile(
5164
// attempt to retrieve the Content-Length header
5265
const contentLength = 'content-length' in headRes.headers ? Number(headRes.headers['content-length']) || null : null
5366
if (options?.getContentInfo) {
54-
return { contentLength: contentLength, contentType: `${headRes.headers['content-type']}`, lastModified: headRes.headers['last-modified'] }
67+
return {
68+
contentLength: contentLength,
69+
contentType: `${headRes.headers['content-type']}`,
70+
lastModified: headRes.headers['last-modified'] as string | undefined
71+
} satisfies DownloadFileContentInfo
5572
}
5673

5774
if (!contentLength) {

0 commit comments

Comments
 (0)