Skip to content

Commit 8b3e24c

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

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";
@@ -92,6 +94,43 @@ export class ContentModel {
9294
);
9395
}
9496

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

client/src/components/ContentNavigator/convert.ts

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

413413
const parentItem = await this.parent();
414-
const newItem = await this.contentModel.createFile(
414+
const newItem = await this.contentModel.createUniqueFileOfPrefix(
415415
parentItem,
416416
outputName,
417417
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)