Fix folder display and add validation to file listing operations

- Handle folder names properly in getDisplayName by preserving trailing slash
- Add validation to skip invalid objects during listing
- Add default values for missing object properties (size, lastModified, etag)
- Add debug logging for listObjectsV2 operations to help troubleshoot issues
- Add null checks in deleteFolder to prevent errors with invalid objects
This commit is contained in:
Matt 2025-06-04 17:09:28 +02:00
parent 39ddebe259
commit 254cfa63a4
2 changed files with 30 additions and 11 deletions

View File

@ -78,6 +78,12 @@ function getFileIcon(filename: string): string {
} }
function getDisplayName(filepath: string): string { function getDisplayName(filepath: string): string {
// Handle folders (ending with /)
if (filepath.endsWith('/')) {
const parts = filepath.slice(0, -1).split('/');
return parts[parts.length - 1] + '/';
}
// Get just the filename from the full path // Get just the filename from the full path
const parts = filepath.split('/'); const parts = filepath.split('/');
const filename = parts[parts.length - 1]; const filename = parts[parts.length - 1];

View File

@ -26,12 +26,23 @@ export const listFiles = async (prefix: string = '', recursive: boolean = false)
try { try {
const stream = client.listObjectsV2(bucketName, prefix, recursive); const stream = client.listObjectsV2(bucketName, prefix, recursive);
console.log(`Starting listObjectsV2 with prefix: "${prefix}", recursive: ${recursive}`);
stream.on('data', (obj) => { stream.on('data', (obj) => {
// Skip objects without a name
if (!obj || typeof obj.name !== 'string') {
console.log('Skipping invalid object:', obj);
return;
}
console.log(`Object received: "${obj.name}", size: ${obj.size}, etag: ${obj.etag}`);
if (!recursive) { if (!recursive) {
if (prefix) { if (prefix) {
// Extract folder structure when inside a folder // Extract folder structure when inside a folder
const relativePath = obj.name.substring(prefix.length); const relativePath = obj.name.substring(prefix.length);
if (!relativePath) return; // Skip if no relative path
const firstSlash = relativePath.indexOf('/'); const firstSlash = relativePath.indexOf('/');
if (firstSlash > -1) { if (firstSlash > -1) {
@ -42,9 +53,9 @@ export const listFiles = async (prefix: string = '', recursive: boolean = false)
// This is a file in the current folder // This is a file in the current folder
files.push({ files.push({
name: obj.name, name: obj.name,
size: obj.size, size: obj.size || 0,
lastModified: obj.lastModified, lastModified: obj.lastModified || new Date(),
etag: obj.etag, etag: obj.etag || '',
isFolder: false, isFolder: false,
}); });
} }
@ -53,7 +64,7 @@ export const listFiles = async (prefix: string = '', recursive: boolean = false)
const firstSlash = obj.name.indexOf('/'); const firstSlash = obj.name.indexOf('/');
if (obj.name.endsWith('/')) { if (obj.name.endsWith('/')) {
// This is a folder placeholder // This is a folder placeholder created by createFolder
folders.add(obj.name); folders.add(obj.name);
} else if (firstSlash > -1) { } else if (firstSlash > -1) {
// This is inside a folder, extract the folder // This is inside a folder, extract the folder
@ -63,9 +74,9 @@ export const listFiles = async (prefix: string = '', recursive: boolean = false)
// This is a file at root // This is a file at root
files.push({ files.push({
name: obj.name, name: obj.name,
size: obj.size, size: obj.size || 0,
lastModified: obj.lastModified, lastModified: obj.lastModified || new Date(),
etag: obj.etag, etag: obj.etag || '',
isFolder: false, isFolder: false,
}); });
} }
@ -75,9 +86,9 @@ export const listFiles = async (prefix: string = '', recursive: boolean = false)
if (!obj.name.endsWith('/')) { if (!obj.name.endsWith('/')) {
files.push({ files.push({
name: obj.name, name: obj.name,
size: obj.size, size: obj.size || 0,
lastModified: obj.lastModified, lastModified: obj.lastModified || new Date(),
etag: obj.etag, etag: obj.etag || '',
isFolder: false, isFolder: false,
}); });
} }
@ -154,7 +165,9 @@ export const deleteFolder = async (folderPath: string) => {
const stream = client.listObjectsV2(bucketName, folderPath, true); const stream = client.listObjectsV2(bucketName, folderPath, true);
stream.on('data', (obj) => { stream.on('data', (obj) => {
objectsList.push(obj.name); if (obj && obj.name) {
objectsList.push(obj.name);
}
}); });
stream.on('error', reject); stream.on('error', reject);