Pages corrections
This commit is contained in:
parent
f006aa73b5
commit
fba75ce4a6
54
lib/s3.ts
54
lib/s3.ts
@ -112,77 +112,54 @@ 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 }>> {
|
||||
// Normalize folder name to lowercase for consistency
|
||||
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}`;
|
||||
// Enable debug logging only in development
|
||||
const isDebug = process.env.NODE_ENV === 'development';
|
||||
|
||||
console.log(`[listUserObjects] Listing objects with prefix: ${prefixWithSlash} in bucket: ${S3_CONFIG.bucket}`);
|
||||
if (isDebug) {
|
||||
console.log(`[listUserObjects] Listing objects with prefix: ${prefix} in bucket: ${S3_CONFIG.bucket}`);
|
||||
}
|
||||
|
||||
const command = new ListObjectsV2Command({
|
||||
Bucket: S3_CONFIG.bucket,
|
||||
Prefix: prefixWithSlash,
|
||||
Prefix: prefix,
|
||||
// Don't use Delimiter to get all objects, not just "folders"
|
||||
});
|
||||
|
||||
const response = await s3Client.send(command);
|
||||
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);
|
||||
}
|
||||
if (isDebug) {
|
||||
console.log(`[listUserObjects] Found ${objects.length} raw objects for prefix ${prefix}`);
|
||||
}
|
||||
|
||||
// Filter out:
|
||||
// - Objects that are "folders" (end with /)
|
||||
// - Placeholder files
|
||||
// - Objects that don't match our prefix (with or without slash)
|
||||
// - Objects that don't match our prefix
|
||||
const filtered = objects
|
||||
.filter(obj => {
|
||||
if (!obj.Key) {
|
||||
console.log(`[listUserObjects] Skipping object with no Key`);
|
||||
return false;
|
||||
}
|
||||
// Exclude folder markers (end with /)
|
||||
if (obj.Key.endsWith('/')) {
|
||||
console.log(`[listUserObjects] Skipping folder marker: ${obj.Key}`);
|
||||
return false;
|
||||
}
|
||||
// Exclude placeholder files
|
||||
if (obj.Key.includes('.placeholder')) {
|
||||
console.log(`[listUserObjects] Skipping placeholder: ${obj.Key}`);
|
||||
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})`);
|
||||
// Ensure it matches our prefix
|
||||
if (!obj.Key.startsWith(prefix)) {
|
||||
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
|
||||
const keyWithoutPrefix = obj.Key.substring(prefix.length);
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
@ -195,9 +172,8 @@ export async function listUserObjects(userId: string, folder: string): Promise<A
|
||||
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));
|
||||
if (isDebug) {
|
||||
console.log(`[listUserObjects] Returning ${filtered.length} filtered objects`);
|
||||
}
|
||||
|
||||
return filtered;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user