fix: avatar dir

This commit is contained in:
Mohamed Marrouchi
2025-01-05 08:52:36 +01:00
parent c35be05416
commit e16660a0a0
10 changed files with 79 additions and 35 deletions

View File

@@ -6,12 +6,16 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import { existsSync, promises as fsPromises } from 'fs';
import { join } from 'path';
import mongoose from 'mongoose';
import attachmentSchema, {
Attachment,
} from '@/attachment/schemas/attachment.schema';
import subscriberSchema, { Subscriber } from '@/chat/schemas/subscriber.schema';
import { config } from '@/config';
import { MigrationServices } from '../types';
@@ -74,13 +78,37 @@ const unpopulateSubscriberAvatar = async ({ logger }: MigrationServices) => {
}
};
const updateOldAvatarsPath = async ({ logger }: MigrationServices) => {
const oldPath = join(process.cwd(), process.env.AVATAR_DIR || '/avatars');
if (existsSync(oldPath)) {
await fsPromises.copyFile(oldPath, config.parameters.avatarDir);
await fsPromises.unlink(oldPath);
logger.log('Avatars folder successfully moved to its new location ...');
} else {
logger.log('No old avatars folder found ...');
}
};
const restoreOldAvatarsPath = async ({ logger }: MigrationServices) => {
const oldPath = join(process.cwd(), process.env.AVATAR_DIR || '/avatars');
if (existsSync(config.parameters.avatarDir)) {
await fsPromises.copyFile(config.parameters.avatarDir, oldPath);
await fsPromises.unlink(config.parameters.avatarDir);
logger.log('Avatars folder successfully moved to its old location ...');
} else {
logger.log('No avatars folder found ...');
}
};
module.exports = {
async up(services: MigrationServices) {
await populateSubscriberAvatar(services);
await updateOldAvatarsPath(services);
return true;
},
async down(services: MigrationServices) {
await unpopulateSubscriberAvatar(services);
await restoreOldAvatarsPath(services);
return true;
},
};