Skip to content

Commit 4b32670

Browse files
authored
Merge pull request #29 from sonicbaume/obf-read-image-data
Add support for data attribute in OBF images
2 parents 90ada06 + 069bbd3 commit 4b32670

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

src/processors/obfProcessor.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -138,38 +138,45 @@ class ObfProcessor extends BaseProcessor {
138138
return this.imageCache.get(imageId) ?? null;
139139
}
140140

141-
if (!this.zipFile || !images) {
142-
return null;
143-
}
141+
if (!images) return null;
144142

145143
// Find the image metadata
146144
const imageData = images.find((img: any) => img.id === imageId);
147145
if (!imageData) {
148146
return null;
149147
}
150148

151-
// Try to get the image file from the ZIP
152-
// Images are typically stored in an 'images' folder or root
153-
const possiblePaths = [
154-
imageData.path, // Explicit path if provided
155-
`images/${imageData.filename || imageId}`, // Standard images folder
156-
imageData.id, // Just the ID
157-
].filter(Boolean);
149+
// If image has data property, use that
150+
if ((imageData as { data?: string }).data) {
151+
const dataUrl = (imageData as { data: string }).data;
152+
this.imageCache.set(imageId, dataUrl);
153+
return dataUrl;
154+
}
158155

159-
for (const imagePath of possiblePaths) {
160-
try {
161-
const buffer = await this.zipFile.readFile(imagePath as string);
162-
if (buffer) {
163-
const contentType =
164-
(imageData as { content_type?: string }).content_type ||
165-
this.getMimeTypeFromFilename(imagePath as string);
166-
const dataUrl = `data:${contentType};base64,${encodeBase64(buffer)}`;
167-
this.imageCache.set(imageId, dataUrl);
168-
return dataUrl;
156+
if (this.zipFile) {
157+
// Try to get the image file from the ZIP
158+
// Images are typically stored in an 'images' folder or root
159+
const possiblePaths = [
160+
imageData.path, // Explicit path if provided
161+
`images/${imageData.filename || imageId}`, // Standard images folder
162+
imageData.id, // Just the ID
163+
].filter(Boolean);
164+
165+
for (const imagePath of possiblePaths) {
166+
try {
167+
const buffer = await this.zipFile.readFile(imagePath as string);
168+
if (buffer) {
169+
const contentType =
170+
(imageData as { content_type?: string }).content_type ||
171+
this.getMimeTypeFromFilename(imagePath as string);
172+
const dataUrl = `data:${contentType};base64,${encodeBase64(buffer)}`;
173+
this.imageCache.set(imageId, dataUrl);
174+
return dataUrl;
175+
}
176+
} catch (err) {
177+
// Continue to next path
178+
continue;
169179
}
170-
} catch (err) {
171-
// Continue to next path
172-
continue;
173180
}
174181
}
175182

0 commit comments

Comments
 (0)