diff --git a/src/migration.ts b/src/migration.ts index b66075b..21d432c 100644 --- a/src/migration.ts +++ b/src/migration.ts @@ -47,5 +47,7 @@ export async function startMigration(args: String[]) { // Do the migration await migration.func(); + console.info("Finished.") + process.exit(0); } \ No newline at end of file diff --git a/src/migrations/AccountImageMigration.ts b/src/migrations/AccountImageMigration.ts new file mode 100644 index 0000000..14ffd31 --- /dev/null +++ b/src/migrations/AccountImageMigration.ts @@ -0,0 +1,63 @@ +/** + * The Account image migration + * + * In May 2020, I moved the references to account images + * from simple txt files to the database, in order to + * improve both performances and beauty. + * + * This migration (which should be run only once and immediatly + * after API update) ensures that the database is filled with appropriate + * account image informations) + * + * @author Pierre Hubert + */ + +import { pathUserData } from "../utils/UserDataUtils"; +import { readdirSync, readFileSync } from "fs"; +import { AccountImageVisibilityLevel } from "../entities/User"; + +const AVATARS_PATH = "avatars/"; +const AVATARS_ADDRESSES_FILES = "avatars/adresse_avatars/"; + +enum OldAccountImageVisibilityLevel { + FRIENDS = 1, + COMUNIC_USERS = 2, + EVERYONE = 3 +} + +const VisibilityLevelsMap = {}; +VisibilityLevelsMap[OldAccountImageVisibilityLevel.FRIENDS] = AccountImageVisibilityLevel.FRIENDS +VisibilityLevelsMap[OldAccountImageVisibilityLevel.COMUNIC_USERS] = AccountImageVisibilityLevel.COMUNIC_USERS +VisibilityLevelsMap[OldAccountImageVisibilityLevel.EVERYONE] = AccountImageVisibilityLevel.EVERYONE + + +export async function accountImageMigration() { + const pathFilesDatabase = pathUserData(AVATARS_ADDRESSES_FILES, true); + + // Process each file + const list = readdirSync(pathFilesDatabase); + + for (const file of list) { + + // Ingore additional files + if(file == ".htaccess" || file.endsWith(".php") || file.includes("jpg")) + continue; + + const fileContent = readFileSync(pathFilesDatabase + file); + const userID = Number(file.replace(".txt", "").replace("limit_view_", "")); + + // Account image path + if(!file.includes("limit_view_")) { + const newPath = AVATARS_PATH + fileContent; + + console.info("User " + userID + "\tAccount image: " + newPath); + } + + // Account image visibility + else { + const newVisibililty = VisibilityLevelsMap[Number(fileContent)]; + + console.info("User " + userID + "\tVisibility: " + newVisibililty); + } + } +} \ No newline at end of file diff --git a/src/migrations/MigrationsList.ts b/src/migrations/MigrationsList.ts index f63602f..7ee2c67 100644 --- a/src/migrations/MigrationsList.ts +++ b/src/migrations/MigrationsList.ts @@ -4,6 +4,8 @@ * @author Pierre Hubert */ +import { accountImageMigration } from "./AccountImageMigration"; + interface Migration { id: string, func: () => Promise @@ -14,7 +16,7 @@ export const MigrationsList : Migration[] = [ // Account image migration (files -> database, May 2020) { id: "account_image_2020", - func: undefined + func: accountImageMigration } ] \ No newline at end of file