mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
open file improve
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user