Last active
July 6, 2019 09:49
-
-
Save JO3-W3B-D3V/4e856a9e4e5ed861bc3aabf86648842f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable } from 'rxjs'; | |
export class BasicFileInformation { | |
public fileName: String; | |
public fileSize: Number; | |
public fileData: String | ArrayBuffer; | |
public fileMimeType: String; | |
constructor(fileName: String, fileSize: Number, fileData: String | ArrayBuffer, fileMimeType: String) { | |
this.fileName = fileName; | |
this.fileSize = fileSize; | |
this.fileData = fileData; | |
this.fileMimeType = fileMimeType; | |
} | |
} | |
export abstract class FileStore { | |
public static sotreFiles(fileArray: Array<File>): Observable<Array<BasicFileInformation>> { | |
const files = [...fileArray]; | |
return new Observable(obs => { | |
const dataURLArray: Array<any> = new Array(); | |
files.forEach((file: File) => { | |
const fileReader: FileReader = new FileReader(); | |
fileReader.onload = () => { | |
const basifFileInformation = new BasicFileInformation(file.name, file.size, fileReader.result, file.type); | |
dataURLArray.push(basifFileInformation); | |
// Once we know that the array's match in length, call obs.complete. | |
if (files.length === dataURLArray.length) { | |
obs.next(dataURLArray as Array<any>); | |
obs.complete(); | |
} | |
}; | |
fileReader.readAsDataURL(file); | |
}); | |
}); | |
} | |
public static loadFiles(fileArray: Array<BasicFileInformation>): Observable<Array<File>> { | |
const files = [...fileArray]; | |
const tempFileArray = []; | |
return new Observable(obs => { | |
files.forEach((basicFile: BasicFileInformation) => { | |
const name: any = basicFile.fileName; | |
const mimeType: any = { | |
type: basicFile.fileMimeType | |
}; | |
const url: any = basicFile.fileData; | |
fetch(url) | |
.then(r => r.arrayBuffer()) | |
.then(buf => new File([buf], name, mimeType)) | |
.then(file => tempFileArray.push(file)) | |
.then(() => { | |
obs.next(tempFileArray as Array<any>); | |
obs.complete(); | |
}); | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment