1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Create base migration logic

This commit is contained in:
Pierre HUBERT 2020-05-25 18:58:26 +02:00
parent 26ff8dfdff
commit 4d1d97ad86
3 changed files with 68 additions and 1 deletions

View File

@ -47,5 +47,7 @@ export async function startMigration(args: String[]) {
// Do the migration // Do the migration
await migration.func(); await migration.func();
console.info("Finished.")
process.exit(0); process.exit(0);
} }

View File

@ -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);
}
}
}

View File

@ -4,6 +4,8 @@
* @author Pierre Hubert * @author Pierre Hubert
*/ */
import { accountImageMigration } from "./AccountImageMigration";
interface Migration { interface Migration {
id: string, id: string,
func: () => Promise<void> func: () => Promise<void>
@ -14,7 +16,7 @@ export const MigrationsList : Migration[] = [
// Account image migration (files -> database, May 2020) // Account image migration (files -> database, May 2020)
{ {
id: "account_image_2020", id: "account_image_2020",
func: undefined func: accountImageMigration
} }
] ]