Skip to content

Commit abaafc1

Browse files
authored
Merge pull request #29 from DenisaCG/multiDrives
Support for inter-drives operations
2 parents ce55cd3 + b764fe6 commit abaafc1

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

jupyter_drives/manager.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,21 +426,39 @@ async def check_file(self, drive_name, path):
426426

427427
return
428428

429-
async def copy_file(self, drive_name, path, to_path):
429+
async def copy_file(self, drive_name, path, to_path, to_drive):
430430
"""Save file with new content.
431431
432432
Args:
433433
drive_name: name of drive where file exists
434434
path: path where original content exists
435435
to_path: path where object should be copied
436+
to_drive: name of drive where to copy object
436437
"""
437438
data = {}
438439
try:
439440
# eliminate leading and trailing backslashes
440441
path = path.strip('/')
441442

442-
await obs.copy_async(self._content_managers[drive_name]["store"], path, to_path)
443-
metadata = await obs.head_async(self._content_managers[drive_name]["store"], to_path)
443+
# copy object within same drive
444+
if to_drive == drive_name:
445+
await obs.copy_async(self._content_managers[drive_name]["store"], path, to_path)
446+
metadata = await obs.head_async(self._content_managers[drive_name]["store"], to_path)
447+
# copy object to another drive
448+
else:
449+
content = b''
450+
try:
451+
# retrieving contents of file
452+
file = await obs.get_async(self._content_managers[drive_name]["store"], path)
453+
stream = file.stream(min_chunk_size=5 * 1024 * 1024) # 5MB sized chunks
454+
async for buf in stream:
455+
content += buf
456+
except:
457+
# dealing with a directory, no contents to retrieve
458+
pass
459+
460+
await obs.put_async(self._content_managers[to_drive]["store"], to_path, content)
461+
metadata = await obs.head_async(self._content_managers[to_drive]["store"], to_path)
444462

445463
data = {
446464
"path": to_path,

src/contents.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ export class Drive implements Contents.IDrive {
597597
): Promise<Contents.IModel> {
598598
if (path !== '') {
599599
const currentDrive = extractCurrentDrive(path, this._drivesList);
600+
const toDrive = extractCurrentDrive(toDir, this._drivesList);
600601

601602
// eliminate drive name from path
602603
const relativePath = formatPath(path);
@@ -606,13 +607,14 @@ export class Drive implements Contents.IDrive {
606607
const newFileName = await this.incrementCopyName(
607608
relativePath,
608609
toRelativePath,
609-
currentDrive.name
610+
toDrive.name
610611
);
611612

612613
data = await copyObjects(currentDrive.name, {
613614
path: relativePath,
614615
toPath: toRelativePath,
615616
newFileName: newFileName,
617+
toDrive: toDrive.name,
616618
registeredFileTypes: this._registeredFileTypes
617619
});
618620
} else {

src/requests.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ export async function copyObjects(
362362
path: string;
363363
toPath: string;
364364
newFileName: string;
365+
toDrive: string;
365366
registeredFileTypes: IRegisteredFileTypes;
366367
}
367368
) {
@@ -385,6 +386,7 @@ export async function copyObjects(
385386
const remainingFilePath = c.path.substring(options.path.length);
386387
Private.copySingleObject(
387388
driveName,
389+
options.toDrive,
388390
PathExt.join(options.path, remainingFilePath),
389391
PathExt.join(formattedNewPath, remainingFilePath)
390392
);
@@ -395,12 +397,13 @@ export async function copyObjects(
395397
try {
396398
const copiedObject = await Private.copySingleObject(
397399
driveName,
400+
options.toDrive,
398401
options.path,
399402
formattedNewPath
400403
);
401404
data = {
402405
name: options.newFileName,
403-
path: PathExt.join(driveName, formattedNewPath),
406+
path: PathExt.join(options.toDrive, formattedNewPath),
404407
last_modified: copiedObject.data.last_modified,
405408
created: '',
406409
content: PathExt.extname(options.newFileName) !== '' ? null : [], // TODO: add dir check
@@ -540,14 +543,16 @@ namespace Private {
540543
*/
541544
export async function copySingleObject(
542545
driveName: string,
546+
toDrive: string,
543547
objectPath: string,
544548
newObjectPath: string
545549
) {
546550
return await requestAPI<any>(
547551
'drives/' + driveName + '/' + objectPath,
548552
'PUT',
549553
{
550-
to_path: newObjectPath
554+
to_path: newObjectPath,
555+
to_drive: toDrive
551556
}
552557
);
553558
}

0 commit comments

Comments
 (0)