diff --git a/src/gui/src/initgui.js b/src/gui/src/initgui.js index 94eb5acfe..cbcf765f7 100644 --- a/src/gui/src/initgui.js +++ b/src/gui/src/initgui.js @@ -49,6 +49,7 @@ import { AntiCSRFService } from './services/AntiCSRFService.js'; import { IPCService } from './services/IPCService.js'; import { ExecService } from './services/ExecService.js'; import { DebugService } from './services/DebugService.js'; +import { privacy_aware_path } from './util/desktop.js'; const launch_services = async function (options) { // === Services Data Structures === @@ -1461,35 +1462,5 @@ $(document).on('contextmenu', '.disable-context-menu', function(e){ } }) -/** - * Converts a file system path to a privacy-aware path. - * - Paths starting with `~/` are returned unchanged. - * - Paths starting with the user's home path are replaced with `~`. - * - Absolute paths not starting with the user's home path are returned unchanged. - * - Relative paths are prefixed with `~/`. - * - Other paths are returned unchanged. - * - * @param {string} fspath - The file system path to be converted. - * @returns {string} The privacy-aware path. - */ -window.privacy_aware_path = function(fspath){ - // e.g. /my_username/test.txt -> ~/test.txt - if(fspath.startsWith('~/')) - return fspath; - // e.g. /my_username/test.txt -> ~/test.txt - else if(fspath.startsWith( - window.home_path.endsWith('/') - ? window.home_path - : window.home_path + '/' - )) - return fspath.replace(window.home_path, '~'); - // e.g. /other_username/test.txt -> /other_username/test.txt - else if(fspath.startsWith('/') && !fspath.startsWith(window.home_path)) - return fspath; - // e.g. test.txt -> ~/test.txt - else if(!fspath.startsWith('/')) - return '~/' + fspath; - // e.g. /username/path/to/item -> /username/path/to/item - else - return fspath; -}; \ No newline at end of file +// util/desktop.js +window.privacy_aware_path = privacy_aware_path({ window }); diff --git a/src/gui/src/util/desktop.js b/src/gui/src/util/desktop.js new file mode 100644 index 000000000..3f3bf1ee1 --- /dev/null +++ b/src/gui/src/util/desktop.js @@ -0,0 +1,43 @@ +/** + * This file contains functions that are used by Puter's desktop GUI. + * Functions moved here are not bound to the `window` object, making it + * easier to write unit tests for them. + * + * Functions here may be bound to `window` at any of the following locations: + * - src/gui/src/initgui.js + * + * ^ Please add to the above list as necessary when moving functions here. + */ + +/** + * Converts a file system path to a privacy-aware path. + * - Paths starting with `~/` are returned unchanged. + * - Paths starting with the user's home path are replaced with `~`. + * - Absolute paths not starting with the user's home path are returned unchanged. + * - Relative paths are prefixed with `~/`. + * - Other paths are returned unchanged. + * + * @param {string} fspath - The file system path to be converted. + * @returns {string} The privacy-aware path. + */ +export const privacy_aware_path = world => function privacy_aware_path (fspath) { + // e.g. /my_username/test.txt -> ~/test.txt + if(fspath.startsWith('~/')) + return fspath; + // e.g. /my_username/test.txt -> ~/test.txt + else if(fspath.startsWith( + world.window.home_path.endsWith('/') + ? world.window.home_path + : world.window.home_path + '/' + )) + return fspath.replace(world.window.home_path, '~'); + // e.g. /other_username/test.txt -> /other_username/test.txt + else if(fspath.startsWith('/') && !fspath.startsWith(world.window.home_path)) + return fspath; + // e.g. test.txt -> ~/test.txt + else if(!fspath.startsWith('/')) + return '~/' + fspath; + // e.g. /username/path/to/item -> /username/path/to/item + else + return fspath; +}; diff --git a/src/gui/test/privacy_aware_path.test.js b/src/gui/test/privacy_aware_path.test.js new file mode 100644 index 000000000..30b628a40 --- /dev/null +++ b/src/gui/test/privacy_aware_path.test.js @@ -0,0 +1,33 @@ +import assert from 'assert'; +import { privacy_aware_path } from '../src/util/desktop.js'; + +const cases = [ + { + title: 'path in user home', + username: 'user', + input: '/home/user/test.txt', + expected: '~/test.txt', + }, + { + title: 'path on user desktop', + username: 'user', + input: '/home/user/Desktop/test.txt', + expected: '~/Desktop/test.txt', + }, + { + title: 'prefix (ed3/ed) bug', + username: 'ed', + input: '/home/ed3/Desktop/test.txt', + expected: '/home/ed3/Desktop/test.txt', + }, +]; + +describe('window.privacy_aware_path', () => { + for (const { title, username, input, expected } of cases) { + it(title, () => { + assert.equal(privacy_aware_path({ + window: { home_path: `/home/${username}` }, + })(input), expected); + }); + } +}); \ No newline at end of file