From c63a50a19627159ca13c513fb8026f42f7c5918e Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Fri, 21 Apr 2023 23:36:01 -0400 Subject: [PATCH] feat(file): copy sync and async support (#10273) --- apps/toolbox/src/pages/fs-helper.ts | 38 +++++++- apps/toolbox/src/pages/fs-helper.xml | 1 + .../file-system/file-system-access.android.ts | 36 +++++++ .../core/file-system/file-system-access.d.ts | 32 ++++++ .../file-system/file-system-access.ios.ts | 59 +++++++++++ packages/core/file-system/index.ts | 47 +++++++++ .../platforms/android/widgets-release.aar | Bin 267580 -> 266072 bytes .../lib/android/org.nativescript.widgets.d.ts | 4 + .../java/org/nativescript/widgets/Async.java | 92 ++++++++++++++++++ 9 files changed, 308 insertions(+), 1 deletion(-) diff --git a/apps/toolbox/src/pages/fs-helper.ts b/apps/toolbox/src/pages/fs-helper.ts index 8924d5852..a4ca20d51 100644 --- a/apps/toolbox/src/pages/fs-helper.ts +++ b/apps/toolbox/src/pages/fs-helper.ts @@ -109,7 +109,8 @@ export function pickFile() { const file = args.intent.getData().toString(); //const file = File.fromPath(args.intent.getData().toString()); //console.log(file); - readFile(file); + //readFile(file); + copyFile(file); } }); const Intent = android.content.Intent; @@ -172,6 +173,41 @@ function saveFile(selected, readSync) { console.log('==== SAVE END ========='); } +function copyFile(file) { + const picked = File.fromPath(file); + const ext = picked.extension; + const name = picked.name.replace(`.${ext}`, ''); + const tempCopy = File.fromPath(path.join(knownFolders.temp().path, `${name}-copy.${ext}`)); + + // const done = picked + // .copySync(tempCopy.path); + // console.log('done: ' + done + '\n' + 'Original path: ' + picked.path + '\n' + 'Copied to: ' + tempCopy.path + '\n' + 'Original size: ' + picked.size + '\n' + 'Copy size: ' + tempCopy.size + '\n'); + + picked + .copy(tempCopy.path) + .then((done) => { + console.log('done: ' + done + '\n' + 'Original path: ' + picked.path + '\n' + 'Copied to: ' + tempCopy.path + '\n' + 'Original size: ' + picked.size + '\n' + 'Copy size: ' + tempCopy.size + '\n'); + }) + .catch((error) => { + console.log(error); + }); +} + +export function copyTest() { + const now = Date.now(); + const tempFile = File.fromPath(path.join(knownFolders.temp().path, `${now}.txt`)); + tempFile.writeTextSync('Hello World: ' + now); + const tempCopy = File.fromPath(path.join(knownFolders.temp().path, `${now}-copy.txt`)); + tempFile + .copy(tempCopy.path) + .then((done) => { + console.log('done: ' + done + '\n' + 'Original path: ' + tempFile.path + '\n' + 'Copied to: ' + tempCopy.path + '\n' + 'Original size: ' + tempFile.size + '\n' + 'Copy size: ' + tempCopy.size + '\n'); + }) + .catch((error) => { + console.log(error); + }); +} + function getFileNameFromContent(content: string) { const file = getFileAccess().getFile(content); return decodeURIComponent(file.name).split('/').pop().toLowerCase(); diff --git a/apps/toolbox/src/pages/fs-helper.xml b/apps/toolbox/src/pages/fs-helper.xml index 000d09a5d..7b0970cd1 100644 --- a/apps/toolbox/src/pages/fs-helper.xml +++ b/apps/toolbox/src/pages/fs-helper.xml @@ -4,5 +4,6 @@