mirror of
https://github.com/directus/directus.git
synced 2026-02-12 14:35:26 -05:00
Autoconvert assets if browser supports it (#18012)
Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch> Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -1,70 +1,29 @@
|
||||
import { isNil } from 'lodash-es';
|
||||
import type {
|
||||
File,
|
||||
Transformation,
|
||||
TransformationParams,
|
||||
TransformationPreset,
|
||||
TransformationPresetFormat,
|
||||
TransformationPresetResize,
|
||||
} from '../types/index.js';
|
||||
import type { File, Transformation, TransformationParams } from '../types/index.js';
|
||||
|
||||
// Extract transforms from a preset
|
||||
export function resolvePreset(input: TransformationParams | TransformationPreset, file: File): Transformation[] {
|
||||
// Do the format conversion last
|
||||
return [extractResize(input), ...(input.transforms ?? []), extractToFormat(input, file)].filter(
|
||||
(transform): transform is Transformation => transform !== undefined
|
||||
);
|
||||
}
|
||||
export function resolvePreset(input: TransformationParams, file: File): Transformation[] {
|
||||
const transforms = input.transforms ?? [];
|
||||
|
||||
function extractOptions<T extends Record<string, any>>(
|
||||
keys: (keyof T)[],
|
||||
numberKeys: (keyof T)[] = [],
|
||||
booleanKeys: (keyof T)[] = []
|
||||
) {
|
||||
return function (input: TransformationParams | TransformationPreset): T {
|
||||
return Object.entries(input).reduce(
|
||||
(config, [key, value]) =>
|
||||
keys.includes(key as any) && isNil(value) === false
|
||||
? {
|
||||
...config,
|
||||
[key]: numberKeys.includes(key as any)
|
||||
? +value!
|
||||
: booleanKeys.includes(key as any)
|
||||
? Boolean(value)
|
||||
: value,
|
||||
}
|
||||
: config,
|
||||
{} as T
|
||||
);
|
||||
};
|
||||
}
|
||||
if (input.format || input.quality)
|
||||
transforms.push([
|
||||
'toFormat',
|
||||
input.format || (file.type!.split('/')[1] as any),
|
||||
{
|
||||
quality: input.quality ? Number(input.quality) : undefined,
|
||||
},
|
||||
]);
|
||||
|
||||
// Extract format transform from a preset
|
||||
function extractToFormat(input: TransformationParams | TransformationPreset, file: File): Transformation | undefined {
|
||||
const options = extractOptions<TransformationPresetFormat>(['format', 'quality'], ['quality'])(input);
|
||||
return Object.keys(options).length > 0
|
||||
? [
|
||||
'toFormat',
|
||||
options.format || (file.type!.split('/')[1] as any),
|
||||
{
|
||||
quality: options.quality,
|
||||
},
|
||||
]
|
||||
: undefined;
|
||||
}
|
||||
if (input.width || input.height)
|
||||
transforms.push([
|
||||
'resize',
|
||||
{
|
||||
width: input.width ? Number(input.width) : undefined,
|
||||
height: input.height ? Number(input.height) : undefined,
|
||||
fit: input.fit,
|
||||
withoutEnlargement: input.withoutEnlargement ? Boolean(input.withoutEnlargement) : undefined,
|
||||
},
|
||||
]);
|
||||
|
||||
function extractResize(input: TransformationParams | TransformationPreset): Transformation | undefined {
|
||||
const resizable = ['width', 'height'].some((key) => key in input);
|
||||
if (!resizable) return undefined;
|
||||
|
||||
return [
|
||||
'resize',
|
||||
extractOptions<TransformationPresetResize>(
|
||||
['width', 'height', 'fit', 'withoutEnlargement'],
|
||||
['width', 'height'],
|
||||
['withoutEnlargement']
|
||||
)(input),
|
||||
];
|
||||
return transforms;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user