Skip to content

Commit 666d8ba

Browse files
Merge pull request #378 from monkvision/fix/drivably
fix: MOZJPEG Compression format for Safari
2 parents a80649c + 28696c2 commit 666d8ba

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

packages/camera/src/components/Camera/hooks/useCamera.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const toBlob = (canvasElement, type) => new Promise((resolve) => {
1818
*/
1919
const diff = 1;
2020
const canvas = document.createElement('canvas');
21-
const imageType = utils.supportsWebP ? 'image/webp' : 'image/png';
21+
const imageType = utils.supportsWebP ? 'image/webp' : 'image/jpeg';
22+
const imageFilenameExtension = imageType.substring('image/'.length);
2223

2324
/**
2425
* `useCamera` is a hook that takes the `canvasResolution` which holds the dimensions of the canvas,
@@ -83,7 +84,7 @@ export default function useCamera(
8384

8485
webCaptureTracing?.finish();
8586

86-
return { uri, width, height };
87+
return { uri, width, height, imageType, imageFilenameExtension };
8788
}, [width, height, stream]);
8889

8990
const resumePreview = async () => {

packages/camera/src/components/Camera/hooks/useCompression/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,29 @@ export default function useCompression() {
3737
* @returns {Blob} - the compressed picture on webp format
3838
*/
3939
return useCallback((data, width, height, options = {}) => {
40+
const tweakedOptions = {
41+
...options,
42+
quality: options.quality && options.quality <= 1 ? options.quality * 100 : options.quality,
43+
};
44+
4045
if (module) {
4146
return new Promise((resolve) => {
42-
const result = module.encode(data, width, height, { ...defaultOptions, ...options });
47+
const result = module.encode(data, width, height, { ...defaultOptions, ...tweakedOptions });
4348

44-
resolve(new Blob([result, { type: 'image/jpeg' }]));
49+
resolve(new Blob([result], { type: 'image/jpeg' }));
4550
});
4651
}
4752

4853
return new Promise((resolve, reject) => {
4954
webEnc()
5055
.then((newModule) => {
5156
setModule(newModule);
52-
const result = newModule.encode(data, width, height, { ...defaultOptions, options });
57+
const result = newModule.encode(
58+
data,
59+
width,
60+
height,
61+
{ ...defaultOptions, tweakedOptions },
62+
);
5363

5464
resolve(new Blob([result], { type: 'image/jpeg' }));
5565
})

packages/camera/src/components/Capture/hooks.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import monk from '@monkvision/corejs';
2-
import { useSentry } from '@monkvision/toolkit';
2+
import { useSentry, utils } from '@monkvision/toolkit';
33
import { SentryConstants } from '@monkvision/toolkit/src/hooks/useSentry';
44
import axios from 'axios';
55
import { Buffer } from 'buffer';
@@ -110,7 +110,7 @@ export function useSetPictureAsync({ current, sights, uploads, Sentry }) {
110110
const uri = picture.localUri || picture.uri;
111111

112112
const actions = [{ resize: { width: 133 } }];
113-
const saveFormat = Platform.OS === 'web' ? SaveFormat.WEBP : SaveFormat.JPEG;
113+
const saveFormat = (Platform.OS === 'web' && utils.supportsWebP()) ? SaveFormat.WEBP : SaveFormat.JPEG;
114114
const saveOptions = { compress: 1, format: saveFormat };
115115
const imageResult = await manipulateAsync(uri, actions, saveOptions);
116116

@@ -264,9 +264,14 @@ export function useStartUploadAsync({
264264
payload: { id, status: 'pending', label },
265265
});
266266

267-
const fileType = Platform.OS === 'web' ? 'webp' : 'jpg';
268-
const filename = `${id}-${inspectionId}.${fileType}`;
269-
const multiPartKeys = { image: 'image', json: 'json', filename, type: `image/${fileType}` };
267+
const fileType = picture.fileType;
268+
const filename = `${id}-${inspectionId}.${picture.imageFilenameExtension}`;
269+
const multiPartKeys = {
270+
image: 'image',
271+
json: 'json',
272+
type: fileType,
273+
filename,
274+
};
270275

271276
const json = JSON.stringify({
272277
acquisition: {
@@ -296,7 +301,7 @@ export function useStartUploadAsync({
296301
fileBits = [file];
297302
} else {
298303
const buffer = Buffer.from(picture.uri, 'base64');
299-
fileBits = new Blob([buffer], { type: 'png' });
304+
fileBits = new Blob([buffer], { type: picture.imageFilenameExtension });
300305
}
301306

302307
const file = await new File(

packages/camera/src/components/UploadCenter/UploadCard/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function UploadCard({
2222
colors,
2323
}) {
2424
const { i18n } = useTranslation();
25-
const { uri } = picture;
25+
const uri = picture?.uri;
2626

2727
const {
2828
isPending,
@@ -159,7 +159,7 @@ UploadCard.propTypes = {
159159
onReupload: PropTypes.func,
160160
picture: PropTypes.shape({
161161
uri: PropTypes.string,
162-
}).isRequired,
162+
}),
163163
upload: PropTypes.shape({
164164
error: PropTypes.objectOf(PropTypes.any),
165165
picture: PropTypes.shape({ uri: PropTypes.string }),
@@ -171,5 +171,6 @@ UploadCard.defaultProps = {
171171
onRetake: () => {},
172172
onReupload: () => {},
173173
onRecheck: () => {},
174+
picture: undefined,
174175
};
175176
export default UploadCard;

0 commit comments

Comments
 (0)