Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions lib/utils/resolve-file-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ import { normalizePath } from "./normalize-path"
export async function resolveFileProxy(
proxy: FileProxy,
file_path: string,
opts: { attachment?: boolean } = {},
): Promise<Response> {
const normalizedPath = normalizePath(file_path)
const pattern = proxy.matching_pattern
const attachment = opts.attachment ?? true

// Extract the relative path after the pattern prefix
// Pattern: "prefix/*" -> prefix is "prefix/"
const prefix = pattern.slice(0, -1) // Remove "*" to get "prefix/"
const relativePath = normalizedPath.slice(prefix.length)

if (proxy.proxy_type === "disk") {
return resolveDiskProxy(proxy.disk_path, relativePath)
return resolveDiskProxy(proxy.disk_path, relativePath, { attachment })
} else {
return resolveHttpProxy(proxy.http_target_url, relativePath)
return resolveHttpProxy(proxy.http_target_url, relativePath, { attachment })
}
}

async function resolveDiskProxy(
diskPath: string,
relativePath: string,
opts: { attachment: boolean },
): Promise<Response> {
const fullPath = join(diskPath, relativePath)

Expand All @@ -33,13 +36,15 @@ async function resolveDiskProxy(
const fileName = relativePath.split("/").pop() || "file"
const contentType = getContentType(fileName)

return new Response(content, {
headers: {
"Content-Type": contentType,
"Content-Disposition": `attachment; filename="${fileName}"`,
"Content-Length": content.byteLength.toString(),
},
const headers = new Headers({
"Content-Type": contentType,
"Content-Length": content.byteLength.toString(),
})
if (opts.attachment) {
headers.set("Content-Disposition", `attachment; filename="${fileName}"`)
}

return new Response(content, { headers })
} catch (error: any) {
if (error.code === "ENOENT") {
return new Response("File not found", { status: 404 })
Expand All @@ -52,6 +57,7 @@ async function resolveDiskProxy(
async function resolveHttpProxy(
httpTargetUrl: string,
relativePath: string,
opts: { attachment: boolean },
): Promise<Response> {
// Ensure the URL doesn't have double slashes
const baseUrl = httpTargetUrl.endsWith("/")
Expand Down Expand Up @@ -79,7 +85,9 @@ async function resolveHttpProxy(
headers.set("Content-Length", contentLength)
}
const fileName = relativePath.split("/").pop() || "file"
headers.set("Content-Disposition", `attachment; filename="${fileName}"`)
if (opts.attachment) {
headers.set("Content-Disposition", `attachment; filename="${fileName}"`)
}

return new Response(response.body, {
status: response.status,
Expand Down
2 changes: 1 addition & 1 deletion routes/files/static/[[...file_path]].ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default withRouteSpec({
// Check if there's a matching proxy
const proxy = ctx.db.matchFileProxy(normalizedPath)
if (proxy) {
return resolveFileProxy(proxy, normalizedPath)
return resolveFileProxy(proxy, normalizedPath, { attachment: false })
}
return new Response("File not found", { status: 404 })
}
Expand Down
24 changes: 24 additions & 0 deletions tests/routes/file-proxy02.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ test("disk proxy with query param download", async () => {
}
})

test("disk proxy static route does not force attachment download", async () => {
const { axios } = await getTestServer()

const tempDir = await mkdtemp(join(tmpdir(), "file-proxy-static-test-"))

try {
await writeFile(join(tempDir, "inline.txt"), "Inline proxy content")

await axios.post("/file_proxies/create", {
proxy_type: "disk",
disk_path: tempDir,
matching_pattern: "static-disk/*",
})

const staticRes = await axios.get("/files/static/static-disk/inline.txt")
expect(staticRes.status).toBe(200)
expect(staticRes.data).toBe("Inline proxy content")
expect(staticRes.headers.get("content-type")).toBe("text/plain")
expect(staticRes.headers.get("content-disposition")).toBeNull()
} finally {
await rm(tempDir, { recursive: true, force: true })
}
})

test("disk proxy binary file", async () => {
const { axios } = await getTestServer()

Expand Down
21 changes: 21 additions & 0 deletions tests/routes/file-proxy03.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ test("http proxy with query param download", async () => {
expect(downloadRes.data).toBe("Query param HTTP proxy test")
})

test("http proxy static route does not force attachment download", async () => {
const { axios, url } = await getTestServer()

await axios.post("/files/upsert", {
file_path: "/static-source/inline.txt",
text_content: "Inline HTTP proxy content",
})

await axios.post("/file_proxies/create", {
proxy_type: "http",
http_target_url: `${url}/files/static/static-source`,
matching_pattern: "http-static/*",
})

const staticRes = await axios.get("/files/static/http-static/inline.txt")
expect(staticRes.status).toBe(200)
expect(staticRes.data).toBe("Inline HTTP proxy content")
expect(staticRes.headers.get("content-type")).toBe("text/plain")
expect(staticRes.headers.get("content-disposition")).toBeNull()
})

test("http proxy binary file", async () => {
const { axios, url } = await getTestServer()

Expand Down
Loading