Skip to content

Commit

Permalink
Handles unexisting files in user storage
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick-Jeffrey Pollo Guilbert <[email protected]>
  • Loading branch information
epatpol committed Nov 28, 2017
1 parent 6defb21 commit 595e943
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/userstorage/src/browser/user-storage-service-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,30 @@ export class UserStorageServiceFilesystemImpl implements UserStorageService {
});
}

readContents(uri: URI) {
return this.userStorageFolder
.then(folderUri => {
const filesystemUri = UserStorageServiceFilesystemImpl.toFilesystemURI(folderUri, uri);
return this.fileSystem.resolveContent(filesystemUri.toString());
})
.then(({ stat, content }) => content);
}
async readContents(uri: URI) {
const folderUri = await this.userStorageFolder;
const filesystemUri = UserStorageServiceFilesystemImpl.toFilesystemURI(folderUri, uri);
const exists = this.fileSystem.exists(filesystemUri.toString());

if (exists) {
return this.fileSystem.resolveContent(filesystemUri.toString()).then(({ stat, content }) => content);
}

saveContents(uri: URI, content: string) {
return "";
}

return this.userStorageFolder.then(folderUri => {
const filesystemUri = UserStorageServiceFilesystemImpl.toFilesystemURI(folderUri, uri);
async saveContents(uri: URI, content: string) {
const folderUri = await this.userStorageFolder;
const filesystemUri = UserStorageServiceFilesystemImpl.toFilesystemURI(folderUri, uri);
const exists = this.fileSystem.exists(filesystemUri.toString());

if (exists) {
this.fileSystem.getFileStat(filesystemUri.toString()).then(fileStat => {
this.fileSystem.setContent(fileStat, content).then(() => Promise.resolve());
});
});
} else {
this.fileSystem.createFile(filesystemUri.toString(), { content });
}
}

get onUserStorageChanged(): Event<UserStorageChangeEvent> {
Expand Down

0 comments on commit 595e943

Please sign in to comment.