Pages corrections
This commit is contained in:
parent
02c8aea589
commit
f006aa73b5
75
lib/s3.ts
75
lib/s3.ts
@ -112,30 +112,80 @@ export async function deleteObject(key: string): Promise<void> {
|
|||||||
export async function listUserObjects(userId: string, folder: string): Promise<Array<{ key: string; name: string; size?: number; lastModified?: Date }>> {
|
export async function listUserObjects(userId: string, folder: string): Promise<Array<{ key: string; name: string; size?: number; lastModified?: Date }>> {
|
||||||
// Normalize folder name to lowercase for consistency
|
// Normalize folder name to lowercase for consistency
|
||||||
const normalizedFolder = folder.toLowerCase();
|
const normalizedFolder = folder.toLowerCase();
|
||||||
const prefix = `user-${userId}/${normalizedFolder}/`;
|
|
||||||
|
// Try both with and without trailing slash to handle different MinIO configurations
|
||||||
|
const prefixWithSlash = `user-${userId}/${normalizedFolder}/`;
|
||||||
|
const prefixWithoutSlash = `user-${userId}/${normalizedFolder}`;
|
||||||
|
|
||||||
|
console.log(`[listUserObjects] Listing objects with prefix: ${prefixWithSlash} in bucket: ${S3_CONFIG.bucket}`);
|
||||||
|
|
||||||
const command = new ListObjectsV2Command({
|
const command = new ListObjectsV2Command({
|
||||||
Bucket: S3_CONFIG.bucket,
|
Bucket: S3_CONFIG.bucket,
|
||||||
Prefix: prefix,
|
Prefix: prefixWithSlash,
|
||||||
// Don't use Delimiter to get all objects, not just "folders"
|
// Don't use Delimiter to get all objects, not just "folders"
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await s3Client.send(command);
|
const response = await s3Client.send(command);
|
||||||
const objects = response.Contents || [];
|
const objects = response.Contents || [];
|
||||||
|
|
||||||
|
console.log(`[listUserObjects] Found ${objects.length} raw objects for prefix ${prefixWithSlash}`);
|
||||||
|
|
||||||
|
// Log all keys for debugging
|
||||||
|
if (objects.length > 0) {
|
||||||
|
console.log(`[listUserObjects] Raw object keys:`, objects.map(obj => obj.Key));
|
||||||
|
} else {
|
||||||
|
// If no objects found with slash, try without slash
|
||||||
|
console.log(`[listUserObjects] No objects found with slash, trying without slash: ${prefixWithoutSlash}`);
|
||||||
|
const commandWithoutSlash = new ListObjectsV2Command({
|
||||||
|
Bucket: S3_CONFIG.bucket,
|
||||||
|
Prefix: prefixWithoutSlash,
|
||||||
|
});
|
||||||
|
const responseWithoutSlash = await s3Client.send(commandWithoutSlash);
|
||||||
|
const objectsWithoutSlash = responseWithoutSlash.Contents || [];
|
||||||
|
console.log(`[listUserObjects] Found ${objectsWithoutSlash.length} objects without slash`);
|
||||||
|
if (objectsWithoutSlash.length > 0) {
|
||||||
|
console.log(`[listUserObjects] Raw object keys (no slash):`, objectsWithoutSlash.map(obj => obj.Key));
|
||||||
|
// Merge results
|
||||||
|
objects.push(...objectsWithoutSlash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Filter out:
|
// Filter out:
|
||||||
// - Objects that are "folders" (end with /)
|
// - Objects that are "folders" (end with /)
|
||||||
// - Placeholder files
|
// - Placeholder files
|
||||||
// - Objects that don't match the exact prefix (shouldn't happen but safety check)
|
// - Objects that don't match our prefix (with or without slash)
|
||||||
return objects
|
const filtered = objects
|
||||||
.filter(obj => {
|
.filter(obj => {
|
||||||
if (!obj.Key) return false;
|
if (!obj.Key) {
|
||||||
|
console.log(`[listUserObjects] Skipping object with no Key`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
// Exclude folder markers (end with /)
|
// Exclude folder markers (end with /)
|
||||||
if (obj.Key.endsWith('/')) return false;
|
if (obj.Key.endsWith('/')) {
|
||||||
|
console.log(`[listUserObjects] Skipping folder marker: ${obj.Key}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
// Exclude placeholder files
|
// Exclude placeholder files
|
||||||
if (obj.Key.includes('.placeholder')) return false;
|
if (obj.Key.includes('.placeholder')) {
|
||||||
// Ensure it matches our prefix
|
console.log(`[listUserObjects] Skipping placeholder: ${obj.Key}`);
|
||||||
if (!obj.Key.startsWith(prefix)) return false;
|
return false;
|
||||||
|
}
|
||||||
|
// Ensure it matches our prefix (with or without slash)
|
||||||
|
if (!obj.Key.startsWith(prefixWithSlash) && !obj.Key.startsWith(prefixWithoutSlash)) {
|
||||||
|
console.log(`[listUserObjects] Key doesn't match prefix: ${obj.Key} (prefixes: ${prefixWithSlash} or ${prefixWithoutSlash})`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Additional check: ensure it's actually in the folder (not a subfolder)
|
||||||
|
const keyWithoutPrefix = obj.Key.startsWith(prefixWithSlash)
|
||||||
|
? obj.Key.substring(prefixWithSlash.length)
|
||||||
|
: obj.Key.substring(prefixWithoutSlash.length + 1); // +1 for the / after folder name
|
||||||
|
|
||||||
|
// If there's another / in the remaining path, it's in a subfolder, skip it
|
||||||
|
if (keyWithoutPrefix.includes('/')) {
|
||||||
|
console.log(`[listUserObjects] Skipping subfolder file: ${obj.Key}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.map(obj => ({
|
.map(obj => ({
|
||||||
@ -144,6 +194,13 @@ export async function listUserObjects(userId: string, folder: string): Promise<A
|
|||||||
size: obj.Size,
|
size: obj.Size,
|
||||||
lastModified: obj.LastModified
|
lastModified: obj.LastModified
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
console.log(`[listUserObjects] Returning ${filtered.length} filtered objects`);
|
||||||
|
if (filtered.length > 0) {
|
||||||
|
console.log(`[listUserObjects] Filtered object keys:`, filtered.map(obj => obj.key));
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user