Skip to content

Commit 59c3aad

Browse files
committed
* make size required in file metadata
* use basename to get file name from file url * lookup content type from file name if no content-type header
1 parent 4cd5b58 commit 59c3aad

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

platform/dist/file-stream.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference types="node" />
22
import { Readable } from "stream";
33
export interface FileMetadata {
4-
size?: number;
4+
size: number;
55
contentType?: string;
66
lastModified?: Date;
77
name?: string;

platform/dist/file-stream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ async function getRemoteFileStreamAndMetadata(url) {
7878
}
7979
const headers = response.headers;
8080
const contentLength = headers.get("content-length");
81-
const contentType = headers.get("content-type") || undefined;
8281
const lastModified = headers.get("last-modified")
8382
? new Date(headers.get("last-modified"))
8483
: undefined;
8584
const etag = headers.get("etag") || undefined;
8685
const urlObj = new URL(url);
87-
const name = urlObj.pathname.split("/").pop() || undefined;
86+
const name = path_1.basename(urlObj.pathname);
87+
const contentType = headers.get("content-type") || mime.lookup(urlObj.pathname) || undefined;
8888
const baseMetadata = {
8989
contentType,
9090
lastModified,

platform/lib/file-stream.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { v4 as uuidv4 } from "uuid";
1212
import * as mime from "mime-types";
1313

1414
export interface FileMetadata {
15-
size?: number;
15+
size: number;
1616
contentType?: string;
1717
lastModified?: Date;
1818
name?: string;
@@ -91,15 +91,15 @@ async function getRemoteFileStreamAndMetadata(url: string): Promise<{ stream: Re
9191

9292
const headers = response.headers;
9393
const contentLength = headers.get("content-length");
94-
const contentType = headers.get("content-type") || undefined;
9594
const lastModified = headers.get("last-modified")
9695
? new Date(headers.get("last-modified")!)
9796
: undefined;
9897
const etag = headers.get("etag") || undefined;
9998
const urlObj = new URL(url);
100-
const name = urlObj.pathname.split("/").pop() || undefined;
99+
const name = basename(urlObj.pathname);
100+
const contentType = headers.get("content-type") || mime.lookup(urlObj.pathname) || undefined;
101101

102-
const baseMetadata: FileMetadata = {
102+
const baseMetadata = {
103103
contentType,
104104
lastModified,
105105
name,
@@ -123,7 +123,7 @@ async function getRemoteFileStreamAndMetadata(url: string): Promise<{ stream: Re
123123
return await downloadToTemporaryFile(response, baseMetadata);
124124
}
125125

126-
async function downloadToTemporaryFile(response: Response, baseMetadata: FileMetadata): Promise<{ stream: Readable; metadata: FileMetadata }> {
126+
async function downloadToTemporaryFile(response: Response, baseMetadata: Partial<FileMetadata>): Promise<{ stream: Readable; metadata: FileMetadata }> {
127127
// Generate unique temporary file path
128128
const tempFileName = `file-stream-${uuidv4()}`;
129129
const tempFilePath = join(tmpdir(), tempFileName);

0 commit comments

Comments
 (0)