dialogs - validate and normalize default path (#186467)

* dialogs - validate and normalize default path

* address feedback
This commit is contained in:
Benjamin Pasero 2023-06-28 12:31:00 +02:00 committed by GitHub
parent fb2354b794
commit fb1d79215e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View file

@ -325,6 +325,8 @@ configurationRegistry.registerConfiguration({
},
'files.dialog.defaultPath': {
'type': 'string',
'pattern': '^((\\/|\\\\\\\\|[a-zA-Z]:\\\\).*)?$', // slash OR UNC-root OR drive-root OR undefined
'patternErrorMessage': nls.localize('defaultPathErrorMessage', "Default path for file dialogs must be an absolute path (e.g. C:\\\\myFolder or /myFolder)."),
'description': nls.localize('fileDialogDefaultPath', "Default path for file dialogs, overriding user's home path. Only used in the absence of a context-specific path, such as most recently opened file or folder."),
'scope': ConfigurationScope.MACHINE
},

View file

@ -11,6 +11,7 @@ import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { URI } from 'vs/base/common/uri';
import * as resources from 'vs/base/common/resources';
import * as path from 'vs/base/common/path';
import { IInstantiationService, } from 'vs/platform/instantiation/common/instantiation';
import { ISimpleFileDialog, SimpleFileDialog } from 'vs/workbench/services/dialogs/browser/simpleFileDialog';
import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
@ -93,17 +94,21 @@ export abstract class AbstractFileDialogService implements IFileDialogService {
}
async preferredHome(schemeFilter = this.getSchemeFilterForWindow()): Promise<URI> {
// Seek a user-local or user-remote machine-scoped override path string depending on whether caller wants a local or a remote home
const inspectedValue = this.configurationService.inspect<string>('files.dialog.defaultPath');
const dialogHomePath = schemeFilter === Schemas.file ? inspectedValue.userLocalValue : inspectedValue.userRemoteValue;
const userHomePromise = this.pathService.userHome({ preferLocal: schemeFilter === Schemas.file });
if (!dialogHomePath) {
return userHomePromise;
const preferLocal = schemeFilter === Schemas.file;
const preferredHomeConfig = this.configurationService.inspect<string>('files.dialog.defaultPath');
const preferredHomeCandidate = preferLocal ? preferredHomeConfig.userLocalValue : preferredHomeConfig.userRemoteValue;
if (preferredHomeCandidate) {
const pathLib = preferLocal ? path : await this.pathService.path;
if (pathLib.isAbsolute(preferredHomeCandidate)) {
const preferredHomeNormalized = pathLib.normalize(preferredHomeCandidate);
const preferredHome = resources.toLocalResource(await this.pathService.fileURI(preferredHomeNormalized), this.environmentService.remoteAuthority, this.pathService.defaultUriScheme);
if (await this.fileService.exists(preferredHome)) {
return preferredHome;
}
}
}
const resource = await this.pathService.fileURI(dialogHomePath);
return resources.toLocalResource(resource, this.environmentService.remoteAuthority, this.pathService.defaultUriScheme);
return this.pathService.userHome({ preferLocal });
}
async defaultWorkspacePath(schemeFilter = this.getSchemeFilterForWindow()): Promise<URI> {