Skip to content

Commit

Permalink
fix: limit unique filenames to be less 101
Browse files Browse the repository at this point in the history
Anki will not handle filenames that are longer. Not sure what the exact
length is but if we are below 119 it should be safer than the current
132.
  • Loading branch information
aalemayhu committed Jul 3, 2022
1 parent 961fe63 commit a9ead4d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/lib/misc/getUniqueFileName.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import getUniqueFileName from './getUniqueFileName';

describe('getUniqueFileName', () => {
test('default max is less than 101 characters', () => {
expect(getUniqueFileName('my image.jpg').length).toBeLessThan(101);
});
});
4 changes: 2 additions & 2 deletions src/lib/misc/getUniqueFileName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import crypto from 'crypto';
* @param input user supplied filename
* @returns hex digest
*/
const getUniqueFileName = (input: string) => {
const getUniqueFileName = (input: string, max: number = 100) => {
const shasum = crypto.createHash('sha512');
shasum.update(input);
return shasum.digest('hex');
return shasum.digest('hex').slice(0, max);
};

export default getUniqueFileName;

0 comments on commit a9ead4d

Please sign in to comment.