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)
}
}
}
@@ -55,6 +55,7 @@ import { LastMessage } from '../../utils/lastMessage';
import { File } from '@awesome-cordova-plugins/file/ngx';
import { Filesystem, Directory } from '@capacitor/filesystem';
import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
import { FileSystemMobileService } from 'src/app/infra/file-system/mobile/file-system-mobile.service';
const IMAGE_DIR = 'stored-images';
@@ -172,6 +173,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
private messageLocalDataSourceService: MessageLocalDataSourceService,
private file: File,
private fileOpener: FileOpener,
private fileSystemMobileService: FileSystemMobileService
) {
@@ -1385,15 +1387,11 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
console.log(pathFile)
console.log(contentFile)
this.file
.writeFile(pathFile, fileName, contentFile, { replace: true })
.then(success => {
this.fileOpener
.open(pathFile + fileName, fileType)
.then(() => console.log('File is opened'))
.catch(e => console.log('Error opening file', e));
})
.catch(e => console.log('Error writing file', e))
const writeResult = await this.fileSystemMobileService.writeFile(pathFile, fileName, contentFile, { replace: true })
if(writeResult.isOk()) {
const openResult = await this.fileSystemMobileService.fileOpener(pathFile + fileName, fileType)
}
}
removeTextBeforeSlash(inputString, controlString) {