Skip to content

Commit

Permalink
fix: fix file creation error/notebook naming
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Dover <[email protected]>
  • Loading branch information
Scott Dover authored and Scott Dover committed Nov 8, 2024
1 parent 607878b commit 285fcb7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
41 changes: 40 additions & 1 deletion client/src/components/ContentNavigator/ContentModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright © 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { Uri } from "vscode";
import { Uri, l10n } from "vscode";

import { extname } from "path";

import { ALL_ROOT_FOLDERS, Messages } from "./const";
import { ContentAdapter, ContentItem } from "./types";
Expand Down Expand Up @@ -92,6 +94,43 @@ export class ContentModel {
);
}

public async createUniqueFileOfPrefix(
parentItem: ContentItem,
fileName: string,
buffer?: ArrayBufferLike,
) {
const itemsInFolder = await this.getChildren(parentItem);
const uniqueFileName = getUniqueFileName();

return await this.createFile(parentItem, uniqueFileName, buffer);

function getUniqueFileName(): string {
const ext = extname(fileName);
const basename = fileName.replace(ext, "");
const usedFlowNames = itemsInFolder.reduce((carry, item) => {
if (item.name.endsWith(ext)) {
return { ...carry, [item.name]: true };
}
return carry;
}, {});

if (!usedFlowNames[fileName]) {
return fileName;
}

let number = 1;
let newFileName;
do {
newFileName = l10n.t("{basename}_Copy{number}.flw", {
basename,
number: number++,
});
} while (usedFlowNames[newFileName]);

return newFileName || fileName;
}
}

public async createFolder(
item: ContentItem,
name: string,
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/ContentNavigator/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export class NotebookToFlowConverter {
}

const parentItem = await this.parent();
const newItem = await this.contentModel.createFile(
const newItem = await this.contentModel.createUniqueFileOfPrefix(
parentItem,
outputName,
flowDataUint8Array,
Expand Down
30 changes: 17 additions & 13 deletions client/src/connection/rest/RestSASServerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,26 @@ class RestSASServerAdapter implements ContentAdapter {
fileName: string,
buffer?: ArrayBufferLike,
): Promise<ContentItem | undefined> {
const response = await this.fileSystemApi.createFileOrDirectory({
sessionId: this.sessionId,
fileOrDirectoryPath: this.trimComputePrefix(parentItem.uri),
fileProperties: { name: fileName, isDirectory: false },
});
try {
const response = await this.fileSystemApi.createFileOrDirectory({
sessionId: this.sessionId,
fileOrDirectoryPath: this.trimComputePrefix(parentItem.uri),
fileProperties: { name: fileName, isDirectory: false },
});

const contentItem = this.filePropertiesToContentItem(response.data);
const contentItem = this.filePropertiesToContentItem(response.data);

if (buffer) {
await this.updateContentOfItemAtPath(
this.trimComputePrefix(contentItem.uri),
new TextDecoder().decode(buffer),
);
}
if (buffer) {
await this.updateContentOfItemAtPath(
this.trimComputePrefix(contentItem.uri),
new TextDecoder().decode(buffer),
);
}

return contentItem;
return contentItem;
} catch (error) {
return;
}
}

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

0 comments on commit 285fcb7

Please sign in to comment.