From a96990ebbc957dd9aab3c4c664f8b54e5c738163 Mon Sep 17 00:00:00 2001 From: Michael An <2331806369@qq.com> Date: Thu, 6 Feb 2025 10:50:09 +0800 Subject: [PATCH] fix heic format rotate image (#7430) --- .../src/components/dialog/image-dialog.js | 3 +- frontend/src/tests/utils/utils.test.js | 63 +++++++++++++++++++ frontend/src/utils/constants.js | 2 +- 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 frontend/src/tests/utils/utils.test.js diff --git a/frontend/src/components/dialog/image-dialog.js b/frontend/src/components/dialog/image-dialog.js index a6b55532b74..626936c5f91 100644 --- a/frontend/src/components/dialog/image-dialog.js +++ b/frontend/src/components/dialog/image-dialog.js @@ -27,7 +27,8 @@ const ImageDialog = ({ enableRotate: oldEnableRotate, imageItems, imageIndex, cl // The backend server does not support rotating HEIC images let enableRotate = oldEnableRotate; - const suffix = mainImg.src.slice(mainImg.src.lastIndexOf('.') + 1, mainImg.src.lastIndexOf('?')).toLowerCase(); + const urlParts = mainImg.src.split('?')[0].split('.'); + const suffix = urlParts[urlParts.length - 1]; if (suffix === 'heic') { enableRotate = false; } diff --git a/frontend/src/tests/utils/utils.test.js b/frontend/src/tests/utils/utils.test.js new file mode 100644 index 00000000000..8602d61b8a9 --- /dev/null +++ b/frontend/src/tests/utils/utils.test.js @@ -0,0 +1,63 @@ +import { Utils } from "../../utils/utils"; + +describe('getFileExtension', () => { + it('should return the file extension with dot', () => { + const fileName = 'document.pdf'; + const result = Utils.getFileExtension(fileName, false); + expect(result).toBe('.pdf'); + }); + it('should return the file extension without dot', () => { + const fileName = 'image.jpeg'; + const result = Utils.getFileExtension(fileName, true); + expect(result).toBe('jpeg'); + }); + it('should handle filenames with multiple dots', () => { + const fileName = 'archive.tar.gz'; + const resultWithDot = Utils.getFileExtension(fileName, false); + const resultWithoutDot = Utils.getFileExtension(fileName, true); + expect(resultWithDot).toBe('.gz'); + expect(resultWithoutDot).toBe('gz'); + }); + it('should handle filenames with upper case extensions', () => { + const fileName = 'movie.MP4'; + const result = Utils.getFileExtension(fileName, true); + expect(result).toBe('mp4'); + }); + it('should handle file name with special characters', () => { + const fileName = '/repo/349d72de-e342-4461-a10a-45265c9cb4c2/raw/HEIC/arec4-j1qmq%20(1).heic'; + const result = Utils.getFileExtension(fileName, true); + expect(result).toBe('heic'); + }); +}); + +describe('bytesToSize', () => { + it('should return empty string if bytes is undefined', () => { + const result = Utils.bytesToSize(undefined); + expect(result).toBe(' '); + }); + it('should return double dash if bytes is negative', () => { + const result = Utils.bytesToSize(-1); + expect(result).toBe('--'); + }); + it('should return bytes with unit if bytes is 0', () => { + const result = Utils.bytesToSize(0); + expect(result).toBe('0 B'); + }); + it('should return bytes with unit if bytes is positive', () => { + const result = Utils.bytesToSize(1000); + expect(result).toBe('1.0 KB'); + }); + it('should handle different units', () => { + const result = Utils.bytesToSize(1000 * 1000); + expect(result).toBe('1.0 MB'); + }); + it('should handle different units', () => { + const result = Utils.bytesToSize(1000 * 1000 * 1000); + expect(result).toBe('1.0 GB'); + }); + it('should handle different units', () => { + const result = Utils.bytesToSize(1000 * 1000 * 1000 * 1000); + expect(result).toBe('1.0 TB'); + }); +}); + diff --git a/frontend/src/utils/constants.js b/frontend/src/utils/constants.js index f87c3cf59ff..91eb515d60e 100644 --- a/frontend/src/utils/constants.js +++ b/frontend/src/utils/constants.js @@ -4,7 +4,7 @@ export const defaultContentForSDoc = { }; export const dirPath = '/'; -export const gettext = window.gettext; +export const gettext = window.gettext || ((str) => str); export const internalFilePath = '/_Internal/seatable-integration.json';