Skip to content

Commit

Permalink
Allow only Uint8Array for screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
antonis committed Jan 10, 2025
1 parent a1cb828 commit 0a4b12d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/js/feedback/FeedbackForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
View
} from 'react-native';

import { base64ToUint8Array } from '../utils/base64ToUint8Array';
import { sentryLogo } from './branding';
import { defaultConfiguration } from './defaults';
import defaultStyles from './FeedbackForm.styles';
Expand Down Expand Up @@ -68,13 +67,11 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
return;
}

const attachement = (this.state?.attachment instanceof Uint8Array)? this.state?.attachment : base64ToUint8Array(this.state?.attachment);

const attachments = this.state.filename && this.state.attachment
? [
{
filename: this.state.filename,
data: attachement,
data: this.state.attachment,
},
]
: undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/js/feedback/FeedbackForm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export interface FeedbackCallbacks {
/**
* Callback when a screenshot is added
*/
onAddScreenshot?: (attachFile: (filename: string, base64Attachment: string | Uint8Array) => void) => void;
onAddScreenshot?: (attachFile: (filename: string, base64Attachment: Uint8Array) => void) => void;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/js/feedback/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const defaultConfiguration: Partial<FeedbackFormProps> = {
);
}
},
onAddScreenshot: (_: (filename: string, base64Attachment: string | Uint8Array) => void) => {
onAddScreenshot: (_: (filename: string, base64Attachment: Uint8Array) => void) => {
if (__DEV__) {
Alert.alert('Development note', 'onAddScreenshot callback is not implemented.');
}
Expand Down
29 changes: 0 additions & 29 deletions packages/core/src/js/utils/base64ToUint8Array.ts

This file was deleted.

37 changes: 34 additions & 3 deletions samples/react-native/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,37 @@ const Stack = isMobileOs
: createStackNavigator();
const Tab = createBottomTabNavigator();

const handleChooseImage = (attachFile: (filename: string, base64Attachment: string | Uint8Array) => void): void => {
/* eslint-disable no-bitwise */
const base64ToUint8Array = (base64?: string): Uint8Array | undefined => {
if (!base64) return undefined;

const cleanedBase64 = base64.replace(/^data:.*;base64,/, ''); // Remove any prefix before the base64 string
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const bytes: number[] = [];

let buffer = 0;
let bits = 0;

for (const char of cleanedBase64) {
if (char === '=') break;

const value = chars.indexOf(char); // Validate each character
if (value === -1) return undefined;

buffer = (buffer << 6) | value; // Shift 6 bits to the left and add the value
bits += 6;

if (bits >= 8) {
// Add a byte when we have 8 or more bits
bits -= 8;
bytes.push((buffer >> bits) & 0xff);
}
}

return new Uint8Array(bytes);
};

const handleChooseImage = (attachFile: (filename: string, base64Attachment: Uint8Array) => void): void => {
launchImageLibrary({ mediaType: 'photo', includeBase64: true }, (response) => {
if (response.didCancel) {
console.log('User cancelled image picker');
Expand All @@ -150,8 +180,9 @@ const handleChooseImage = (attachFile: (filename: string, base64Attachment: stri
} else if (response.assets && response.assets.length > 0) {
const filename = response.assets[0].fileName;
const base64String = response.assets[0].base64;
if (filename && base64String) {
attachFile(filename, base64String);
const screenShotUint8Array = base64ToUint8Array(base64String);
if (filename && screenShotUint8Array) {
attachFile(filename, screenShotUint8Array);
}
}
});
Expand Down

0 comments on commit 0a4b12d

Please sign in to comment.