Skip to content

Commit

Permalink
Add to readFile functionality for png badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex H authored and Alex H committed Feb 18, 2025
1 parent 387c682 commit 8e64c7a
Showing 1 changed file with 90 additions and 3 deletions.
93 changes: 90 additions & 3 deletions app/lib/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,101 @@ import * as RNFS from 'react-native-fs';

import { ProfileRecord } from '../model';
import { CredentialImportReport } from '../types/credential';

import { Buffer } from '@craftzdog/react-native-buffer';
export type ReportDetails = Record<string, string[]>;

export async function readFile(path: string): Promise<string> {
const decodedPath = path.replace(/%20/g, ' ');
return RNFS.readFile(decodedPath, 'utf8');
let fileContent: string = '';
let openBadgeCredential = '';
try {
// Extract the file extension from the resolved/provided path
const fileExtension = getFileExtension(path);

if (fileExtension === 'png') {
// Read the PNG file as base64
const base64Image = await RNFS.readFile(path, 'base64');

const decodedString = Buffer.from(base64Image, 'base64').toString('utf8');

// Search for keyword and extract the object following it
const keyword = 'openbadgecredential';
const keywordIndex = decodedString.indexOf(keyword);

// Check if the keyword is found
if (keywordIndex !== -1) {
// Extract the portion of the string after the keyword
const startIndex = keywordIndex + keyword.length;

// Find start of the object
const objectStart = decodedString.indexOf('{', startIndex);

if (objectStart !== -1) {
// Find matching closing brace
let braceCount = 0;
let objectEnd = objectStart;

while (objectEnd < decodedString.length) {
if (decodedString[objectEnd] === '{') {
braceCount++;
} else if (decodedString[objectEnd] === '}') {
braceCount--;
}

// When brace count goes back to zero = found the end of object
if (braceCount === 0) {
break;
}

objectEnd++;
}

// Slice string to capture the entire object (including braces)
const objectString = decodedString.slice(objectStart, objectEnd + 1);

// Parse object
try {
const parsedObject = JSON.parse(objectString);
openBadgeCredential = JSON.stringify(parsedObject, null, 2);
} catch (error) {
console.error('Failed to parse JSON:', error);
}
}
} else {
console.log('Keyword not found');
}

fileContent = openBadgeCredential;

} else {
// For other file types (text, etc.), read as UTF-8
const decodedPath = path.replace(/%20/g, ' ');
fileContent = await RNFS.readFile(decodedPath, 'utf8');
}

} catch (error) {
console.error('Error reading file:', error);
fileContent = ''; // Handle error, fallback value
}

if (fileContent === undefined) {
throw new Error('File content could not be determined');
}

return fileContent;
}

// Utility function to extract file extension
function getFileExtension(path: string): string {
// Check if the path ends with a file extension like ".png"
const regex = /(?:\.([^.]+))?$/;
const match = path.match(regex);
if (match && match[1]) {
return match[1].toLowerCase(); // Return the extension in lowercase (e.g., png, jpg)
}
return ''; // If no extension found, return an empty string
}


export async function pickAndReadFile(): Promise<string> {
const { uri } = await DocumentPicker.pickSingle({
type: DocumentPicker.types.allFiles,
Expand Down

0 comments on commit 8e64c7a

Please sign in to comment.