muaz-khan / RecordRTC

RecordRTC is WebRTC JavaScript library for audio/video as well as screen activity recording. It supports Chrome, Firefox, Opera, Android, and Microsoft Edge. Platforms: Linux, Mac and Windows.
https://www.webrtc-experiment.com/RecordRTC/
MIT License
6.49k stars 1.75k forks source link

RecordRTC.invokeSaveAsDialog mishandles dots in filename #756

Open realies opened 3 years ago

realies commented 3 years ago

test.test.test.webm downloads as test.test, is there any way to escape dots in the filename?

Firionus commented 2 years ago

I can confirm this bug.

The behavior can be traced to https://github.com/muaz-khan/RecordRTC/blob/ebae1da10f50a521000cbcced5c5adfb96f32411/dev/Cross-Browser-Declarations.js#L160-L164

The easiest fix would be to change it to

var splitted = fileName.split('.');
fileName = splitted.slice(0, -1).join('.'); // all except last element
fileExtension = splitted[splitted.length - 1]; // last element

Then, test.test.test.webm would result in the expected filename of test.test.test.webm.

But what if you want to have a dot in your filename, but also want the library to pick the file extension for you? For example if you want to do a timestamp with milliseconds like this: 2021-10-17T122910.042 and the library should then pick the best extension, resulting in a filename of e.g. 2021-10-17T122910.042.webm.

To accomodate this use case in a backwards-compatible manner, I'd propose adding an optional parameter to the function signature:

/**
 * @param {Blob} file - File or Blob object. This parameter is required.
 * @param {string} fileName - Optional file name e.g. "Recorded-Video.webm"
 * @param {string} appendFileExtension - Optional. Set true to always append a file extension to the 
 * fileName, even if it contains a dot.
 * @example
 * invokeSaveAsDialog(blob or file, [optional] fileName);
 * @see {@link https://github.com/muaz-khan/RecordRTC|RecordRTC Source Code}
 */
function invokeSaveAsDialog(file, fileName, appendFileExtension = false)

Thoughts?