mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-12-25 13:08:51 +00:00
Create base migration logic
This commit is contained in:
parent
26ff8dfdff
commit
4d1d97ad86
@ -47,5 +47,7 @@ export async function startMigration(args: String[]) {
|
||||
// Do the migration
|
||||
await migration.func();
|
||||
|
||||
console.info("Finished.")
|
||||
|
||||
process.exit(0);
|
||||
}
|
63
src/migrations/AccountImageMigration.ts
Normal file
63
src/migrations/AccountImageMigration.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,6 +4,8 @@
|
||||
* @author Pierre Hubert
|
||||
*/
|
||||
|
||||
import { accountImageMigration } from "./AccountImageMigration";
|
||||
|
||||
interface Migration {
|
||||
id: string,
|
||||
func: () => Promise<void>
|
||||
@ -14,7 +16,7 @@ export const MigrationsList : Migration[] = [
|
||||
// Account image migration (files -> database, May 2020)
|
||||
{
|
||||
id: "account_image_2020",
|
||||
func: undefined
|
||||
func: accountImageMigration
|
||||
}
|
||||
|
||||
]
|
Loading…
Reference in New Issue
Block a user