#116 Support custom URL links (slug-type links)
This commit is contained in:
+6
-1
@@ -156,12 +156,13 @@ class Immich {
|
||||
if (res.status === 200) {
|
||||
// Normal response - get the shared assets
|
||||
link = jsonBody as SharedLink
|
||||
link.keyType = keyType
|
||||
|
||||
// For an album, we need to make a second request to Immich to populate
|
||||
// the array of assets
|
||||
if (link.type === AlbumType.album) {
|
||||
const albumRes = await fetch(this.buildUrl(this.apiUrl() + '/albums/' + link?.album?.id, {
|
||||
key,
|
||||
[keyType]: key,
|
||||
password
|
||||
}))
|
||||
const album = await albumRes.json() as Album
|
||||
@@ -310,6 +311,10 @@ class Immich {
|
||||
return size as ImageSize
|
||||
}
|
||||
}
|
||||
|
||||
getKeyTypeFromShare (shareType: string) {
|
||||
return shareType === 's' ? KeyType.slug : KeyType.key
|
||||
}
|
||||
}
|
||||
|
||||
const immich = new Immich()
|
||||
|
||||
+21
-22
@@ -7,7 +7,7 @@ import crypto from 'crypto'
|
||||
import render from './render'
|
||||
import dayjs from 'dayjs'
|
||||
import { NextFunction, Request, Response } from 'express-serve-static-core'
|
||||
import { AssetType, ImageSize, KeyType } from './types'
|
||||
import { Asset, AssetType, ImageSize, KeyType } from './types'
|
||||
import { addResponseHeaders, getConfigOption, log, toString } from './functions'
|
||||
import { decrypt, encrypt } from './encrypt'
|
||||
import { respondToInvalidRequest } from './invalidRequestHandler'
|
||||
@@ -75,13 +75,20 @@ app.get(/^(|\/share)\/healthcheck$/, async (_req, res) => {
|
||||
* [ROUTE] This is the main URL that someone would visit if they are opening a shared link
|
||||
*/
|
||||
app.get('/:shareType(share|s)/:key/:mode(download)?', decodeCookie, async (req, res) => {
|
||||
await immich.handleShareRequest({
|
||||
req,
|
||||
key: req.params.key,
|
||||
keyType: req.params.shareType === 's' ? KeyType.slug : KeyType.key,
|
||||
mode: req.params.mode,
|
||||
password: req.password
|
||||
}, res)
|
||||
const keyType = immich.getKeyTypeFromShare(req.params.shareType)
|
||||
|
||||
if (keyType === KeyType.slug && !getConfigOption('ipp.allowSlugLinks', true)) {
|
||||
// Slug type links are not allowed
|
||||
respondToInvalidRequest(res, 404)
|
||||
} else {
|
||||
await immich.handleShareRequest({
|
||||
req,
|
||||
key: req.params.key,
|
||||
keyType,
|
||||
mode: req.params.mode,
|
||||
password: req.password
|
||||
}, res)
|
||||
}
|
||||
})
|
||||
|
||||
/*
|
||||
@@ -120,25 +127,17 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, async (req, r
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the shared link information from Immich, so we can check to make sure that the requested asset
|
||||
// is allowed by this shared link.
|
||||
const sharedLink = (await immich.getShareByKey(req.params.key, req.password))?.link
|
||||
const request = {
|
||||
req,
|
||||
key: req.params.key,
|
||||
range: req.headers.range || ''
|
||||
}
|
||||
if (sharedLink?.assets.length) {
|
||||
// Check that the requested asset exists in this share
|
||||
const asset = sharedLink.assets.find(x => x.id === req.params.id)
|
||||
if (asset) {
|
||||
asset.type = req.params.type === 'video' ? AssetType.video : AssetType.image
|
||||
render.assetBuffer(request, res, asset, req.params.size).then()
|
||||
}
|
||||
} else {
|
||||
log('No asset found for ' + req.path)
|
||||
respondToInvalidRequest(res, 404)
|
||||
}
|
||||
const asset = {
|
||||
id: req.params.id,
|
||||
key: req.params.key,
|
||||
type: req.params.type === 'video' ? AssetType.video : AssetType.image
|
||||
} as Asset
|
||||
render.assetBuffer(request, res, asset, req.params.size).then()
|
||||
})
|
||||
|
||||
/*
|
||||
|
||||
@@ -16,6 +16,18 @@ class Render {
|
||||
* Stream data from Immich back to the client
|
||||
*/
|
||||
async assetBuffer (req: IncomingShareRequest, res: Response, asset: Asset, size?: ImageSize | string) {
|
||||
// Get meta info regarding the asset
|
||||
const metaRes = await fetch(immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id), {
|
||||
key: asset.key
|
||||
}))
|
||||
const meta = await metaRes.json()
|
||||
|
||||
// Make sure we should display this asset
|
||||
if (meta.isTrashed || meta.visibility === 'locked') {
|
||||
respondToInvalidRequest(res, 404)
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare the request
|
||||
const headerList = ['content-type', 'content-length', 'last-modified', 'etag']
|
||||
size = immich.validateImageSize(size)
|
||||
|
||||
@@ -38,6 +38,7 @@ export interface Album {
|
||||
|
||||
export interface SharedLink {
|
||||
key: string;
|
||||
keyType: KeyType;
|
||||
type: string;
|
||||
description?: string;
|
||||
assets: Asset[];
|
||||
|
||||
Reference in New Issue
Block a user