Skip to content

Instantly share code, notes, and snippets.

@ozturkkl
Created November 7, 2024 19:17
Show Gist options
  • Save ozturkkl/3d1660e8911906d38b9f5b7db2beaf2c to your computer and use it in GitHub Desktop.
Save ozturkkl/3d1660e8911906d38b9f5b7db2beaf2c to your computer and use it in GitHub Desktop.
(async () => {
// Create a handle to the OPFS root directory
const rootHandle = await navigator.storage.getDirectory();
// Function to recursively read all files in the OPFS
async function readFiles(directoryHandle, path = '') {
let fileContent = '';
for await (const [name, handle] of directoryHandle.entries()) {
if (handle.kind === 'directory') {
// Recursively read subdirectories
fileContent += await readFiles(handle, path + name + '/');
} else if (handle.kind === 'file') {
// Check if the file is an .mkv, if so, just write the name
if (name.endsWith('.mkv') || name.endsWith('.debackup')) {
fileContent += `Path: ${path + name} (binary or not readable file, skipped)\n\n`;
} else {
// Read non-binary file content
const file = await handle.getFile();
const text = await file.text();
fileContent += `Path: ${path + name}\n${text}\n\n`;
}
}
}
return fileContent;
}
// Read all files and prepare the final content
const content = await readFiles(rootHandle);
// Create a blob and trigger a download
const blob = new Blob([content], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'opfs_files.txt';
a.click();
// Cleanup URL object
URL.revokeObjectURL(url);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment