{"version":3,"file":"fileTree.mjs","sources":["../../../lib/upload/utils/fileTree.ts"],"sourcesContent":["/*!\n * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n/**\n * Helpers to generate a file tree when the File and Directory API is used (e.g. Drag and Drop or <input type=\"file\" webkitdirectory>)\n */\n\nimport { basename } from '@nextcloud/paths'\nimport { isFileSystemDirectoryEntry, isFileSystemFileEntry } from './filesystem.ts'\n\n/**\n * This is a helper class to allow building a file tree for uploading\n * It allows to create virtual directories\n */\nexport class Directory extends File {\n\tprivate _originalName: string\n\tprivate _path: string\n\tprivate _children: Map<string, File | this>\n\n\tconstructor(path: string) {\n\t\tsuper([], basename(path), { type: 'httpd/unix-directory', lastModified: 0 })\n\t\tthis._children = new Map()\n\t\tthis._originalName = basename(path)\n\t\tthis._path = path\n\t}\n\n\tget size(): number {\n\t\treturn this.children.reduce((sum, file) => sum + file.size, 0)\n\t}\n\n\tget lastModified(): number {\n\t\treturn this.children.reduce((latest, file) => Math.max(latest, file.lastModified), 0)\n\t}\n\n\t// We need this to keep track of renamed files\n\tget originalName(): string {\n\t\treturn this._originalName\n\t}\n\n\tget children(): Array<File | Directory> {\n\t\treturn Array.from(this._children.values())\n\t}\n\n\tget webkitRelativePath(): string {\n\t\treturn this._path\n\t}\n\n\tgetChild(name: string): File | Directory | null {\n\t\treturn this._children.get(name) ?? null\n\t}\n\n\t/**\n\t * Add multiple children at once\n\t *\n\t * @param files The files to add\n\t */\n\tasync addChildren(files: Array<File | FileSystemEntry>): Promise<void> {\n\t\tfor (const file of files) {\n\t\t\tawait this.addChild(file)\n\t\t}\n\t}\n\n\t/**\n\t * Add a child to the directory.\n\t * If it is a nested child the parents will be created if not already exist.\n\t *\n\t * @param file The child to add\n\t */\n\tasync addChild(file: File | FileSystemEntry) {\n\t\tconst rootPath = this._path && `${this._path}/`\n\t\tif (isFileSystemFileEntry(file)) {\n\t\t\tfile = await new Promise<File>((resolve, reject) => (file as FileSystemFileEntry).file(resolve, reject))\n\t\t} else if (isFileSystemDirectoryEntry(file)) {\n\t\t\tconst reader = file.createReader()\n\t\t\t// `readEntries` does not necessarily return all entries at once,\n\t\t\t// e.g. Chromium returns at most 100 entries per call,\n\t\t\t// so we need to call it until it returns an empty array.\n\t\t\tconst entries: FileSystemEntry[] = []\n\t\t\tlet batch: FileSystemEntry[]\n\t\t\tdo {\n\t\t\t\tbatch = await new Promise<FileSystemEntry[]>((resolve, reject) => reader.readEntries(resolve, reject))\n\t\t\t\tentries.push(...batch)\n\t\t\t} while (batch.length > 0)\n\n\t\t\t// Create a new child directory and add the entries\n\t\t\tconst child = new Directory(`${rootPath}${file.name}`)\n\t\t\tawait child.addChildren(entries)\n\t\t\tthis._children.set(file.name, child)\n\t\t\treturn\n\t\t}\n\n\t\t// Make Typescript calm - we ensured it is not a file system entry above.\n\t\tfile = file as File\n\n\t\tconst filePath = file.webkitRelativePath ?? file.name\n\t\t// Handle plain files\n\t\tif (!filePath.includes('/')) {\n\t\t\t// Direct child of the directory\n\t\t\tthis._children.set(file.name, file)\n\t\t} else {\n\t\t\t// Check if file is a child\n\t\t\tif (!filePath.startsWith(this._path)) {\n\t\t\t\tthrow new Error(`File ${filePath} is not a child of ${this._path}`)\n\t\t\t}\n\n\t\t\t// If file is a child check if we need to nest it\n\t\t\tconst relPath = filePath.slice(rootPath.length)\n\t\t\tconst name = basename(relPath)\n\n\t\t\tif (name === relPath) {\n\t\t\t\t// It is a direct child so we can add it\n\t\t\t\tthis._children.set(name, file)\n\t\t\t} else {\n\t\t\t\t// It is not a direct child so we need to create intermediate nodes\n\t\t\t\tconst base = relPath.slice(0, relPath.indexOf('/'))\n\t\t\t\tif (this._children.has(base)) {\n\t\t\t\t\t// It is a grandchild so we can add it directly\n\t\t\t\t\tawait (this._children.get(base) as Directory).addChild(file)\n\t\t\t\t} else {\n\t\t\t\t\t// We do not know any parent of that child\n\t\t\t\t\t// so we need to add a new child on the current level\n\t\t\t\t\tconst child = new Directory(`${rootPath}${base}`)\n\t\t\t\t\tawait child.addChild(file)\n\t\t\t\t\tthis._children.set(base, child)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Interface of the internal Directory class\n */\nexport type IDirectory = Pick<Directory, keyof Directory>\n"],"names":[],"mappings":";;AAAA;AAAA;AAAA;AAAA;AAeO,MAAM,kBAAkB,KAAK;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,MAAc;AACzB,UAAM,CAAA,GAAI,SAAS,IAAI,GAAG,EAAE,MAAM,wBAAwB,cAAc,GAAG;AAC3E,SAAK,gCAAgB,IAAA;AACrB,SAAK,gBAAgB,SAAS,IAAI;AAClC,SAAK,QAAQ;AAAA,EACd;AAAA,EAEA,IAAI,OAAe;AAClB,WAAO,KAAK,SAAS,OAAO,CAAC,KAAK,SAAS,MAAM,KAAK,MAAM,CAAC;AAAA,EAC9D;AAAA,EAEA,IAAI,eAAuB;AAC1B,WAAO,KAAK,SAAS,OAAO,CAAC,QAAQ,SAAS,KAAK,IAAI,QAAQ,KAAK,YAAY,GAAG,CAAC;AAAA,EACrF;AAAA;AAAA,EAGA,IAAI,eAAuB;AAC1B,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,IAAI,WAAoC;AACvC,WAAO,MAAM,KAAK,KAAK,UAAU,QAAQ;AAAA,EAC1C;AAAA,EAEA,IAAI,qBAA6B;AAChC,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,SAAS,MAAuC;AAC/C,WAAO,KAAK,UAAU,IAAI,IAAI,KAAK;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,YAAY,OAAqD;AACtE,eAAW,QAAQ,OAAO;AACzB,YAAM,KAAK,SAAS,IAAI;AAAA,IACzB;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,SAAS,MAA8B;AAC5C,UAAM,WAAW,K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