open file improve

This commit is contained in:
Peter Maquiran
2024-09-12 15:19:14 +01:00
parent e2df6a0919
commit df079fc66e
2 changed files with 54 additions and 9 deletions
@@ -0,0 +1,47 @@
import { Injectable } from '@angular/core';
import { File, IWriteOptions } from '@awesome-cordova-plugins/file/ngx';
import { err, ok, Result } from 'neverthrow';
import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
@Injectable({
providedIn: 'root'
})
export class FileSystemMobileService {
constructor(
private file: File,
private FileOpener: FileOpener,
) { }
/**
* Write a new file to the desired location.
*
* @param {string} path Base FileSystem. Please refer to the iOS and Android filesystem above
* @param {string} fileName path relative to base path
* @param {string | Blob | ArrayBuffer} text content, blob or ArrayBuffer to write
* @param {IWriteOptions} whether to replace/append to an existing file. See IWriteOptions for more information.
* @param options
* @returns {Promise<any>} Returns a Promise that resolves to updated file entry or rejects with an error.
*/
async writeFile(path: string, fileName: string, context: string | Blob | ArrayBuffer, options?: IWriteOptions): Promise<Result<any, any>> {
try {
const result = await this.file.writeFile(path, fileName, context, { replace: true })
return ok(result)
} catch (e) {
console.log('Error writing file', e)
return err(e)
}
}
async fileOpener(filePath: string, mimetype: string) {
try {
const result = this.FileOpener.open(filePath, mimetype)
return ok(result)
} catch (e) {
console.error('Error opening file', e)
return err(e)
}
}
}