Move static assets to shared location
This commit is contained in:
+11
-9
@@ -10,6 +10,7 @@ import {
|
||||
Asset,
|
||||
AssetType,
|
||||
asyncHandler,
|
||||
CORE_PUBLIC_DIR,
|
||||
decodeCookie,
|
||||
enforceMinimumImmichVersion,
|
||||
errorHandler,
|
||||
@@ -65,17 +66,18 @@ app.use(sessionMiddleware({ name: 'session' }))
|
||||
app.use(express.json())
|
||||
// For parsing the selective-download form POST (form-encoded body)
|
||||
app.use(express.urlencoded({ extended: false, limit: '1mb' }))
|
||||
/** Serve IPP's own `public` folder, then core's, at `path`. */
|
||||
function serveStatic (path: string, options: Parameters<typeof express.static>[1] = {}) {
|
||||
for (const dir of ['public', CORE_PUBLIC_DIR]) {
|
||||
app.use(path, express.static(dir, { ...options, setHeaders: addResponseHeaders }))
|
||||
}
|
||||
}
|
||||
// Cache-busted, immutable static assets under a per-release version segment.
|
||||
const inProduction = process.env.NODE_ENV === 'production'
|
||||
app.use('/share/static/' + ASSET_VERSION, express.static('public', {
|
||||
immutable: inProduction,
|
||||
maxAge: inProduction ? '365d' : 0,
|
||||
setHeaders: addResponseHeaders
|
||||
}))
|
||||
// Serve static assets from the 'public' folder as /share/static
|
||||
app.use('/share/static', express.static('public', { setHeaders: addResponseHeaders }))
|
||||
// Serve the same assets on /, to allow for /robots.txt and /favicon.ico
|
||||
app.use(express.static('public', { setHeaders: addResponseHeaders }))
|
||||
serveStatic('/share/static/' + ASSET_VERSION, { immutable: inProduction, maxAge: inProduction ? '365d' : 0 })
|
||||
serveStatic('/share/static')
|
||||
// The same assets on /, to allow for /robots.txt and /favicon.ico
|
||||
serveStatic('/')
|
||||
// Remove the X-Powered-By ExpressJS header
|
||||
app.disable('x-powered-by')
|
||||
|
||||
|
||||
+5
-11
@@ -1,4 +1,4 @@
|
||||
import { Asset, log, readableFromWeb, respondToInvalidRequest, SharedLink } from '@ipp/core'
|
||||
import { abortOnClose, Asset, log, readableFromWeb, respondToInvalidRequest, SharedLink } from '@ipp/core'
|
||||
import {
|
||||
assetFetchUrl,
|
||||
authHeadersForAsset,
|
||||
@@ -20,14 +20,8 @@ import { pipeline } from 'stream/promises'
|
||||
* to serve them, and the upstream failure surfaces as a client 404.
|
||||
*/
|
||||
export async function assetBuffer (req: IncomingShareRequest, res: Response, asset: Asset, size?: ImageSize | string, share?: SharedLink, forceVideoPlayback = false) {
|
||||
/*
|
||||
Abort the upstream fetch as soon as the visitor goes away, so a cancelled
|
||||
download doesn't leave Immich streaming #288.
|
||||
*/
|
||||
const upstream = new AbortController()
|
||||
const onClose = () => { if (!res.writableFinished) upstream.abort() }
|
||||
res.once('close', onClose)
|
||||
if (res.closed) onClose()
|
||||
// A cancelled download must not leave Immich streaming #288
|
||||
const upstream = abortOnClose(res)
|
||||
|
||||
const headerList = ['content-type', 'content-length', 'last-modified', 'etag']
|
||||
const fetchHeaders: Record<string, string> = {}
|
||||
@@ -78,9 +72,9 @@ export async function assetBuffer (req: IncomingShareRequest, res: Response, ass
|
||||
const reqHeaders = await authHeadersForAsset(asset)
|
||||
let data: globalThis.Response
|
||||
try {
|
||||
data = await fetch(url, { headers: { ...fetchHeaders, ...reqHeaders }, signal: upstream.signal })
|
||||
data = await fetch(url, { headers: { ...fetchHeaders, ...reqHeaders }, signal: upstream })
|
||||
} catch (e) {
|
||||
if (upstream.signal.aborted) return // visitor left before Immich answered
|
||||
if (upstream.aborted) return // visitor left before Immich answered
|
||||
throw e
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,7 @@ export function Gallery (props: GalleryProps) {
|
||||
<meta name="twitter:card" content="summary_large_image"/>
|
||||
</>}
|
||||
<link rel="icon" href="/share/static/favicon.ico" type="image/x-icon"/>
|
||||
<link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/theme.css`}/>
|
||||
<link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/style.css`}/>
|
||||
<link type="text/css" rel="stylesheet" href="/share/static/photoswipe/photoswipe.css"/>
|
||||
<link type="text/css" rel="stylesheet" href={`/share/static/${ASSET_VERSION}/photoswipe-overrides.css`}/>
|
||||
|
||||
Reference in New Issue
Block a user