feat(file-system): allow copy when opening a File (#10274)

* feat: add copy file to file-system

* feat(file-system): allow temp copy of files opened with File.fromPath

* chore: remove log

* chore: remove log

* fix: only copy if true

---------

Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
This commit is contained in:
Osei Fortune
2023-04-25 00:30:28 -04:00
committed by GitHub
parent 4551da075b
commit 18bba2bc11
7 changed files with 104 additions and 34 deletions

View File

@ -15,7 +15,7 @@ function getApplicationContext() {
}
function getOrSetHelper(path: string): org.nativescript.widgets.FileHelper {
return org.nativescript.widgets.FileHelper.fromString(applicationContext, path);
return org.nativescript.widgets.FileHelper.fromString(getApplicationContext(), path);
}
function isContentUri(path: string): boolean {
@ -766,6 +766,7 @@ export class FileSystemAccess29 extends FileSystemAccess {
if (isContentUri(path)) {
try {
const file = getOrSetHelper(path);
return {
path,
name: file.getName(),

View File

@ -80,11 +80,27 @@ export class File extends FileSystemEntity {
*/
isLocked: boolean;
/**
* Copies a file to a given path.
* @param dest The path to the destination file.
* Returns a Promise with a boolean.
*/
copy(dest: string): Promise<boolean>;
/**
* Copies a file to a given path.
* @param dest The path to the destination file.
* @param onError (optional) A callback function to use if any error occurs.
* Returns a Promise with a boolean.
*/
copySync(dest: string, onError?: (error: any) => any): any;
/**
* Gets or creates a File entity at the specified path.
* @param path The path to get/create the file at.
* @param copy An optional value when set, copies the content-uri to a temp file enabling the legacy behaviour
*/
static fromPath(path: string): File;
static fromPath(path: string, copy?: boolean): File;
/**
* Reads the content of the file as a string using the specified encoding (defaults to UTF-8).

View File

@ -1,5 +1,6 @@
import { IFileSystemAccess, FileSystemAccess, FileSystemAccess29 } from './file-system-access';
import { SDK_VERSION } from '../utils/constants';
import { getNativeApplication } from '../application';
// The FileSystemAccess implementation, used through all the APIs.
let fileAccess: IFileSystemAccess;
@ -180,12 +181,39 @@ export class FileSystemEntity {
}
}
let applicationContext;
function getApplicationContext() {
if (!applicationContext) {
applicationContext = (<android.app.Application>getNativeApplication()).getApplicationContext();
}
return applicationContext;
}
export class File extends FileSystemEntity {
public static fromPath(path: string) {
public static fromPath(path: string, copy: boolean = false) {
const onError = function (error) {
throw error;
};
if (global.isAndroid && copy) {
if (path.startsWith('content:')) {
const fileInfo = getFileAccess().getFile(path, onError);
// falls back to creating a temp file without a known extension.
if (!fileInfo) {
const tempFile = `${knownFolders.temp().path}/${java.util.UUID.randomUUID().toString()}`;
org.nativescript.widgets.Async.File.copySync(path, tempFile, getApplicationContext());
path = tempFile;
} else {
const ext = fileInfo.extension;
const name = `${fileInfo.name.replace(`.${ext}`, '')}.${ext}`;
const tempFile = `${knownFolders.temp().path}/${name}`;
getFileAccess().copySync(path, tempFile);
path = tempFile;
}
}
}
const fileInfo = getFileAccess().getFile(path, onError);
if (!fileInfo) {
return undefined;