Pages corrections
This commit is contained in:
parent
f006aa73b5
commit
fba75ce4a6
52
lib/s3.ts
52
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 }>> {
|
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
|
// Enable debug logging only in development
|
||||||
const prefixWithSlash = `user-${userId}/${normalizedFolder}/`;
|
const isDebug = process.env.NODE_ENV === 'development';
|
||||||
const prefixWithoutSlash = `user-${userId}/${normalizedFolder}`;
|
|
||||||
|
|
||||||
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({
|
const command = new ListObjectsV2Command({
|
||||||
Bucket: S3_CONFIG.bucket,
|
Bucket: S3_CONFIG.bucket,
|
||||||
Prefix: prefixWithSlash,
|
Prefix: prefix,
|
||||||
// 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}`);
|
if (isDebug) {
|
||||||
|
console.log(`[listUserObjects] Found ${objects.length} raw objects for prefix ${prefix}`);
|
||||||
// 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 our prefix (with or without slash)
|
// - Objects that don't match our prefix
|
||||||
const filtered = objects
|
const filtered = objects
|
||||||
.filter(obj => {
|
.filter(obj => {
|
||||||
if (!obj.Key) {
|
if (!obj.Key) {
|
||||||
console.log(`[listUserObjects] Skipping object with no Key`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Exclude folder markers (end with /)
|
// Exclude folder markers (end with /)
|
||||||
if (obj.Key.endsWith('/')) {
|
if (obj.Key.endsWith('/')) {
|
||||||
console.log(`[listUserObjects] Skipping folder marker: ${obj.Key}`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Exclude placeholder files
|
// Exclude placeholder files
|
||||||
if (obj.Key.includes('.placeholder')) {
|
if (obj.Key.includes('.placeholder')) {
|
||||||
console.log(`[listUserObjects] Skipping placeholder: ${obj.Key}`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Ensure it matches our prefix (with or without slash)
|
// Ensure it matches our prefix
|
||||||
if (!obj.Key.startsWith(prefixWithSlash) && !obj.Key.startsWith(prefixWithoutSlash)) {
|
if (!obj.Key.startsWith(prefix)) {
|
||||||
console.log(`[listUserObjects] Key doesn't match prefix: ${obj.Key} (prefixes: ${prefixWithSlash} or ${prefixWithoutSlash})`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Additional check: ensure it's actually in the folder (not a subfolder)
|
// Additional check: ensure it's actually in the folder (not a subfolder)
|
||||||
const keyWithoutPrefix = obj.Key.startsWith(prefixWithSlash)
|
const keyWithoutPrefix = obj.Key.substring(prefix.length);
|
||||||
? 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 there's another / in the remaining path, it's in a subfolder, skip it
|
||||||
if (keyWithoutPrefix.includes('/')) {
|
if (keyWithoutPrefix.includes('/')) {
|
||||||
console.log(`[listUserObjects] Skipping subfolder file: ${obj.Key}`);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,9 +172,8 @@ export async function listUserObjects(userId: string, folder: string): Promise<A
|
|||||||
lastModified: obj.LastModified
|
lastModified: obj.LastModified
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
if (isDebug) {
|
||||||
console.log(`[listUserObjects] Returning ${filtered.length} filtered objects`);
|
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;
|
return filtered;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user