Skip to content

Commit

Permalink
Merge pull request #158 from Open-Model-Initiative/156-fix-image-mode…
Browse files Browse the repository at this point in the history
…ration-first-load-with-no-uploads

Call recursive mkdir on image moderation access to ensure folders are made if they don't already exist
  • Loading branch information
fearnworks authored Feb 17, 2025
2 parents 4160bb5 + 28fa2b3 commit c105341
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 74 deletions.
1 change: 0 additions & 1 deletion modules/odr_frontend/src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export const actions = {
await mkdir(REJECTED_DIR, { recursive: true });
await mkdir(FLAGGED_DIR, { recursive: true });


if (process.env.NODE_ENV === 'production') {
// S3 upload for production
if (!s3Client) {
Expand Down
150 changes: 77 additions & 73 deletions modules/odr_frontend/src/routes/admin/moderation/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
import { readdir, readFile, rename } from 'fs/promises';
import { mkdir, readdir, readFile, rename } from 'fs/promises';
import { join } from 'path';
import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';
Expand Down Expand Up @@ -57,7 +57,6 @@ async function getS3ObjectContent(key: string) {
return response.Body?.transformToString();
}


interface ImageData {
filename: string;
previewUrl: string;
Expand Down Expand Up @@ -115,6 +114,11 @@ export const load: PageServerLoad = async ({ url }) => {
console.log(`Grabbed image data from S3`);
} else {
// Local file system logic
await mkdir(PENDING_DIR, { recursive: true });
await mkdir(ACCEPTED_DIR, { recursive: true });
await mkdir(REJECTED_DIR, { recursive: true });
await mkdir(FLAGGED_DIR, { recursive: true });

const files = await readdir(PENDING_DIR);
imageFiles = files.filter(file => file.endsWith('.jpg'));
totalPages = Math.ceil(imageFiles.length / ITEMS_PER_PAGE);
Expand Down Expand Up @@ -151,85 +155,85 @@ export const load: PageServerLoad = async ({ url }) => {
};

export const actions = {
accept: async ({ request }) => {
const formData = await request.formData();
const filename = formData.get('filename');
if (typeof filename !== 'string') {
throw error(400, 'Invalid filename');
}
await moveFile(filename, 'accepted');
return { success: true };
},

reject: async ({ request }) => {
const formData = await request.formData();
const filename = formData.get('filename');
if (typeof filename !== 'string') {
throw error(400, 'Invalid filename');
}
await moveFile(filename, 'rejected');
return { success: true };
},

flag: async ({ request }) => {
const formData = await request.formData();
const filename = formData.get('filename');
if (typeof filename !== 'string') {
throw error(400, 'Invalid filename');
}
await moveFile(filename, 'flagged');
return { success: true };
accept: async ({ request }) => {
const formData = await request.formData();
const filename = formData.get('filename');
if (typeof filename !== 'string') {
throw error(400, 'Invalid filename');
}
};
await moveFile(filename, 'accepted');
return { success: true };
},

reject: async ({ request }) => {
const formData = await request.formData();
const filename = formData.get('filename');
if (typeof filename !== 'string') {
throw error(400, 'Invalid filename');
}
await moveFile(filename, 'rejected');
return { success: true };
},

flag: async ({ request }) => {
const formData = await request.formData();
const filename = formData.get('filename');
if (typeof filename !== 'string') {
throw error(400, 'Invalid filename');
}
await moveFile(filename, 'flagged');
return { success: true };
}
};

async function moveFile(filename: string, destFolder: string) {
if (process.env.NODE_ENV === 'production') {
// S3 move logic
if (!s3Client) {
throw new Error('S3 client is not initialized');
}
async function moveFile(filename: string, destFolder: string) {
if (process.env.NODE_ENV === 'production') {
// S3 move logic
if (!s3Client) {
throw new Error('S3 client is not initialized');
}

const sourceKey = `pending/${filename}`;
const destKey = `${destFolder}/${filename}`;
const sourceKey = `pending/${filename}`;
const destKey = `${destFolder}/${filename}`;

const fileExtensions = ['jpg', 'json', 'dng'];
const fileExtensions = ['jpg', 'json', 'dng'];

await Promise.all(fileExtensions.map(async (ext) => {
const sourceKeyExt = sourceKey.replace(/\.[^.]+$/, `.${ext}`);
const destKeyExt = destKey.replace(/\.[^.]+$/, `.${ext}`);
await Promise.all(fileExtensions.map(async (ext) => {
const sourceKeyExt = sourceKey.replace(/\.[^.]+$/, `.${ext}`);
const destKeyExt = destKey.replace(/\.[^.]+$/, `.${ext}`);

await s3Client.send(new CopyObjectCommand({
Bucket: S3_BUCKET_NAME,
CopySource: `${S3_BUCKET_NAME}/${sourceKeyExt}`,
Key: destKeyExt,
}));
await s3Client.send(new CopyObjectCommand({
Bucket: S3_BUCKET_NAME,
CopySource: `${S3_BUCKET_NAME}/${sourceKeyExt}`,
Key: destKeyExt,
}));

await s3Client.send(new DeleteObjectCommand({
Bucket: S3_BUCKET_NAME,
Key: sourceKeyExt,
}));
await s3Client.send(new DeleteObjectCommand({
Bucket: S3_BUCKET_NAME,
Key: sourceKeyExt,
}));
}));
} else {
// Local file system move logic
const sourcePath = join(PENDING_DIR, filename);
let destPath;
if (destFolder === 'accepted') {
destPath = join(ACCEPTED_DIR, filename)
} else if (destFolder === 'rejected') {
destPath = join(REJECTED_DIR, filename)
} else if (destFolder === 'flagged') {
destPath = join(FLAGGED_DIR, filename)
} else {
// Local file system move logic
const sourcePath = join(PENDING_DIR, filename);
let destPath;
if (destFolder === 'accepted') {
destPath = join(ACCEPTED_DIR, filename)
} else if (destFolder === 'rejected') {
destPath = join(REJECTED_DIR, filename)
} else if (destFolder === 'flagged') {
destPath = join(FLAGGED_DIR, filename)
} else {
throw new Error(`Invalid destination folder: ${destFolder}`);
}

await rename(sourcePath, destPath);

// Move associated JSON and DNG files
['json', 'dng'].forEach(async (ext) => {
const sourcePathExt = sourcePath.replace('.jpg', `.${ext}`);
const destPathExt = destPath.replace('.jpg', `.${ext}`);
await rename(sourcePathExt, destPathExt);
});
throw new Error(`Invalid destination folder: ${destFolder}`);
}

await rename(sourcePath, destPath);

// Move associated JSON and DNG files
['json', 'dng'].forEach(async (ext) => {
const sourcePathExt = sourcePath.replace('.jpg', `.${ext}`);
const destPathExt = destPath.replace('.jpg', `.${ext}`);
await rename(sourcePathExt, destPathExt);
});
}
}

0 comments on commit c105341

Please sign in to comment.