Skip to content

Commit

Permalink
move to pathToFileURL
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi committed Apr 1, 2024
1 parent 1206ecb commit e9bf49c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 69 deletions.
2 changes: 2 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export * as configStore from './configStore.js';

export { isLinux, isWindows, isMac, platform } from './util.js';

export { pathToFileURL } from 'node:url';


// https://www.i18next.com/overview/typescript#argument-of-type-defaulttfuncreturn-is-not-assignable-to-parameter-of-type-xyz
// todo This should not be necessary anymore since v23.0.0
Expand Down
49 changes: 49 additions & 0 deletions src/main/pathToFileURL.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// eslint-disable-next-line import/no-extraneous-dependencies

Check failure on line 1 in src/main/pathToFileURL.test.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Filename is not in camel case or pascal case. Rename it to `pathToFileUrl.test.ts`, `pathToFileUrl.Test.ts`, `PathToFileUrl.test.ts`, or `PathToFileUrl.Test.ts`
import { test, expect, describe } from 'vitest';

import { pathToFileURL } from 'node:url';


if (process.platform === 'win32') {
describe('file uri windows only', () => {
test('converts path to file url', () => {
expect(pathToFileURL('C:\\Users\\sindresorhus\\dev\\te^st.jpg').href).toEqual('file:///C:/Users/sindresorhus/dev/te^st.jpg');
});
});
} else {
describe('file uri non-windows', () => {
// https://github.com/mifi/lossless-cut/issues/1941
test('file with backslash', () => {
expect(pathToFileURL('/has/back\\slash').href).toEqual('file:///has/back%5Cslash');
});
});
}

// taken from https://github.com/sindresorhus/file-url
describe('file uri both platforms', () => {
test('converts path to file url', () => {
expect(pathToFileURL('/test.jpg').href).toMatch(/file:\/{3}test\.jpg/);

expect(pathToFileURL('/Users/sindresorhus/dev/te^st.jpg').href).toEqual('file:///Users/sindresorhus/dev/te^st.jpg');
});

test('escapes more special characters in path', () => {
expect(pathToFileURL('/a^?!@#$%&\'";<>').href).toEqual('file:///a^%3F!@%23$%25&\'%22;%3C%3E');
});

test('escapes whitespace characters in path', () => {
expect(pathToFileURL('/file with\r\nnewline').href).toEqual('file:///file%20with%0D%0Anewline');
});

test('relative path', () => {
expect(pathToFileURL('relative/test.jpg').href).toMatch(/^file:\/{3}.*\/relative\/test\.jpg$/);
});

test('slash', () => {
expect(pathToFileURL('/').href).toEqual('file:///');
});

test('empty', () => {
expect(pathToFileURL('').href).toMatch(/^file:\/{3}.*$/);
});
});
7 changes: 3 additions & 4 deletions src/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import {
isStoreBuild, dragPreventer,
havePermissionToReadFile, resolvePathIfNeeded, getPathReadAccessError, html5ifiedPrefix, html5dummySuffix, findExistingHtml5FriendlyFile,
deleteFiles, isOutOfSpaceError, isExecaFailure, readFileSize, readFileSizes, checkFileSizes, setDocumentTitle, getOutFileExtension, getSuffixedFileName, mustDisallowVob, readVideoTs, readDirRecursively, getImportProjectType,
calcShouldShowWaveform, calcShouldShowKeyframes, mediaSourceQualities, isWindows,
calcShouldShowWaveform, calcShouldShowKeyframes, mediaSourceQualities,
} from './util';
import { toast, errorToast } from './swal';
import { formatDuration } from './util/duration';
Expand All @@ -89,14 +89,13 @@ import isDev from './isDev';
import { ChromiumHTMLVideoElement, EdlFileType, FfmpegCommandLog, FormatTimecode, PlaybackMode, SegmentColorIndex, SegmentTags, SegmentToExport, StateSegment, Thumbnail, TunerType } from './types';
import { CaptureFormat, KeyboardAction, Html5ifyMode } from '../../../types';
import { FFprobeChapter, FFprobeFormat, FFprobeStream } from '../../../ffprobe';
import filePathToUrl from './util/fileUri';

const electron = window.require('electron');
const { exists } = window.require('fs-extra');
const { lstat } = window.require('fs/promises');
const { parse: parsePath, join: pathJoin, basename, dirname } = window.require('path');

const { focusWindow, hasDisabledNetworking, quitApp } = window.require('@electron/remote').require('./index.js');
const { focusWindow, hasDisabledNetworking, quitApp, pathToFileURL } = window.require('@electron/remote').require('./index.js');


const videoStyle: CSSProperties = { width: '100%', height: '100%', objectFit: 'contain' };
Expand Down Expand Up @@ -449,7 +448,7 @@ function App() {
const effectiveFilePath = previewFilePath || filePath;
const fileUri = useMemo(() => {
if (!effectiveFilePath) return ''; // Setting video src="" prevents memory leak in chromium
const uri = filePathToUrl(effectiveFilePath, isWindows);
const uri = pathToFileURL(effectiveFilePath).href;
// https://github.com/mifi/lossless-cut/issues/1674
if (cacheBuster !== 0) {
const qs = new URLSearchParams();
Expand Down
48 changes: 0 additions & 48 deletions src/renderer/src/util/fileUri.test.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/renderer/src/util/fileUri.ts

This file was deleted.

0 comments on commit e9bf49c

Please sign in to comment.