Skip to content

Commit

Permalink
fix(basic.gblib): COPY is now target recursive.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jan 15, 2021
1 parent ebbda4e commit 4048e72
Showing 1 changed file with 54 additions and 40 deletions.
94 changes: 54 additions & 40 deletions packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,37 @@ export class SystemKeywords {
const parts = name.split('/');
let lastFolder = null;

// Creates each subfolder.

await CollectionUtil.asyncForEach(parts, async item => {

path = urlJoin(path, item);
// Calls drive API.

const body = {
"name": name,
"name": item,
"folder": {},
"@microsoft.graph.conflictBehavior": "rename"
"@microsoft.graph.conflictBehavior": "fail"
};
lastFolder = await client
.api(`${baseUrl}/drive/root:/${path}:/children`)
.post(body);

try {
lastFolder = await client
.api(`${baseUrl}/drive/root:/${path}:/children`)
.post(body);

} catch (error) {
if (error.code !== "nameAlreadyExists") {
throw error;
}
else {
lastFolder = await client
.api(`${baseUrl}/drive/root:/${urlJoin(path, item)}`)
.get();
}
}

// Increments path to the next child be created.

path = urlJoin(path, item);
});
return lastFolder;
}
Expand All @@ -397,32 +416,17 @@ export class SystemKeywords {
let [, client] = await this.internalGetDriveClient();
const driveId = folderReference.parentReference.driveId;
const itemId = folderReference.id;
const body = {
"recipients": [{ "email": email }],
"message": message,
"requireSignIn": true,
"sendInvitation": true,
"roles": ["write"]
};

return new Promise<string>((resolve, reject) => {

const body = {
"recipients": [
{
"email": email
}
],
"message": message,
"requireSignIn": true,
"sendInvitation": true,
"roles": ["write"]
};

client
.api(`https://graph.microsoft.com/v1.0/drives/${driveId}/items/${itemId}/invite`)
.post(body, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res);
}
});
});
await client
.api(`https://graph.microsoft.com/v1.0/drives/${driveId}/items/${itemId}/invite`)
.post(body);
}

/**
Expand All @@ -437,32 +441,42 @@ export class SystemKeywords {

let [baseUrl, client] = await this.internalGetDriveClient();
const botId = this.min.instance.botId;

// Normalizes all slashes.

src = src.replace(/\\/gi, '/');
dest = dest.replace(/\\/gi, '/');

// Determines full path at source and destination.

const root = urlJoin(`/${botId}.gbai/${botId}.gbdata`);
const srcPath = urlJoin(root, src);
const dstPath = urlJoin(`/${botId}.gbai/${botId}.gbdata`, dest);


// Checks if the destination contains subfolders that
// need to be created.

let folder;
if (dest.indexOf('/') !== -1)
{
if (dest.indexOf('/') !== -1) {
const pathOnly = path.dirname(dest);
folder = await this.createFolder(pathOnly);
}
else
{
else {
folder = await client.api(
`${baseUrl}/drive/root:/${root}`)
.get();
}

// Performs the copy operation getting a reference
// to the source and calling /copy on drive API.

try {
const srcFile = await client.api(
`${baseUrl}/drive/root:/${srcPath}`)
.get();

const destFile =
{
const destFile = {
"parentReference": { driveId: folder.parentReference.driveId, id: folder.id },
"name": `${dest}`
"name": `${path.basename(dest)}`
}

return await client.api(
Expand Down

0 comments on commit 4048e72

Please sign in to comment.