🎨 chore: sanitize slugs to handle not compatible usernames

This commit is contained in:
Khaled FERJANI
2025-10-15 13:48:37 +02:00
parent 489a61c172
commit 4f97b56c11
3 changed files with 17 additions and 5 deletions
@@ -12,6 +12,7 @@ import {
DEFAULT_COMPANY,
getDriveToken,
nodeReadableToWebReadable,
sanitizeSlug,
} from "./utils";
const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
@@ -99,7 +100,7 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
userFilesObjects.push(fileObject);
if (!dryRun) {
const cozyUrl = `${userId}.${COZY_DOMAIN}`;
const cozyUrl = `${sanitizeSlug(userId)}.${COZY_DOMAIN}`;
const userToken = await getDriveToken(cozyUrl);
const client = new CozyClient({
uri: `https://${cozyUrl}`,
@@ -161,7 +162,7 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
console.log(`\n✅ File migrated successfully: ${fileObject.name}`);
} else {
console.log(
`[DRY-RUN] Would create Cozy instance for user ${user.email_canonical}`,
`[DRY-RUN] Would migrate ${user.email_canonical} file: ${fileObject.name}`,
);
}
} catch (error) {
@@ -78,7 +78,10 @@ const migrateUsersCommand: yargs.CommandModule<unknown, unknown> = {
// Even if error, continue to next user
}
} else {
console.log(`[DRY-RUN] Would create Cozy instance for user ${user.email_canonical}`);
console.log(
`[DRY-RUN] Would create Cozy instance for user ${user.email_canonical} with the following params`,
userObject
);
}
}
});
@@ -49,7 +49,7 @@ export async function createCozyInstance(user: {
`${COZY_MANAGER_URL}/instances`,
{
offer: COZY_OFFER,
slug: user.id,
slug: sanitizeSlug(user.id),
domain: COZY_DOMAIN,
email: user.email,
public_name: user.name,
@@ -149,7 +149,7 @@ export async function uploadFile(
userToken: string,
fileReadable: ReadableStream<any>,
) {
const baseUrl = `https://${userId}.${COZY_DOMAIN}/files/${fileDirPath}`;
const baseUrl = `https://${sanitizeSlug(userId)}.${COZY_DOMAIN}/files/${fileDirPath}`;
const params = {
Name: fileName,
Type: "file",
@@ -169,3 +169,11 @@ export async function uploadFile(
} as RequestInit);
return resp;
}
/**
* Remove dots from userId to match the slug format
*
* @param {string} userId - the username to clean
* @returns {string} - the sanitized slug
*/
export const sanitizeSlug = (userId: string): string => userId.replaceAll(/\./g, "");