Skip to content

Commit

Permalink
add mimeType decoded as base64 and separate by hypen ('-') in interna…
Browse files Browse the repository at this point in the history
…lCID
  • Loading branch information
dekiakbar committed Jan 28, 2024
1 parent 2333891 commit 706fbb8
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/file/service/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class FileService {
const userFiles = await Promise.all(
ipfsFiles.map(async (file) => {
file.userId = user.id;
file.internalCid = this.generateInternalCid();
file.internalCid = this.generateInternalCid(file.mimeType);
return file;
}),
);
Expand Down Expand Up @@ -189,18 +189,33 @@ export class FileService {
await file.destroy();
}

generateInternalCid(): string {
/**
* (Only use in nextJS frontend)
* Internal CID format : base64('mimeType')-randomString
* include mimeType in Internal CID to make frontend easy recognized the file,
* and know which media player should be rendered.
*
* @param mimeType mime type, i.e: vide/mp4
*
* @returns {string}
*/
generateInternalCid(mimeType: string): string {
const separator = '-';
const length = this.configService.get('INTERNAL_CID_LENGTH');
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
let randomString = '';

for (let i = 0; i < length; i++) {
result += characters.charAt(
randomString += characters.charAt(
Math.floor(Math.random() * characters.length),
);
}

result =
Buffer.from(mimeType).toString('base64') + separator + randomString;

return result;
}

Expand Down

0 comments on commit 706fbb8

Please sign in to comment.