mirror of https://github.com/mifi/lossless-cut
parent
66f6a8e4b6
commit
a1422ad264
@ -0,0 +1,48 @@
|
||||
import { test, expect, describe } from 'vitest';
|
||||
|
||||
import fileUriRaw from './fileUri.js';
|
||||
|
||||
|
||||
describe('file uri windows only', () => {
|
||||
test('converts path to file url', () => {
|
||||
expect(fileUriRaw('C:\\Users\\sindresorhus\\dev\\te^st.jpg', true)).toEqual('file:///C:/Users/sindresorhus/dev/te%5Est.jpg');
|
||||
});
|
||||
});
|
||||
|
||||
describe('file uri non-windows', () => {
|
||||
// https://github.com/mifi/lossless-cut/issues/1941
|
||||
test('file with backslash', () => {
|
||||
expect(fileUriRaw('/has/back\\slash', false)).toEqual('file:///has/back%5Cslash');
|
||||
});
|
||||
});
|
||||
|
||||
// taken from https://github.com/sindresorhus/file-url
|
||||
describe.each([{ isWindows: false }, { isWindows: true }])('file uri both platforms isWindows=$isWindows', ({ isWindows }) => {
|
||||
const fileUri = (path) => fileUriRaw(path, isWindows);
|
||||
|
||||
test('converts path to file url', () => {
|
||||
expect(fileUri('/test.jpg')).toMatch(/file:\/{3}test\.jpg/);
|
||||
|
||||
expect(fileUri('/Users/sindresorhus/dev/te^st.jpg')).toEqual('file:///Users/sindresorhus/dev/te%5Est.jpg');
|
||||
});
|
||||
|
||||
test('escapes more special characters in path', () => {
|
||||
expect(fileUri('/a?!@#$%^&\'";<>')).toEqual('file:///a%3F!@%23$%25%5E&\'%22;%3C%3E');
|
||||
});
|
||||
|
||||
test('escapes whitespace characters in path', () => {
|
||||
expect(fileUri('/file with\r\nnewline')).toEqual('file:///file%20with%0D%0Anewline');
|
||||
});
|
||||
|
||||
test('relative path', () => {
|
||||
expect(fileUri('relative/test.jpg')).toEqual('file:///relative/test.jpg');
|
||||
});
|
||||
|
||||
test('empty', () => {
|
||||
expect(fileUri('')).toEqual('file:///');
|
||||
});
|
||||
|
||||
test('slash', () => {
|
||||
expect(fileUri('/')).toEqual('file:///');
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,17 @@
|
||||
export default function fileUri(filePath: string, isWindows: boolean) {
|
||||
let pathName = filePath;
|
||||
|
||||
if (isWindows) {
|
||||
pathName = pathName.replaceAll('\\', '/');
|
||||
}
|
||||
|
||||
// Windows drive letter must be prefixed with a slash.
|
||||
// also relative paths will be converted to absolute
|
||||
if (pathName[0] !== '/') {
|
||||
pathName = `/${pathName}`;
|
||||
}
|
||||
|
||||
// Escape required characters for path components.
|
||||
// See: https://tools.ietf.org/html/rfc3986#section-3.3
|
||||
return encodeURI(`file://${pathName}`).replaceAll(/[#?]/g, encodeURIComponent);
|
||||
}
|
||||
Loading…
Reference in New Issue