mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
207672c481
* fix: user-agent format * ci: fix static analysis --------- Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
26 lines
999 B
TypeScript
26 lines
999 B
TypeScript
import { IncomingHttpHeaders } from 'node:http';
|
|
import { UAParser } from 'ua-parser-js';
|
|
|
|
export const fromChecksum = (checksum: string): Buffer => {
|
|
return Buffer.from(checksum, checksum.length === 28 ? 'base64' : 'hex');
|
|
};
|
|
|
|
export const fromMaybeArray = <T>(param: T | T[]) => (Array.isArray(param) ? param[0] : param);
|
|
|
|
export const getAppVersionFromUA = (ua: string) =>
|
|
ua.match(/^immich-(?:android|ios|unknown)\/(?<appVersion>.+)$/)?.groups?.appVersion ??
|
|
// legacy format
|
|
ua.match(/^Immich_(?:Android|iOS|Unknown)_(?<appVersion>.+)$/)?.groups?.appVersion ??
|
|
null;
|
|
|
|
export const getUserAgentDetails = (headers: IncomingHttpHeaders) => {
|
|
const userAgent = UAParser(headers['user-agent']);
|
|
const appVersion = getAppVersionFromUA(headers['user-agent'] ?? '');
|
|
|
|
return {
|
|
deviceType: userAgent.browser.name || userAgent.device.type || (headers['devicemodel'] as string) || '',
|
|
deviceOS: userAgent.os.name || (headers['devicetype'] as string) || '',
|
|
appVersion,
|
|
};
|
|
};
|