Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/phoenix/fslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ const fileSystemLib = {
}
return filerShell.rm(path, { recursive: true }, cb);
},
copyFile: function (src, dst, callback) {
if(Mounts.isMountSubPath(src) && Mounts.isMountSubPath(dst)) {
return NativeFS.copyFile(src, dst, callback);
}
throw new Errors.ENOSYS('Phoenix fs copy on filer or across filer and native not yet supported');
},
showSaveDialog: function () {
throw new Errors.ENOSYS('Phoenix fs showSaveDialog function not yet supported.');
},
Expand Down
15 changes: 14 additions & 1 deletion src/phoenix/fslib_mounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ function getHandleFromPath(normalisedPath, callback) {
});
}

async function getHandleFromPathIfPresent(normalisedPath) {
return new Promise(resolve => {
getHandleFromPath(normalisedPath, (err, handle) =>{
if(err) {
resolve(null);
} else {
resolve(handle);
}
});
});
}

function getMountPoints() {
return MountPointsStore.getMountPoints();
}
Expand All @@ -287,7 +299,8 @@ const Mounts = {
isMountSubPath,
getHandleFromPath,
getMountPoints,
refreshMountPoints
refreshMountPoints,
getHandleFromPathIfPresent
};

export default Mounts;
51 changes: 50 additions & 1 deletion src/phoenix/fslib_native.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,54 @@ async function unlink(path, callback) {
});
}

async function _getDestinationFileHandle(dst, srcFileName) {
return new Promise(async (resolve, reject) => {
dst = window.path.normalize(dst);
let dirPath= window.path.dirname(dst);
let dstFileName= window.path.basename(dst);
let dstHandle = await Mounts.getHandleFromPathIfPresent(dst);
let dstParentHandle = await Mounts.getHandleFromPathIfPresent(dirPath);
if (dstHandle && dstHandle.kind === Constants.KIND_FILE) {
reject(new Errors.EEXIST(`Copy file destination already exists: ${dst}`));
} else if (dstHandle && dstHandle.kind === Constants.KIND_DIRECTORY) {
const fileHandle = await dstHandle.getFileHandle(srcFileName, {create: true});
resolve(fileHandle);
} else if (!dstHandle && dstParentHandle && dstParentHandle.kind === Constants.KIND_DIRECTORY) {
const fileHandle = await dstParentHandle.getFileHandle(dstFileName, {create: true});
resolve(fileHandle);
} else {
reject(new Errors.ENOENT(`Copy destination doesnt exist: ${dst}`));
}
});
}

async function _copyFile(srcFileHandle, dst, srcFileName, callback) {
try {
let dstHandle = await _getDestinationFileHandle(dst, srcFileName);
const srcFile = await srcFileHandle.getFile();
const srcStream = await srcFile.stream();
const writable = await dstHandle.createWritable();
await srcStream.pipeTo(writable);
callback();
} catch (e) {
callback(e);
}
}

async function copyFile(src, dst, callback) {
let srcFile = window.path.normalize(src);
let srcFileName= window.path.basename(srcFile);
Mounts.getHandleFromPath(srcFile, async (err, srcHandle) => {
if(err){
callback(err);
} else if (srcHandle.kind === Constants.KIND_DIRECTORY) {
callback(new Errors.EISDIR(`Copy file cannot copy directory: ${srcFile}`));
} else {
_copyFile(srcHandle, dst, srcFileName, callback);
}
});
}

function mountNativeFolder(...args) {
Mounts.mountNativeFolder(...args);
}
Expand All @@ -232,7 +280,8 @@ const NativeFS = {
stat,
readFile,
writeFile,
unlink
unlink,
copyFile
};

export default NativeFS;
3 changes: 3 additions & 0 deletions test/SpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@
</script>

<!-- Import the phoenix browser virtual file system -->
<script src="../src/thirdparty/idb/idb-min.js"></script>
<script src="../src/thirdparty/filer/filer.min.js"></script>
<script src="../src/thirdparty/Buffer/buffer-min.js"></script>
<script src="../src/phoenix/shell.js" type="module"></script>
<script src="../src/phoenix/nohost_server.js" type="module"></script>

<script src="thirdparty/jasmine-core/jasmine.js"></script>
<!-- Pre-load third party scripts that cannot be async loaded. -->
Expand Down