fix: user-agent format (#27488)

* fix: user-agent format

* ci: fix static analysis

---------

Co-authored-by: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com>
This commit is contained in:
Jason Rasmussen
2026-04-03 12:26:50 -04:00
committed by GitHub
parent 4fcd9c2e0d
commit 207672c481
5 changed files with 42 additions and 9 deletions
+29
View File
@@ -0,0 +1,29 @@
import { getAppVersionFromUA } from 'src/utils/request';
describe(getAppVersionFromUA.name, () => {
it('should get the app version for android', () => {
expect(getAppVersionFromUA('immich-android/1.123.4')).toEqual('1.123.4');
});
it('should get the app version for ios', () => {
expect(getAppVersionFromUA('immich-ios/1.123.4')).toEqual('1.123.4');
});
it('should get the app version for unknown', () => {
expect(getAppVersionFromUA('immich-unknown/1.123.4')).toEqual('1.123.4');
});
describe('legacy format', () => {
it('should get the app version from the old android format', () => {
expect(getAppVersionFromUA('Immich_Android_1.123.4')).toEqual('1.123.4');
});
it('should get the app version from the old ios format', () => {
expect(getAppVersionFromUA('Immich_iOS_1.123.4')).toEqual('1.123.4');
});
it('should get the app version from the old unknown format', () => {
expect(getAppVersionFromUA('Immich_Unknown_1.123.4')).toEqual('1.123.4');
});
});
});
+5 -2
View File
@@ -7,8 +7,11 @@ export const fromChecksum = (checksum: string): Buffer => {
export const fromMaybeArray = <T>(param: T | T[]) => (Array.isArray(param) ? param[0] : param);
const getAppVersionFromUA = (ua: string) =>
ua.match(/^Immich_(?:Android|iOS)_(?<appVersion>.+)$/)?.groups?.appVersion ?? null;
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']);