Skip to content

Commit

Permalink
Enable opening read-only files in electron. (#9950)
Browse files Browse the repository at this point in the history
Signed-off-by: Kejsi Take <[email protected]>
  • Loading branch information
kejsitake authored and RomanNikitenko committed Sep 16, 2021
1 parent 2a7ff2e commit ed299e9
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ElectronFileDialogService extends DefaultFileDialogService {
}

const uris = filePaths.map(path => FileUri.create(path));
const canAccess = await this.canReadWrite(uris);
const canAccess = await this.canRead(uris);
const result = canAccess ? uris.length === 1 ? uris[0] : uris : undefined;
return result;
}
Expand All @@ -74,8 +74,8 @@ export class ElectronFileDialogService extends DefaultFileDialogService {
return uri;
}

const canAccess = await this.canReadWrite(uri);
return canAccess ? uri : undefined;
const canWrite = await this.canReadWrite(uri);
return canWrite ? uri : undefined;
}
return undefined;
}
Expand All @@ -90,6 +90,16 @@ export class ElectronFileDialogService extends DefaultFileDialogService {
return true;
}

protected async canRead(uris: MaybeArray<URI>): Promise<boolean> {
const inaccessibleFilePaths = await Promise.all((Array.isArray(uris) ? uris : [uris]).map(
async uri => (!await this.fileService.access(uri, FileAccess.Constants.R_OK) && uri.path || '')
).filter(e => e));
if (inaccessibleFilePaths.length) {
this.messageService.error(`Cannot read ${inaccessibleFilePaths.length} resources: ${inaccessibleFilePaths.join(', ')}`);
}
return !!inaccessibleFilePaths.length;
}

protected toDialogOptions(uri: URI, props: SaveFileDialogProps | OpenFileDialogProps, dialogTitle: string): electron.FileDialogProps {
const title = props.title || dialogTitle;
const defaultPath = FileUri.fsPath(uri);
Expand Down

0 comments on commit ed299e9

Please sign in to comment.