<commit_before><?php

/**
 * ownCloud - Music app
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2018, 2019
 */

namespace OCA\Music\Utility;

use \OCA\Music\AppFramework\Core\Logger;



class DetailsHelper {
	private $extractor;
	private $logger;

	public function __construct(
			Extractor $extractor,
			Logger $logger) {
		$this->extractor = $extractor;
		$this->logger = $logger;
	}

	/**
	 * @param integer $fileId
	 * @param Folder $userFolder
	 * $return array|null
	 */
	public function getDetails($fileId, $userFolder) {
		$fileNodes = $userFolder->getById($fileId);
		if (\count($fileNodes) > 0) {
			$data = $this->extractor->extract($fileNodes[0]);

			// remove intermediate arrays
			$data['comments'] = self::flattenComments($data['comments']);

			// cleanup strings from invalid characters
			\array_walk($data['audio'], ['self', 'sanitizeString']);
			\array_walk($data['comments'], ['self', 'sanitizeString']);

			$result = [
				'fileinfo' => $data['audio'],
				'tags' => $data['comments']
			];

			// binary data has to be encoded
			if (\array_key_exists('picture', $result['tags'])) {
				$result['tags']['picture'] = self::encodePictureTag($result['tags']['picture']);
			}

			// 'streams' contains duplicate data
			unset($result['fileinfo']['streams']);

			// one track number is enough
			if (\array_key_exists('track', $result['tags'])
				&& \array_key_exists('track_number', $result['tags'])) {
				unset($result['tags']['track']);
			}<commit_msg>Fix unable to open details pane if getID3 can't extract any metadata<commit_after><?php

/**
 * ownCloud - Music app
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2018, 2019
 */

namespace OCA\Music\Utility;

use \OCA\Music\AppFramework\Core\Logger;



class DetailsHelper {
	private $extractor;
	private $logger;

	public function __construct(
			Extractor $extractor,
			Logger $logger) {
		$this->extractor = $extractor;
		$this->logger = $logger;
	}

	/**
	 * @param integer $fileId
	 * @param Folder $userFolder
	 * $return array|null
	 */
	public function getDetails($fileId, $userFolder) {
		$fileNodes = $userFolder->getById($fileId);
		if (\count($fileNodes) > 0) {
			$data = $this->extractor->extract($fileNodes[0]);

			// remove intermediate arrays and map null comments into an empty array
			$data['comments'] = self::flattenComments($data['comments'] ?: []);

			// cleanup strings from invalid characters
			\array_walk($data['audio'], ['self', 'sanitizeString']);
			\array_walk($data['comments'], ['self', 'sanitizeString']);

			$result = [
				'fileinfo' => $data['audio'],
				'tags' => $data['comments']
			];

			// binary data has to be encoded
			if (\array_key_exists('picture', $result['tags'])) {
				$result['tags']['picture'] = self::encodePictureTag($result['tags']['picture']);
			}

			// 'streams' contains duplicate data
			unset($result['fileinfo']['streams']);

			// one track number is enough
			if (\array_key_exists('track', $result['tags'])
				&& \array_key_exists('track_number', $result['tags'])) {
				unset($result['tags']['track']);
			}
{'fraction_non_alphanumeric': 0.17750226791654067, 'fraction_numerical': 0.006954944058058663, 'mean_word_length': 4.136645962732919, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '4594363', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1015, 'n_words': 253}