'use strict'; const Promise = require("bluebird"); const memoizee = require("memoizee"); const debug = require("debug")("iso9660:object:directory"); const readExtendedAttributeRecord = require("../read/extended-attribute-record"); const readDirectoryExtent = require("../read/directory-extent"); module.exports = function(image) { const createFile = require("./file")(image); function getExtendedAttributeRecord(extentLocation, extendedAttributeRecordLength) { if (extendedAttributeRecordLength === 0) { return {}; } else { return readExtendedAttributeRecord(extentLocation, extendedAttributeRecordLength); } } return function createDirectory(directoryRecord, parent) { debug(`Creating new directory object for ${directoryRecord.identifier.filename}`); return { type: "directory", filename: directoryRecord.identifier.filename, version: directoryRecord.identifier.version, parent: parent, recordingDate: directoryRecord.recordingDate, encoding: directoryRecord.encoding, record: directoryRecord, getChildren: memoizee(function getChildren() { return Promise.try(() => { return readDirectoryExtent(image, directoryRecord.extentLocation, directoryRecord.extentSize, directoryRecord.encoding); }).then((directoryRecords) => { /* The first two records are . and .. respectively, so we'll strip these off. */ let realDirectoryRecords = directoryRecords.slice(2); return Promise.map(realDirectoryRecords, (directoryRecord) => { if (directoryRecord.fileFlags.directory) { debug(`Found directory: ${directoryRecord.identifier.filename}`); return createDirectory(directoryRecord, this); } else { debug(`Found file: ${directoryRecord.identifier.filename}`); return Promise.try(() => { return getExtendedAttributeRecord(directoryRecord.extentLocation, directoryRecord.extendedAttributeRecordLength); }).then((extendedAttributeRecord) => { directoryRecord.extendedAttributeRecord = extendedAttributeRecord; return createFile(directoryRecord, this); }); } }); }) }), getTree: function getTree() { return Promise.try(() => { return this.getChildren(); }).map((child) => { if (child.type === "directory") { return Promise.try(() => { return child.getTree(); }).then((tree) => { return Object.assign({ children: tree }, child); }); } else { return child; } }); } } } }