diff --git a/package.json b/package.json index 01add88d..9f65fe02 100644 --- a/package.json +++ b/package.json @@ -149,6 +149,7 @@ "json-analyzer": "^1.2.2", "json2csharp": "^1.0.3", "js-base64": "^3.7.7", + "ical.js": "^2.0.1", "json5": "^2.2.3", "jsonpath": "^1.1.1", "jsonar-mod": "^1.9.0", @@ -200,6 +201,7 @@ "pp-qr-code": "^0.6.3", "qrcode": "^1.5.1", "qrcode-terminal-nooctal": "^0.12.1", + "qrcode-parser": "^2.1.3", "sql-formatter": "^13.0.0", "svg2png-wasm": "^1.4.1", "svg-to-url": "^4.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8758559a..9925b406 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -258,7 +258,7 @@ dependencies: specifier: ^8.0.0 version: 8.0.1(@types/luxon@3.4.2)(@types/node@18.15.11)(luxon@3.5.0) ical.js: - specifier: ^2.1.0 + specifier: ^2.0.1 version: 2.1.0 iconv-lite: specifier: ^0.6.3 @@ -401,6 +401,9 @@ dependencies: qrcode: specifier: ^1.5.1 version: 1.5.1 + qrcode-parser: + specifier: ^2.1.3 + version: 2.1.3 qrcode-terminal-nooctal: specifier: ^0.12.1 version: 0.12.1 @@ -10083,7 +10086,6 @@ packages: /jsesc@2.5.2: resolution: {integrity: sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==} engines: {node: '>=4'} - hasBin: true /jsesc@3.0.2: resolution: {integrity: sha512-xKqzzWXDttJuOcawBt4KnKHHIf5oQ/Cxax+0PWFG+DFDgHNAdi+TXECADI+RYiFUMmx8792xsMbbgXj4CwnP4g==} @@ -10201,6 +10203,10 @@ packages: hasBin: true dev: false + /jsqr@1.4.0: + resolution: {integrity: sha512-dxLob7q65Xg2DvstYkRpkYtmKm2sPJ9oFhrhmudT1dZvNFFTlroai3AWSpLey/w5vMcLBXRgOJsbXpdN9HzU/A==} + dev: false + /jwt-decode@3.1.2: resolution: {integrity: sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A==} dev: false @@ -11993,6 +11999,12 @@ packages: resolution: {integrity: sha512-HM7yY8O2ilqhmULxGMpcHSF1EhJJ9yBj8gvDEuZ6M+KGJ0YY2hKpnXvRD+hZPLrDVck3ExIGhmPtSdcjC+guuw==} dev: false + /qrcode-parser@2.1.3: + resolution: {integrity: sha512-tyakoHUQXCjH1+RGiqxH3/6XqbQuXuSaW0CkUp1AlYT0+XA4ndG7bxxyyWpdnr0Z2Vuv0GRwgKSq6sOzNiQfog==} + dependencies: + jsqr: 1.4.0 + dev: false + /qrcode-terminal-nooctal@0.12.1: resolution: {integrity: sha512-jy/kkD0iIMDjTucB+5T6KBsnirlhegDH47vHgrj5MejchSQmi/EAMM0xMFeePgV9CJkkAapNakpVUWYgHvtdKg==} hasBin: true diff --git a/src/tools/index.ts b/src/tools/index.ts index 38a99126..d87541dc 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -69,6 +69,7 @@ import { tool as icalGenerator } from './ical-generator'; import { tool as imageToCss } from './image-to-css'; import { tool as jsonToGo } from './json-to-go'; import { tool as jsonToSchema } from './json-to-schema'; +import { tool as qrCodeDecoder } from './qr-code-decoder'; import { tool as markdownToHtml } from './markdown-to-html'; import { tool as nginxFormatter } from './nginx-formatter'; import { tool as potrace } from './potrace'; @@ -267,6 +268,9 @@ export const toolsByCategory: ToolCategory[] = [ imageConverter, ocrImage, potrace, + qrCodeDecoder, + svgPlaceholderGenerator, + cameraRecorder, ], }, { diff --git a/src/tools/qr-code-decoder/ical.js.d.ts b/src/tools/qr-code-decoder/ical.js.d.ts new file mode 100644 index 00000000..9c60cc38 --- /dev/null +++ b/src/tools/qr-code-decoder/ical.js.d.ts @@ -0,0 +1,3 @@ +declare module "ical.js" { + export function parse(content: string): object; +} \ No newline at end of file diff --git a/src/tools/qr-code-decoder/index.ts b/src/tools/qr-code-decoder/index.ts new file mode 100644 index 00000000..b7f5bf50 --- /dev/null +++ b/src/tools/qr-code-decoder/index.ts @@ -0,0 +1,12 @@ +import { Qrcode } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'QR Code decoder', + path: '/qr-code-decoder', + description: 'QR Code Reader', + keywords: ['qrcode', 'qr-code', 'decoder', 'reader'], + component: () => import('./qr-code-decoder.vue'), + icon: Qrcode, + createdAt: new Date('2024-09-01'), +}); diff --git a/src/tools/qr-code-decoder/qr-code-decoder.service.test.ts b/src/tools/qr-code-decoder/qr-code-decoder.service.test.ts new file mode 100644 index 00000000..8f4cdfc5 --- /dev/null +++ b/src/tools/qr-code-decoder/qr-code-decoder.service.test.ts @@ -0,0 +1,151 @@ +import { describe, expect, test } from 'vitest'; +import { parseQRData } from './qr-code-decoder.service'; + +describe('qr-code-decoder', () => { + test('parseQRData should parse content correctly', () => { + expect(parseQRData(null)).toEqual({ + type: 'Unknown', + value: '', + }); + expect(parseQRData('')).toEqual({ + type: 'Unknown', + value: '', + }); + expect(parseQRData('TEL:+33123456')).toEqual({ + type: 'Phone', + value: '+33123456', + }); + expect(parseQRData('MATMSG:TO: email@example.com;SUB:email subject;BODY:Email text;;')).toEqual({ + type: 'Email', + value: { + body: 'Email text', + subject: 'email subject', + to: 'email@example.com', + }, + }); + expect(parseQRData('mailto:email@example.com?subject=email subject&body=Email text')).toEqual({ + type: 'Email', + value: { + body: 'Email text', + subject: 'email subject', + to: 'email@example.com', + }, + }); + expect(parseQRData('SMTP:email@example.com:email subject:Email text')).toEqual({ + type: 'Email', + value: { + body: 'Email text', + subject: 'email subject', + to: 'email@example.com', + }, + }); + expect(parseQRData('smsto:+33315555:message')).toEqual({ + type: 'SMS', + value: { + message: 'message', + to: '+33315555', + }, + }); + expect(parseQRData('WIFI:T:nopass;S:ssid;H:true;')).toEqual({ + type: 'Wifi', + value: { + authentication: 'nopass', + hidden: 'true', + name: 'ssid', + password: undefined, + }, + }); + expect(parseQRData('WIFI:T:WPA;S:ssid;P:password;H:false;')).toEqual({ + type: 'Wifi', + value: { + authentication: 'WPA', + hidden: 'false', + name: 'ssid', + password: 'password', // NOSONAR + }, + }); + expect(parseQRData('BEGIN:VCALENDAR\nPRODID:-//xyz Corp//NONSGML PDA Calendar Version 1.0//EN\nVERSION:2.0\nBEGIN:VEVENT\nDTSTAMP:19960704T120000Z\nUID:uid1@example.com\nORGANIZER:mailto:jsmith@example.com\nDTSTART:19960918T143000Z\nDTEND:19960920T220000Z\nSTATUS:CONFIRMED\nCATEGORIES:CONFERENCE\nSUMMARY:Networld+Interop Conference\nDESCRIPTION:Networld+Interop Conference\n and Exhibit\\nAtlanta World Congress Center\\n\n Atlanta\\, Georgia\nEND:VEVENT\nEND:VCALENDAR')) + .toEqual({ + type: 'iCal', + value: [ + 'vcalendar', + [ + [ + 'prodid', + {}, + 'text', + '-//xyz Corp//NONSGML PDA Calendar Version 1.0//EN', + ], + [ + 'version', + {}, + 'text', + '2.0', + ], + ], + [ + [ + 'vevent', + [ + [ + 'dtstamp', + {}, + 'date-time', + '1996-07-04T12:00:00Z', + ], + [ + 'uid', + {}, + 'text', + 'uid1@example.com', + ], + [ + 'organizer', + {}, + 'cal-address', + 'mailto:jsmith@example.com', + ], + [ + 'dtstart', + {}, + 'date-time', + '1996-09-18T14:30:00Z', + ], + [ + 'dtend', + {}, + 'date-time', + '1996-09-20T22:00:00Z', + ], + [ + 'status', + {}, + 'text', + 'CONFIRMED', + ], + [ + 'categories', + {}, + 'text', + 'CONFERENCE', + ], + [ + 'summary', + {}, + 'text', + 'Networld+Interop Conference', + ], + [ + 'description', + {}, + 'text', + 'Networld+Interop Conference and Exhibit\nAtlanta World Congress Center\nAtlanta, Georgia', + ], + ], + [], + ], + ], + ], + }); + }); +}); diff --git a/src/tools/qr-code-decoder/qr-code-decoder.service.ts b/src/tools/qr-code-decoder/qr-code-decoder.service.ts new file mode 100644 index 00000000..2eb19394 --- /dev/null +++ b/src/tools/qr-code-decoder/qr-code-decoder.service.ts @@ -0,0 +1,83 @@ +import ICAL from 'ical.js'; + +export function parseQRData(qrContent: string | null) { + if (!qrContent) { + return { type: 'Unknown', value: '' }; + } + if (qrContent.startsWith('BEGIN:VCALENDAR')) { + return { type: 'iCal', value: ICAL.parse(qrContent?.trim()) }; + } + if (qrContent.startsWith('TEL:')) { + return { type: 'Phone', value: qrContent.substring(4)?.trim() }; + } + if (qrContent.startsWith('MATMSG:')) { + // MATMSG:TO: email@example.com;SUB:email subject;BODY:Email text;; + const parsing = /^MATMSG:(?:TO:([^;]*);)?(?:SUB:([^;]*);)?(?:BODY:([^;]*))?;;$/.exec(qrContent) || []; + return { + type: 'Email', + value: { + to: parsing[1]?.trim(), + subject: parsing[2]?.trim(), + body: parsing[3]?.trim(), + }, + }; + } + if (qrContent.startsWith('mailto:')) { + // mailto:email@example.com?subject=email subject&body=Email text + const parsing = /^mailto:([^\?]+)\?subject=([^\&]*)(?:&body=(.*))$/.exec(qrContent) || []; + return { + type: 'Email', + value: { + to: parsing[1]?.trim(), + subject: parsing[2]?.trim(), + body: parsing[3]?.trim(), + }, + }; + } + if (qrContent.startsWith('SMTP:')) { + // SMTP:email@example.com:email subject:Email text + const parsing = /^SMTP:([^:]+)(?::([^:]*))(?::([^:]*))?$/.exec(qrContent) || []; + return { + type: 'Email', + value: { + to: parsing[1]?.trim(), + subject: parsing[2]?.trim(), + body: parsing[3]?.trim(), + }, + }; + } + if (qrContent.startsWith('smsto:')) { + // smsto:${phoneNumber}:${message} + const parsing = /^smsto:([^:]+)(?::(.+))$/.exec(qrContent) || []; + return { + type: 'SMS', + value: { + to: parsing[1]?.trim(), + message: parsing[2]?.trim(), + }, + }; + } + if (qrContent.startsWith('WIFI:')) { + // WIFI:T:${authentication};S:${name};${authentication !== 'nopass' ? `P:${password};` : ''}H:${hidden}; + const parsing = /^WIFI:T:([^;]+);S:([^;]+);(?:P:([^;]+);)?(?:H:([^;]+);)?$/.exec(qrContent) || []; + return { + type: 'Wifi', + value: { + authentication: parsing[1]?.trim(), + name: parsing[2]?.trim(), + password: parsing[3]?.trim(), + hidden: parsing[4]?.trim(), + }, + }; + } + if (/^(?:https?|ftp):\/\//.test(qrContent)) { + return { + type: 'Url', + value: qrContent, + }; + } + return { + type: 'Text', + value: qrContent, + }; +} diff --git a/src/tools/qr-code-decoder/qr-code-decoder.vue b/src/tools/qr-code-decoder/qr-code-decoder.vue new file mode 100644 index 00000000..7bd07ff8 --- /dev/null +++ b/src/tools/qr-code-decoder/qr-code-decoder.vue @@ -0,0 +1,64 @@ + + + + +