Skip to content

Commit 60454e5

Browse files
Scott DoverScott Dover
authored andcommitted
chore: implement upload/download/drag drop
1 parent 7d3a214 commit 60454e5

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

client/src/connection/rest/RestSASServerAdapter.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class RestSASServerAdapter implements ContentAdapter {
5656
this.sessionId = session?.sessionId();
5757
// This proxies all calls to the fileSystem api to reconnect
5858
// if we ever get a 401 (unauthorized)
59+
const reconnect = async () => await this.connect();
5960
this.fileSystemApi = new Proxy(FileSystemApi(getApiConfig()), {
6061
get: function (target, property) {
6162
if (typeof target[property] === "function") {
@@ -68,7 +69,7 @@ class RestSASServerAdapter implements ContentAdapter {
6869
throw error;
6970
}
7071

71-
await this.connect();
72+
await reconnect();
7273

7374
return await target(...argList);
7475
}
@@ -130,21 +131,16 @@ class RestSASServerAdapter implements ContentAdapter {
130131
fileProperties: { name: fileName, isDirectory: false },
131132
});
132133

134+
const contentItem = this.filePropertiesToContentItem(response.data);
135+
133136
if (buffer) {
134-
const etag = response.headers.etag;
135-
// TODO (sas-server) This could be combined with update content most likely.
136-
const filePath = this.trimComputePrefix(
137-
getLink(response.data.links, "GET", "self").uri,
137+
await this.updateContentOfItemAtPath(
138+
this.trimComputePrefix(contentItem.uri),
139+
new TextDecoder().decode(buffer),
138140
);
139-
await this.fileSystemApi.updateFileContentOnSystem({
140-
sessionId: this.sessionId,
141-
filePath,
142-
body: new File([buffer], response.data.name),
143-
ifMatch: etag,
144-
});
145141
}
146142

147-
return this.filePropertiesToContentItem(response.data);
143+
return contentItem;
148144
}
149145

150146
public async deleteItem(item: ContentItem): Promise<boolean> {
@@ -218,12 +214,16 @@ class RestSASServerAdapter implements ContentAdapter {
218214
}
219215

220216
public async getContentOfItem(item: ContentItem): Promise<string> {
221-
throw new Error("getContentOfItem");
217+
const path = this.trimComputePrefix(item.uri);
218+
return await this.getContentOfItemAtPath(path);
222219
}
223220

224221
public async getContentOfUri(uri: Uri): Promise<string> {
225222
const path = this.trimComputePrefix(getResourceId(uri));
223+
return await this.getContentOfItemAtPath(path);
224+
}
226225

226+
private async getContentOfItemAtPath(path: string) {
227227
const response = await this.fileSystemApi.getFileContentFromSystem(
228228
{
229229
sessionId: this.sessionId,
@@ -313,7 +313,7 @@ class RestSASServerAdapter implements ContentAdapter {
313313
): Promise<boolean> {
314314
const currentFilePath = this.trimComputePrefix(item.uri);
315315
const newFilePath = this.trimComputePrefix(targetParentFolderUri);
316-
const { etag } = await this.getFileInfo(currentFilePath);
316+
const { etag } = await this.getFileInfo(currentFilePath, true);
317317
const params = {
318318
sessionId: this.sessionId,
319319
fileOrDirectoryPath: currentFilePath,
@@ -374,9 +374,15 @@ class RestSASServerAdapter implements ContentAdapter {
374374

375375
public async updateContentOfItem(uri: Uri, content: string): Promise<void> {
376376
const filePath = this.trimComputePrefix(getResourceId(uri));
377-
const { etag } = await this.getFileInfo(filePath);
377+
return await this.updateContentOfItemAtPath(filePath, content);
378+
}
378379

379-
const response = await this.fileSystemApi.updateFileContentOnSystem({
380+
private async updateContentOfItemAtPath(
381+
filePath: string,
382+
content: string,
383+
): Promise<void> {
384+
const { etag } = await this.getFileInfo(filePath);
385+
const data = {
380386
sessionId: this.sessionId,
381387
filePath,
382388
// updateFileContentOnSystem requires body to be a File type. However, the
@@ -385,7 +391,8 @@ class RestSASServerAdapter implements ContentAdapter {
385391
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
386392
body: content as unknown as File,
387393
ifMatch: etag,
388-
});
394+
};
395+
const response = await this.fileSystemApi.updateFileContentOnSystem(data);
389396

390397
this.updateFileMetadata(filePath, response);
391398
}
@@ -402,6 +409,10 @@ class RestSASServerAdapter implements ContentAdapter {
402409
uri: link.uri,
403410
}));
404411

412+
if (!getLink(links, "GET", "self")) {
413+
console.log("OH NBO");
414+
}
415+
405416
const id = getLink(links, "GET", "self").uri;
406417
const isRootFolder = [SERVER_FOLDER_ID, SAS_SERVER_HOME_DIRECTORY].includes(
407418
id,
@@ -447,7 +458,9 @@ class RestSASServerAdapter implements ContentAdapter {
447458
}
448459

449460
private trimComputePrefix(uri: string): string {
450-
return uri.replace(`/compute/sessions/${this.sessionId}/files/`, "");
461+
return decodeURI(
462+
uri.replace(`/compute/sessions/${this.sessionId}/files/`, ""),
463+
);
451464
}
452465

453466
private updateFileMetadata(id: string, { headers }: AxiosResponse) {
@@ -458,8 +471,8 @@ class RestSASServerAdapter implements ContentAdapter {
458471
return this.fileMetadataMap[id];
459472
}
460473

461-
private async getFileInfo(path: string) {
462-
if (path in this.fileMetadataMap) {
474+
private async getFileInfo(path: string, forceRefresh?: boolean) {
475+
if (!forceRefresh && path in this.fileMetadataMap) {
463476
return this.fileMetadataMap[path];
464477
}
465478

0 commit comments

Comments
 (0)