🎨 chore: sanitize slugs to handle not compatible usernames
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
|||||||
DEFAULT_COMPANY,
|
DEFAULT_COMPANY,
|
||||||
getDriveToken,
|
getDriveToken,
|
||||||
nodeReadableToWebReadable,
|
nodeReadableToWebReadable,
|
||||||
|
sanitizeSlug,
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
|
|
||||||
const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
|
const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
|
||||||
@@ -99,7 +100,7 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
|
|||||||
userFilesObjects.push(fileObject);
|
userFilesObjects.push(fileObject);
|
||||||
|
|
||||||
if (!dryRun) {
|
if (!dryRun) {
|
||||||
const cozyUrl = `${userId}.${COZY_DOMAIN}`;
|
const cozyUrl = `${sanitizeSlug(userId)}.${COZY_DOMAIN}`;
|
||||||
const userToken = await getDriveToken(cozyUrl);
|
const userToken = await getDriveToken(cozyUrl);
|
||||||
const client = new CozyClient({
|
const client = new CozyClient({
|
||||||
uri: `https://${cozyUrl}`,
|
uri: `https://${cozyUrl}`,
|
||||||
@@ -161,7 +162,7 @@ const purgeIndexesCommand: yargs.CommandModule<unknown, unknown> = {
|
|||||||
console.log(`\n✅ File migrated successfully: ${fileObject.name}`);
|
console.log(`\n✅ File migrated successfully: ${fileObject.name}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
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) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -78,7 +78,10 @@ const migrateUsersCommand: yargs.CommandModule<unknown, unknown> = {
|
|||||||
// Even if error, continue to next user
|
// Even if error, continue to next user
|
||||||
}
|
}
|
||||||
} else {
|
} 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`,
|
`${COZY_MANAGER_URL}/instances`,
|
||||||
{
|
{
|
||||||
offer: COZY_OFFER,
|
offer: COZY_OFFER,
|
||||||
slug: user.id,
|
slug: sanitizeSlug(user.id),
|
||||||
domain: COZY_DOMAIN,
|
domain: COZY_DOMAIN,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
public_name: user.name,
|
public_name: user.name,
|
||||||
@@ -149,7 +149,7 @@ export async function uploadFile(
|
|||||||
userToken: string,
|
userToken: string,
|
||||||
fileReadable: ReadableStream<any>,
|
fileReadable: ReadableStream<any>,
|
||||||
) {
|
) {
|
||||||
const baseUrl = `https://${userId}.${COZY_DOMAIN}/files/${fileDirPath}`;
|
const baseUrl = `https://${sanitizeSlug(userId)}.${COZY_DOMAIN}/files/${fileDirPath}`;
|
||||||
const params = {
|
const params = {
|
||||||
Name: fileName,
|
Name: fileName,
|
||||||
Type: "file",
|
Type: "file",
|
||||||
@@ -169,3 +169,11 @@ export async function uploadFile(
|
|||||||
} as RequestInit);
|
} as RequestInit);
|
||||||
return resp;
|
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, "");
|
||||||
|
|||||||
Reference in New Issue
Block a user