Skip to content

Commit 8502cc6

Browse files
asteriscosDesvelao
andauthored
[Backport 4.4-7.16] Upload file for customization.logo.* settings (#4773)
* Upload file for `customization.logo.*` settings (#4504) * feat(settings): centralize the plugin settings Create the plugin setting schema Define the current plugin settings Remove refactored code * feat(settings): add setting services and replaced the references to constants * feat(settings): refactor the content of the default configuration file Use dynamically the definition of the plugin settings * feat(inputs): create new inputs components Add new hooks to manage when a input value or form has changed Add new inputs components * feat(configuration): refactor the form of Settings/Configuration Refactor Header, BottomBar, Configuration components Remove deprecated files * feat(settings): support updating multiple setting at the same time Changed the endpoint that updating the plugin setting to support multiple settings at the same time Refactor the getConfiguration service. Split the logic to: - Read the file and transform to JSON - Obfuscate the password key of the host configuration * feat: add validation to the plugin settings Create services to validate Add the validation to the plugin settings * feat(validation): add validation to the `PUT /utils/configuration` endpoint * feat(validation): add validation to the configuration form in `Settings/Configuration` * feat(validatio): remove no used import * clean: remove not used code * feat(settings): upload file for `customization.logo` settigs Add endpoints - `PUT /utils/configuration/files/{key}` - `DELETE /utils/configuration/files/{key}` Add plugin setting type: filepicker Add filepicker input form Display the customized image in `Settings/Configuration` Add button to remove the customized image * feat(settings): Add validation for extensions of files for `customization.logo.*` settings * fix: fixed category name in `Settings/Configuration` * fix(settings): Fix accessing to `validate` of undefined error * fix(settings): fixed error due to missing service * fix(settings): fixed problems setting a custom logo. Add logic to do request to the expected endpoint Add function to transform the filepicker input form * Made upload file mkdir recursive * Fixed configuration image preview ratios * Fixed logo aspect ratio in wz-menu * Fix file input Remove button error * fix(settings): refactor the form and inputs of `Settings/Configuration` to control the global state of the form * fix: add value transformation for the form inputs and output of fields changed * fix: Fixed some settings validation * fix(settings): fixed validation of literals * fix(settings): removed unused import * feat(settings): clean file picker inputs when there is a selected file and saving the configuration * fix(settings): get plugin setting description to display in Settings/Configuration * fix(settings): renamed properties related to transform the value of the input * feat(settings): add description to the plugin setting definition properties * fix(settings): fix getConfiguration service when the configuration file has no `hosts` entry * fix(settings): Fixed error when do changes of the `useForm` hook an rename methods of this * tests(settings): add test related to the plugin settings and its configuration from the UI * feat(settings): rename plugin setting options of type select to match its type * feat(settings): add plugin settings services and enhance the description of the plugin settings in default configuration file and UI * tests(input-form): update tests of InputForm component * test(configuration-file): add tests of the default configuration file * feat(settings): remove `extensions.mitre` plugin setting * test(settings): add test to validate the plugin setting when updating it through PUT /utils/configuration fix some plugin settings validation * feat(settings): add documentation to some setting services and test some of them * fix: fixed documentation of setting service * doc(settings): move the documentation of the plugin setting properties * fix(settings): rename some plugin setting properties because of request changes - Rename plugin setting properties: - `default` to `defaultValue` - `defaultHidden` to `defaultValueIfNotSet` - `configurableFile` to `isConfigurableFromFile` - configurableUI` to `isConfigurableFromUI` - `requireHealthCheck` to `requiresRunningHealthCheck` - `requireReload` to `requiresReloadingBrowserTab` - `requireRestart` to `requiresRestartingPluginPlatform` - Fix tests * tests: fix tests of InputForm component * fix: response properties when saving the configuration * fix(settings): fix validation plugin settings value in the UI * feat(settings): add validation of selected file size Frontend: - Validate the selected file size in the file picker Backend: - Validate the body payload. This is not the same than the file. So the file size should be lower than the total allowed * fix(settings): fix validation of numbers * fix(settings): fix validation of numbers * fix(settings): fix an issue when removing a file from a file picker that the form displayed there was some change. * fix(settings): fix error when deleting a custom image * feat(settings): format bytes to meaningful unit. Create service to format the bytes to meaningful unit. Add test to the service Replace similar method used in the frontend Add the validation to the settings related to files * test(settings): Add tests related to validation for the `useForm` hook and the `InputForm` component * test(settings): add test to upload and delete customization files Display configuration toast when removing a customization file if it is required * fix(settings): fix displaying toast to run the healthcheck when saving the configuration * feat(settings): add file size to the settings description * fix(settings): remove the selected files in the file pickers when clicks on the `Cancel changes` button * Added category sorting + description + docs link * Added settings sorting within their category * Fixed constant types definition * Checks if localCompare exists validation * fix(settings): fixed plugin setting description doesn't display the minimum number value when it is falsy (0) * fix(settings): fix setting type of `wazuh.monitoring.replicas` and limit the valid number for the number input * feat(settins): add plugin settings category description * fix(settings): fix a problem comparing the initial and current value for the plugin settings of the `number` type * fix(settings): fix wrong conflict resolution * fix(settings): fix typo in setting description * feat(settings): enhance the validation of plugin settings related to indices or index patterns taking in account the supported characters * feat(settings): add validation of setting values in the inputs * fix(tests): format tables of the tests * test(settings): add tests related to the `customization.logo.*` in the form inputs * Fix small typo * fix(settings): fix response when uploading custom files for `customization.logo.*` setting and fix URL in test * feat(upload-file): get the file extension from file buffer. - Add service to get the file extesion from file buffer - Add tests - Removed unnecessary `extension` field when uploading a file using `PUT /utils/configuration/files/{key}` * fix(settings): fix a typo in a toast related to modify the plugin settings from UI * Changed Custom Branding documentation link * Merge centralize plugin settings PR * Fix white-labeling documentation url * Code format * Delete unused imports * fix(settings): fix a problem with the useForm hook * fix(settings): refactor the settings validation function to a class and rename the file * feat(settings): add check for integer numbers and adapt the affected settings * Fix semi-colon error * changelog: add entries to changelog * fix(settings): fix some request changes - unused imports - change some toast texts * fix(settings): fix an unsupported attribute for the number inputs * fix(settings): fix a problem when removing the `customization.logo.reports` from Settings/Configuration * fix(settings): remove unused import * Change validation error for inputs that don't allow whitespaces Signed-off-by: Alex Ruiz Becerra <alejandro.ruiz.becerra@wazuh.com> * Update file-extension.ts Add `3c73` SVG file signature * feat(settings): change the layout of the filepicker input plus logo and button to remove the configuration * test: update snapshots * test: fix tests and update snapshots * changelog: add PR entry * changelog:fix PR entries * fix(settings): fix a problem with SVG images that are not displayed when these do not have `width` and `height` * fix: remove comment * Fix logo image cache on upload * Added a comment describing image versioning Signed-off-by: Alex Ruiz Becerra <alejandro.ruiz.becerra@wazuh.com> Co-authored-by: Federico Rodriguez <federico.rodriguez@wazuh.com> Co-authored-by: Álex <alejandro.ruiz.becerra@wazuh.com> (cherry picked from commit bc51482) * Update snapshots Co-authored-by: Antonio <34042064+Desvelao@users.noreply.github.com>
1 parent 84a1b5b commit 8502cc6

33 files changed

+1229
-371
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,6 @@ cypress/.idea/
7979
cypress/cypress.env.json
8080
cypress/report/
8181
cypress/cookies.json
82+
83+
# Customization plugin assets
84+
public/assets/custom/*

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ All notable changes to the Wazuh app project will be documented in this file.
1010
- Added agent synchronization status in the agent module. [#3874](https://github.com/wazuh/wazuh-kibana-app/pull/3874)
1111
- Redesign the SCA table from agent's dashboard [#4512](https://github.com/wazuh/wazuh-kibana-app/pull/4512)
1212
- Enhanced the plugin setting description displayed in the UI and the configuration file. [#4501](https://github.com/wazuh/wazuh-kibana-app/pull/4501)
13-
- Added validation to the plugin settings in the form of `Settings/Configuration` and the endpoint to update the plugin configuration [#4501](https://github.com/wazuh/wazuh-kibana-app/pull/4503)
13+
- Added validation to the plugin settings in the form of `Settings/Configuration` and the endpoint to update the plugin configuration [#4503](https://github.com/wazuh/wazuh-kibana-app/pull/4503)
1414

1515
### Changed
1616

@@ -20,6 +20,7 @@ All notable changes to the Wazuh app project will be documented in this file.
2020
- Made Agents Overview icons load independently [#4363](https://github.com/wazuh/wazuh-kibana-app/pull/4363)
2121
- Improved the message displayed when there is a versions mismatch between the Wazuh API and the Wazuh APP [#4529](https://github.com/wazuh/wazuh-kibana-app/pull/4529)
2222
- Changed the endpoint that updates the plugin configuration to support multiple settings. [#4501](https://github.com/wazuh/wazuh-kibana-app/pull/4501)
23+
- Allowed to upload an image for the `customization.logo.*` settings in `Settings/Configuration` [#4504](https://github.com/wazuh/wazuh-kibana-app/pull/4504)
2324

2425
### Fixed
2526

common/constants.ts

+153-13
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ export const DOCUMENTATION_WEB_BASE_URL = "https://documentation.wazuh.com";
342342
// Default Elasticsearch user name context
343343
export const ELASTIC_NAME = 'elastic';
344344

345+
346+
// Customization
347+
export const CUSTOMIZATION_ENDPOINT_PAYLOAD_UPLOAD_CUSTOM_FILE_MAXIMUM_BYTES = 1048576;
348+
349+
345350
// Plugin settings
346351
export enum SettingCategory {
347352
GENERAL,
@@ -357,6 +362,35 @@ type TPluginSettingOptionsSelect = {
357362
select: { text: string, value: any }[]
358363
};
359364

365+
type TPluginSettingOptionsEditor = {
366+
editor: {
367+
language: string
368+
}
369+
};
370+
371+
type TPluginSettingOptionsFile = {
372+
file: {
373+
type: 'image'
374+
extensions?: string[]
375+
size?: {
376+
maxBytes?: number
377+
minBytes?: number
378+
}
379+
recommended?: {
380+
dimensions?: {
381+
width: number,
382+
height: number,
383+
unit: string
384+
}
385+
}
386+
store?: {
387+
relativePathFileSystem: string
388+
filename: string
389+
resolveStaticURL: (filename: string) => string
390+
}
391+
}
392+
};
393+
360394
type TPluginSettingOptionsNumber = {
361395
number: {
362396
min?: number
@@ -365,12 +399,6 @@ type TPluginSettingOptionsNumber = {
365399
}
366400
};
367401

368-
type TPluginSettingOptionsEditor = {
369-
editor: {
370-
language: string
371-
}
372-
};
373-
374402
type TPluginSettingOptionsSwitch = {
375403
switch: {
376404
values: {
@@ -387,6 +415,7 @@ export enum EpluginSettingType {
387415
number = 'number',
388416
editor = 'editor',
389417
select = 'select',
418+
filepicker = 'filepicker'
390419
};
391420

392421
export type TPluginSetting = {
@@ -413,14 +442,14 @@ export type TPluginSetting = {
413442
// Modify the setting requires restarting the plugin platform to take effect.
414443
requiresRestartingPluginPlatform?: boolean
415444
// Define options related to the `type`.
416-
options?: TPluginSettingOptionsNumber | TPluginSettingOptionsEditor | TPluginSettingOptionsSelect | TPluginSettingOptionsSwitch
445+
options?: TPluginSettingOptionsNumber | TPluginSettingOptionsEditor | TPluginSettingOptionsFile | TPluginSettingOptionsSelect | TPluginSettingOptionsSwitch
417446
// Transform the input value. The result is saved in the form global state of Settings/Configuration
418447
uiFormTransformChangedInputValue?: (value: any) => any
419448
// Transform the configuration value or default as initial value for the input in Settings/Configuration
420449
uiFormTransformConfigurationValueToInputValue?: (value: any) => any
421450
// Transform the input value changed in the form of Settings/Configuration and returned in the `changed` property of the hook useForm
422451
uiFormTransformInputValueToConfigurationValue?: (value: any) => any
423-
// Validate the value in the form of Settings/Configuration. It returns a string if there is some validation error.
452+
// Validate the value in the form of Settings/Configuration. It returns a string if there is some validation error.
424453
validate?: (value: any) => string | undefined
425454
// Validate function creator to validate the setting in the backend. It uses `schema` of the `@kbn/config-schema` package.
426455
validateBackend?: (schema: any) => (value: unknown) => string | undefined
@@ -898,39 +927,150 @@ export const PLUGIN_SETTINGS: { [key: string]: TPluginSetting } = {
898927
title: "App main logo",
899928
description: `This logo is used in the app main menu, at the top left corner.`,
900929
category: SettingCategory.CUSTOMIZATION,
901-
type: EpluginSettingType.text,
930+
type: EpluginSettingType.filepicker,
902931
defaultValue: "",
903932
isConfigurableFromFile: true,
904933
isConfigurableFromUI: true,
934+
options: {
935+
file: {
936+
type: 'image',
937+
extensions: ['.jpeg', '.jpg', '.png', '.svg'],
938+
size: {
939+
maxBytes: CUSTOMIZATION_ENDPOINT_PAYLOAD_UPLOAD_CUSTOM_FILE_MAXIMUM_BYTES,
940+
},
941+
recommended: {
942+
dimensions: {
943+
width: 300,
944+
height: 70,
945+
unit: 'px'
946+
}
947+
},
948+
store: {
949+
relativePathFileSystem: 'public/assets/custom/images',
950+
filename: 'customization.logo.app',
951+
resolveStaticURL: (filename: string) => `custom/images/${filename}?v=${Date.now()}`
952+
// ?v=${Date.now()} is used to force the browser to reload the image when a new file is uploaded
953+
}
954+
}
955+
},
956+
validate: function(value){
957+
return SettingsValidator.compose(
958+
SettingsValidator.filePickerFileSize({...this.options.file.size, meaningfulUnit: true}),
959+
SettingsValidator.filePickerSupportedExtensions(this.options.file.extensions)
960+
)(value)
961+
},
905962
},
906963
"customization.logo.healthcheck": {
907964
title: "Healthcheck logo",
908965
description: `This logo is displayed during the Healthcheck routine of the app.`,
909966
category: SettingCategory.CUSTOMIZATION,
910-
type: EpluginSettingType.text,
967+
type: EpluginSettingType.filepicker,
911968
defaultValue: "",
912969
isConfigurableFromFile: true,
913970
isConfigurableFromUI: true,
971+
options: {
972+
file: {
973+
type: 'image',
974+
extensions: ['.jpeg', '.jpg', '.png', '.svg'],
975+
size: {
976+
maxBytes: CUSTOMIZATION_ENDPOINT_PAYLOAD_UPLOAD_CUSTOM_FILE_MAXIMUM_BYTES,
977+
},
978+
recommended: {
979+
dimensions: {
980+
width: 300,
981+
height: 70,
982+
unit: 'px'
983+
}
984+
},
985+
store: {
986+
relativePathFileSystem: 'public/assets/custom/images',
987+
filename: 'customization.logo.healthcheck',
988+
resolveStaticURL: (filename: string) => `custom/images/${filename}?v=${Date.now()}`
989+
// ?v=${Date.now()} is used to force the browser to reload the image when a new file is uploaded
990+
}
991+
}
992+
},
993+
validate: function(value){
994+
return SettingsValidator.compose(
995+
SettingsValidator.filePickerFileSize({...this.options.file.size, meaningfulUnit: true}),
996+
SettingsValidator.filePickerSupportedExtensions(this.options.file.extensions)
997+
)(value)
998+
},
914999
},
9151000
"customization.logo.reports": {
9161001
title: "PDF reports logo",
9171002
description: `This logo is used in the PDF reports generated by the app. It's placed at the top left corner of every page of the PDF.`,
9181003
category: SettingCategory.CUSTOMIZATION,
919-
type: EpluginSettingType.text,
1004+
type: EpluginSettingType.filepicker,
9201005
defaultValue: "",
9211006
defaultValueIfNotSet: REPORTS_LOGO_IMAGE_ASSETS_RELATIVE_PATH,
9221007
isConfigurableFromFile: true,
9231008
isConfigurableFromUI: true,
1009+
options: {
1010+
file: {
1011+
type: 'image',
1012+
extensions: ['.jpeg', '.jpg', '.png'],
1013+
size: {
1014+
maxBytes: CUSTOMIZATION_ENDPOINT_PAYLOAD_UPLOAD_CUSTOM_FILE_MAXIMUM_BYTES,
1015+
},
1016+
recommended: {
1017+
dimensions: {
1018+
width: 190,
1019+
height: 40,
1020+
unit: 'px'
1021+
}
1022+
},
1023+
store: {
1024+
relativePathFileSystem: 'public/assets/custom/images',
1025+
filename: 'customization.logo.reports',
1026+
resolveStaticURL: (filename: string) => `custom/images/${filename}`
1027+
}
1028+
}
1029+
},
1030+
validate: function(value){
1031+
return SettingsValidator.compose(
1032+
SettingsValidator.filePickerFileSize({...this.options.file.size, meaningfulUnit: true}),
1033+
SettingsValidator.filePickerSupportedExtensions(this.options.file.extensions)
1034+
)(value)
1035+
},
9241036
},
9251037
"customization.logo.sidebar": {
9261038
title: "Navigation drawer logo",
9271039
description: `This is the logo for the app to display in the platform's navigation drawer, this is, the main sidebar collapsible menu.`,
9281040
category: SettingCategory.CUSTOMIZATION,
929-
type: EpluginSettingType.text,
1041+
type: EpluginSettingType.filepicker,
9301042
defaultValue: "",
9311043
isConfigurableFromFile: true,
9321044
isConfigurableFromUI: true,
9331045
requiresReloadingBrowserTab: true,
1046+
options: {
1047+
file: {
1048+
type: 'image',
1049+
extensions: ['.jpeg', '.jpg', '.png', '.svg'],
1050+
size: {
1051+
maxBytes: CUSTOMIZATION_ENDPOINT_PAYLOAD_UPLOAD_CUSTOM_FILE_MAXIMUM_BYTES,
1052+
},
1053+
recommended: {
1054+
dimensions: {
1055+
width: 80,
1056+
height: 80,
1057+
unit: 'px'
1058+
}
1059+
},
1060+
store: {
1061+
relativePathFileSystem: 'public/assets/custom/images',
1062+
filename: 'customization.logo.sidebar',
1063+
resolveStaticURL: (filename: string) => `custom/images/${filename}?v=${Date.now()}`
1064+
// ?v=${Date.now()} is used to force the browser to reload the image when a new file is uploaded
1065+
}
1066+
}
1067+
},
1068+
validate: function(value){
1069+
return SettingsValidator.compose(
1070+
SettingsValidator.filePickerFileSize({...this.options.file.size, meaningfulUnit: true}),
1071+
SettingsValidator.filePickerSupportedExtensions(this.options.file.extensions)
1072+
)(value)
1073+
},
9341074
},
9351075
"disabled_roles": {
9361076
title: "Disable roles",
@@ -1361,7 +1501,7 @@ export const PLUGIN_SETTINGS: { [key: string]: TPluginSetting } = {
13611501
SettingsValidator.isString,
13621502
SettingsValidator.isNotEmptyString,
13631503
SettingsValidator.hasNoSpaces,
1364-
SettingsValidator.noLiteralString('.', '..'),
1504+
SettingsValidator.noLiteralString('.', '..'),
13651505
SettingsValidator.noStartsWithString('-', '_', '+', '.'),
13661506
SettingsValidator.hasNotInvalidCharacters('\\', '/', '?', '"', '<', '>', '|', ',', '#')
13671507
)),

common/plugin-settings.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ describe('[settings] Input validation', () => {
9393
${'cron.statistics.interval'} | ${true} | ${"Interval is not valid."}
9494
${'cron.statistics.status'} | ${true} | ${undefined}
9595
${'cron.statistics.status'} | ${0} | ${'It should be a boolean. Allowed values: true or false.'}
96+
${'customization.logo.app'} | ${{size: 124000, name: 'image.jpg'}} | ${undefined}
97+
${'customization.logo.app'} | ${{size: 124000, name: 'image.jpeg'}} | ${undefined}
98+
${'customization.logo.app'} | ${{size: 124000, name: 'image.png'}} | ${undefined}
99+
${'customization.logo.app'} | ${{size: 124000, name: 'image.svg'}} | ${undefined}
100+
${'customization.logo.app'} | ${{size: 124000, name: 'image.txt'}} | ${'File extension is invalid. Allowed file extensions: .jpeg, .jpg, .png, .svg.'}
101+
${'customization.logo.app'} | ${{size: 1240000, name: 'image.txt'}} | ${'File size should be lower or equal than 1 MB.'}
102+
${'customization.logo.healthcheck'} | ${{size: 124000, name: 'image.jpg'}} | ${undefined}
103+
${'customization.logo.healthcheck'} | ${{size: 124000, name: 'image.jpeg'}} | ${undefined}
104+
${'customization.logo.healthcheck'} | ${{size: 124000, name: 'image.png'}} | ${undefined}
105+
${'customization.logo.healthcheck'} | ${{size: 124000, name: 'image.svg'}} | ${undefined}
106+
${'customization.logo.healthcheck'} | ${{size: 124000, name: 'image.txt'}} | ${'File extension is invalid. Allowed file extensions: .jpeg, .jpg, .png, .svg.'}
107+
${'customization.logo.healthcheck'} | ${{size: 1240000, name: 'image.txt'}} | ${'File size should be lower or equal than 1 MB.'}
108+
${'customization.logo.reports'} | ${{size: 124000, name: 'image.jpg'}} | ${undefined}
109+
${'customization.logo.reports'} | ${{size: 124000, name: 'image.jpeg'}} | ${undefined}
110+
${'customization.logo.reports'} | ${{size: 124000, name: 'image.png'}} | ${undefined}
111+
${'customization.logo.reports'} | ${{size: 124000, name: 'image.svg'}} | ${'File extension is invalid. Allowed file extensions: .jpeg, .jpg, .png.'}
112+
${'customization.logo.reports'} | ${{size: 124000, name: 'image.txt'}} | ${'File extension is invalid. Allowed file extensions: .jpeg, .jpg, .png.'}
113+
${'customization.logo.reports'} | ${{size: 1240000, name: 'image.txt'}} | ${'File size should be lower or equal than 1 MB.'}
114+
${'customization.logo.sidebar'} | ${{size: 124000, name: 'image.jpg'}} | ${undefined}
115+
${'customization.logo.sidebar'} | ${{size: 124000, name: 'image.jpeg'}} | ${undefined}
116+
${'customization.logo.sidebar'} | ${{size: 124000, name: 'image.png'}} | ${undefined}
117+
${'customization.logo.sidebar'} | ${{size: 124000, name: 'image.svg'}} | ${undefined}
118+
${'customization.logo.sidebar'} | ${{size: 124000, name: 'image.txt'}} | ${'File extension is invalid. Allowed file extensions: .jpeg, .jpg, .png, .svg.'}
119+
${'customization.logo.sidebar'} | ${{size: 1240000, name: 'image.txt'}} | ${'File size should be lower or equal than 1 MB.'}
96120
${'disabled_roles'} | ${['test']} | ${undefined}
97121
${'disabled_roles'} | ${['']} | ${'Value can not be empty.'}
98122
${'disabled_roles'} | ${['test space']} | ${"No whitespaces allowed."}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { getFileExtensionFromBuffer } from "./file-extension";
2+
import fs from 'fs';
3+
import path from 'path';
4+
5+
describe('getFileExtensionFromBuffer', () => {
6+
it.each`
7+
filepath | extension
8+
${'../../server/routes/wazuh-utils/fixtures/fixture_image_small.jpg'} | ${'jpg'}
9+
${'../../server/routes/wazuh-utils/fixtures/fixture_image_small.png'} | ${'png'}
10+
${'../../server/routes/wazuh-utils/fixtures/fixture_image_big.png'} | ${'png'}
11+
${'../../server/routes/wazuh-utils/fixtures/fixture_image_small.svg'} | ${'svg'}
12+
${'../../server/routes/wazuh-utils/fixtures/fixture_file.txt'} | ${'unknown'}
13+
`(`filepath: $filepath expects to get extension: $extension`, ({ extension, filepath }) => {
14+
const bufferFile = fs.readFileSync(path.join(__dirname, filepath));
15+
expect(getFileExtensionFromBuffer(bufferFile)).toBe(extension);
16+
});
17+
});

common/services/file-extension.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Get the file extension from a file buffer. Calculates the image format by reading the first 4 bytes of the image (header)
3+
* Supported types: jpeg, jpg, png, svg
4+
* Additionally, this function allows checking gif images.
5+
* @param buffer file buffer
6+
* @returns the file extension. Example: jpg, png, svg. it Returns unknown if it can not find the extension.
7+
*/
8+
export function getFileExtensionFromBuffer(buffer: Buffer): string {
9+
const imageFormat = buffer.toString('hex').substring(0, 4);
10+
switch (imageFormat) {
11+
case '4749':
12+
return 'gif';
13+
case 'ffd8':
14+
return 'jpg'; // Also jpeg
15+
case '8950':
16+
return 'png';
17+
case '3c73':
18+
case '3c3f':
19+
return 'svg';
20+
default:
21+
return 'unknown';
22+
}
23+
};

common/services/file-size.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {formatBytes } from "./file-size";
2+
3+
describe('formatBytes', () => {
4+
it.each`
5+
bytes | decimals | expected
6+
${1024} | ${2} | ${'1 KB'}
7+
${1023} | ${2} | ${'1023 Bytes'}
8+
${1500} | ${2} | ${'1.46 KB'}
9+
${1500} | ${1} | ${'1.5 KB'}
10+
${1500} | ${3} | ${'1.465 KB'}
11+
${1048576} | ${2} | ${'1 MB'}
12+
${1048577} | ${2} | ${'1 MB'}
13+
${1475487} | ${2} | ${'1.41 MB'}
14+
${1475487} | ${1} | ${'1.4 MB'}
15+
${1475487} | ${3} | ${'1.407 MB'}
16+
${1073741824} | ${2} | ${'1 GB'}
17+
`(`bytes: $bytes | decimals: $decimals | expected: $expected`, ({ bytes, decimals, expected }) => {
18+
expect(formatBytes(bytes, decimals)).toBe(expected);
19+
});
20+
});

common/services/file-size.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Format the number the bytes to the higher unit.
3+
* @param bytes Bytes
4+
* @param decimals Number of decimals
5+
* @returns Formatted value with the unit
6+
*/
7+
export function formatBytes(bytes: number, decimals: number = 2): string {
8+
if (!+bytes) return '0 Bytes';
9+
10+
const k = 1024;
11+
const dm = decimals < 0 ? 0 : decimals;
12+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
13+
14+
const i = Math.floor(Math.log(bytes) / Math.log(k));
15+
16+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
17+
};

0 commit comments

Comments
 (0)