1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-04-30 00:00:54 +00:00
comunicapiv2/src/migrations/AccountImageMigration.ts

63 lines
1.9 KiB
TypeScript

/**
* 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);
}
}
}