mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat: add conditional caching and allow-same-origin to file preview, add PreviewHtmlButton
This commit is contained in:
1 parent
7f9403ce5e
commit
ba08f5c24a
10 files changed
+155
-63
No files matched your search
@@ -57,19 +57,50 @@ function isPreviewableMimeType(mimeType?: string): boolean {
|
||||
return mimeType !== undefined && PREVIEWABLE_MIME_TYPES.has(mimeType)
|
||||
}
|
||||
|
||||
function getPreviewHeaders(result: { name: string; size: number; mimeType?: string }): Record<string, string> {
|
||||
function buildPreviewValidators(stat: { size: number; lastModified: Date }): { etag: string; lastModified: string } {
|
||||
return {
|
||||
etag: `"${stat.size.toString(16)}-${stat.lastModified.getTime().toString(16)}"`,
|
||||
lastModified: stat.lastModified.toUTCString(),
|
||||
}
|
||||
}
|
||||
|
||||
function isPreviewFresh(c: Context, validators: { etag: string; lastModified: string }): boolean {
|
||||
const ifNoneMatch = c.req.header('if-none-match')
|
||||
if (ifNoneMatch !== undefined) {
|
||||
return ifNoneMatch.split(',').some((tag: string) => tag.trim() === validators.etag)
|
||||
}
|
||||
|
||||
const ifModifiedSince = c.req.header('if-modified-since')
|
||||
if (ifModifiedSince !== undefined) {
|
||||
const since = Date.parse(ifModifiedSince)
|
||||
return !Number.isNaN(since) && Date.parse(validators.lastModified) <= since
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
function getPreviewHeaders(result: {
|
||||
name: string
|
||||
size: number
|
||||
mimeType?: string
|
||||
lastModified: Date
|
||||
}): Record<string, string> {
|
||||
const isHtmlSvg = result.mimeType === 'text/html' || result.mimeType === 'image/svg+xml'
|
||||
const validators = buildPreviewValidators(result)
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': result.mimeType || 'application/octet-stream',
|
||||
'Content-Length': result.size.toString(),
|
||||
'Content-Disposition': `inline; filename="${result.name}"`,
|
||||
'X-Content-Type-Options': 'nosniff',
|
||||
'Referrer-Policy': 'no-referrer',
|
||||
'Cache-Control': 'private, must-revalidate, max-age=0',
|
||||
'ETag': validators.etag,
|
||||
'Last-Modified': validators.lastModified,
|
||||
}
|
||||
|
||||
if (isHtmlSvg) {
|
||||
headers['Content-Security-Policy'] = [
|
||||
'sandbox allow-scripts;',
|
||||
'sandbox allow-scripts allow-same-origin;',
|
||||
"default-src 'self' http: https: data: blob: 'unsafe-inline' 'unsafe-eval';",
|
||||
"script-src 'self' http: https: 'unsafe-inline' 'unsafe-eval';",
|
||||
"style-src 'self' http: https: 'unsafe-inline';",
|
||||
@@ -100,18 +131,23 @@ export function createFileRoutes() {
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await fileService.getFile(userPath)
|
||||
const stat = await fileService.getFilePreviewStat(userPath)
|
||||
|
||||
if (result.isDirectory) {
|
||||
if (stat.isDirectory) {
|
||||
return c.json({ error: 'Cannot preview directories' }, 400)
|
||||
}
|
||||
|
||||
if (!isPreviewableMimeType(result.mimeType)) {
|
||||
if (!isPreviewableMimeType(stat.mimeType)) {
|
||||
return c.json({ error: 'File type cannot be previewed' }, 415)
|
||||
}
|
||||
|
||||
const headers = getPreviewHeaders(stat)
|
||||
|
||||
if (isPreviewFresh(c, buildPreviewValidators(stat))) {
|
||||
return new Response(null, { status: 304, headers })
|
||||
}
|
||||
|
||||
const content = await fileService.getRawFileContent(userPath)
|
||||
const headers = getPreviewHeaders(result)
|
||||
|
||||
return new Response(content, { headers })
|
||||
} catch (error: unknown) {
|
||||
|
||||
@@ -61,6 +61,33 @@ export async function getRawFileContent(userPath: string): Promise<Buffer> {
|
||||
}
|
||||
}
|
||||
|
||||
export interface FilePreviewStat {
|
||||
isDirectory: boolean
|
||||
name: string
|
||||
size: number
|
||||
mimeType: string
|
||||
lastModified: Date
|
||||
}
|
||||
|
||||
export async function getFilePreviewStat(userPath: string): Promise<FilePreviewStat> {
|
||||
const validatedPath = validatePath(userPath)
|
||||
|
||||
const exists = await fileExists(validatedPath)
|
||||
if (!exists) {
|
||||
throw { message: 'File not found or cannot be read', statusCode: 404 }
|
||||
}
|
||||
|
||||
const stats = await getFileStats(validatedPath)
|
||||
|
||||
return {
|
||||
isDirectory: stats.isDirectory,
|
||||
name: path.basename(validatedPath),
|
||||
size: stats.size,
|
||||
mimeType: getMimeType(validatedPath),
|
||||
lastModified: stats.lastModified,
|
||||
}
|
||||
}
|
||||
|
||||
export async function getFile(userPath: string): Promise<FileInfo> {
|
||||
const validatedPath = validatePath(userPath)
|
||||
logger.info(`Getting file for path: ${userPath} -> ${validatedPath}`)
|
||||
|
||||
Reference in new issue
Block a user