<commit_before>/**
 * PptxGenJS: Media Methods
 */

import { IMG_BROKEN } from './core-enums'
import { ISlide, ISlideLayout, ISlideRelMedia } from './core-interfaces'

/**
* Encode Image/Audio/Video into base64
*/
export function encodeSlideMediaRels(layout: ISlide | ISlideLayout): Promise<string>[] {
	let imageProms: Promise<string>[] = []

	// A: Read/Encode each audio/image/video thats not already encoded (eg: base64 provided by user)
	layout.relsMedia
		.filter(rel => {
			return rel.type != 'online' && !rel.data
		})
		.forEach(rel => {
			imageProms.push(
				new Promise((resolve, reject) => {
					if (typeof this.fs === 'function' && rel.path.indexOf('http') != 0) {
						// DESIGN: Node local-file encoding is syncronous, so we can load all images here, then call export with a callback (if any)
						try {
							let bitmap = this.fs.readFileSync(rel.path)
							rel.data = Buffer.from(bitmap).toString('base64')
							resolve('done')
						} catch (ex) {
							rel.data = IMG_BROKEN
							reject('ERROR: Unable to read media: "' + rel.path + '"\n' + ex.toString())
						}
					} else if (typeof this.fs === 'function' && rel.path.indexOf('http') == 0) {
						this.https.get(rel.path, res => {
							var rawData = ''
							res.setEncoding('binary') // IMPORTANT: Only binary encoding works
							res.on('data', chunk => (rawData += chunk))
							res.on('end', () => {
								rel.data = Buffer.from(rawData, 'binary').toString('base64')
								resolve('done')
							})
							res.on('error', ex => {
								rel.data = IMG_BROKEN
								reject('ERROR: Unable to load image: "' + rel.path + '"\n' + ex.toString())
							})
						})
					} else {
						// A: Declare XHR and onload/onerror handlers
						// DESIGN: `XMLHttpRequest()` plus `FileReader()` = Ablity to read any file into base64!
						let xhr = new XMLHttpRequest()
						xhr.onload = () => {
							let reader = new FileReader()
							reader.onloadend = () => {
								rel.data = reader.result
								if (!rel.isSvgPng) {
									resolve('done')
								} else {
									createSvgPngPreview(rel)
										.then(() => {
											resolve('done')
										})
										.catch(ex => {
											reject(ex.toString())
										})
								}
							}
							reader.readAsDataURL(xhr.response)
						}
						xhr.onerror = ex => {
							rel.data = IMG_BROKEN
							reject('ERROR: Unable to load image: "' + rel.path + '"\n' + ex.toString())
						}

						// B: Execute request
						xhr.open('GET', rel.path)
						xhr.responseType = 'blob'
						xhr.send()
					}
				})
			)
		})

	// B: SVG: base64 data still reqs a png to be generated (`isSvgPng` flag this as the preview image, not the SVG itself)
	layout.relsMedia
		.filter(rel => {
			return rel.isSvgPng && rel.data
		})
		.forEach(rel => {
			// A:
			if (typeof this.fs === 'function') throw 'Sorry, SVG is not supported in Node (more info: https://github.com/gitbrent/PptxGenJS/issues/401)'

			// B:
			imageProms.push(createSvgPngPreview(rel))
		})

	return imageProms
}

function createSvgPngPreview(rel: ISlideRelMedia): Promise<string> {
	return new Promise((resolve, reject) => {<commit_msg>fixed "this" issues with new IIFE build<commit_after>/**
 * PptxGenJS: Media Methods
 */

import { IMG_BROKEN } from './core-enums'
import { ISlide, ISlideLayout, ISlideRelMedia } from './core-interfaces'

/**
* Encode Image/Audio/Video into base64
*/
export function encodeSlideMediaRels(layout: ISlide | ISlideLayout): Promise<string>[] {
	let imageProms: Promise<string>[] = []

	// A: Read/Encode each audio/image/video thats not already encoded (eg: base64 provided by user)
	layout.relsMedia
		.filter(rel => {
			return rel.type != 'online' && !rel.data
		})
		.forEach(rel => {
			imageProms.push(
				new Promise((resolve, reject) => {
					if (this && typeof this.fs === 'function' && rel.path.indexOf('http') != 0) {
						// DESIGN: Node local-file encoding is syncronous, so we can load all images here, then call export with a callback (if any)
						try {
							let bitmap = this.fs.readFileSync(rel.path)
							rel.data = Buffer.from(bitmap).toString('base64')
							resolve('done')
						} catch (ex) {
							rel.data = IMG_BROKEN
							reject('ERROR: Unable to read media: "' + rel.path + '"\n' + ex.toString())
						}
					} else if (this && typeof this.fs === 'function' && rel.path.indexOf('http') == 0) {
						this.https.get(rel.path, res => {
							var rawData = ''
							res.setEncoding('binary') // IMPORTANT: Only binary encoding works
							res.on('data', chunk => (rawData += chunk))
							res.on('end', () => {
								rel.data = Buffer.from(rawData, 'binary').toString('base64')
								resolve('done')
							})
							res.on('error', ex => {
								rel.data = IMG_BROKEN
								reject('ERROR: Unable to load image: "' + rel.path + '"\n' + ex.toString())
							})
						})
					} else {
						// A: Declare XHR and onload/onerror handlers
						// DESIGN: `XMLHttpRequest()` plus `FileReader()` = Ablity to read any file into base64!
						let xhr = new XMLHttpRequest()
						xhr.onload = () => {
							let reader = new FileReader()
							reader.onloadend = () => {
								rel.data = reader.result
								if (!rel.isSvgPng) {
									resolve('done')
								} else {
									createSvgPngPreview(rel)
										.then(() => {
											resolve('done')
										})
										.catch(ex => {
											reject(ex.toString())
										})
								}
							}
							reader.readAsDataURL(xhr.response)
						}
						xhr.onerror = ex => {
							rel.data = IMG_BROKEN
							reject('ERROR: Unable to load image: "' + rel.path + '"\n' + ex.toString())
						}

						// B: Execute request
						xhr.open('GET', rel.path)
						xhr.responseType = 'blob'
						xhr.send()
					}
				})
			)
		})

	// B: SVG: base64 data still reqs a png to be generated (`isSvgPng` flag this as the preview image, not the SVG itself)
	layout.relsMedia
		.filter(rel => {
			return rel.isSvgPng && rel.data
		})
		.forEach(rel => {
			// A:
			if (this && typeof this.fs === 'function') throw 'Sorry, SVG is not supported in Node (more info: https://github.com/gitbrent/PptxGenJS/issues/401)'

			// B:
			imageProms.push(createSvgPngPreview(rel))
		})

	return imageProms
}

function createSvgPngPreview(rel: ISlideRelMedia): Promise<string> {
	return new Promise((resolve, reject) => {
{'fraction_non_alphanumeric': 0.15469093191088798, 'fraction_numerical': 0.005334170065892689, 'mean_word_length': 2.3891547049441786, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6296401', 'n_tokens_mistral': 2868, 'n_tokens_neox': 2089, 'n_words': 511}