Skip to content

Commit be833a2

Browse files
Scott DoverScott Dover
authored andcommitted
fix: fix file creation error/notebook naming
Signed-off-by: Scott Dover <[email protected]>
1 parent 79ea926 commit be833a2

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

client/src/components/ContentNavigator/ContentModel.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { Uri } from "vscode";
3+
import { Uri, l10n } from "vscode";
4+
5+
import { extname } from "path";
46

57
import { ALL_ROOT_FOLDERS, Messages } from "./const";
68
import { ContentAdapter, ContentItem } from "./types";
@@ -90,6 +92,43 @@ export class ContentModel {
9092
);
9193
}
9294

95+
public async createUniqueFileOfPrefix(
96+
parentItem: ContentItem,
97+
fileName: string,
98+
buffer?: ArrayBufferLike,
99+
) {
100+
const itemsInFolder = await this.getChildren(parentItem);
101+
const uniqueFileName = getUniqueFileName();
102+
103+
return await this.createFile(parentItem, uniqueFileName, buffer);
104+
105+
function getUniqueFileName(): string {
106+
const ext = extname(fileName);
107+
const basename = fileName.replace(ext, "");
108+
const usedFlowNames = itemsInFolder.reduce((carry, item) => {
109+
if (item.name.endsWith(ext)) {
110+
return { ...carry, [item.name]: true };
111+
}
112+
return carry;
113+
}, {});
114+
115+
if (!usedFlowNames[fileName]) {
116+
return fileName;
117+
}
118+
119+
let number = 1;
120+
let newFileName;
121+
do {
122+
newFileName = l10n.t("{basename}_Copy{number}.flw", {
123+
basename,
124+
number: number++,
125+
});
126+
} while (usedFlowNames[newFileName]);
127+
128+
return newFileName || fileName;
129+
}
130+
}
131+
93132
public async createFolder(
94133
item: ContentItem,
95134
name: string,

client/src/components/ContentNavigator/convert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ export class NotebookToFlowConverter {
410410
}
411411

412412
const parentItem = await this.parent();
413-
const newItem = await this.contentModel.createFile(
413+
const newItem = await this.contentModel.createUniqueFileOfPrefix(
414414
parentItem,
415415
outputName,
416416
flowDataUint8Array,

client/src/connection/rest/RestSASServerAdapter.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,26 @@ class RestSASServerAdapter implements ContentAdapter {
146146
fileName: string,
147147
buffer?: ArrayBufferLike,
148148
): Promise<ContentItem | undefined> {
149-
const response = await this.fileSystemApi.createFileOrDirectory({
150-
sessionId: this.sessionId,
151-
fileOrDirectoryPath: this.trimComputePrefix(parentItem.uri),
152-
fileProperties: { name: fileName, isDirectory: false },
153-
});
149+
try {
150+
const response = await this.fileSystemApi.createFileOrDirectory({
151+
sessionId: this.sessionId,
152+
fileOrDirectoryPath: this.trimComputePrefix(parentItem.uri),
153+
fileProperties: { name: fileName, isDirectory: false },
154+
});
154155

155-
const contentItem = this.filePropertiesToContentItem(response.data);
156+
const contentItem = this.filePropertiesToContentItem(response.data);
156157

157-
if (buffer) {
158-
await this.updateContentOfItemAtPath(
159-
this.trimComputePrefix(contentItem.uri),
160-
new TextDecoder().decode(buffer),
161-
);
162-
}
158+
if (buffer) {
159+
await this.updateContentOfItemAtPath(
160+
this.trimComputePrefix(contentItem.uri),
161+
new TextDecoder().decode(buffer),
162+
);
163+
}
163164

164-
return contentItem;
165+
return contentItem;
166+
} catch (error) {
167+
return;
168+
}
165169
}
166170

167171
public async deleteItem(item: ContentItem): Promise<boolean> {

0 commit comments

Comments
 (0)