refactored DockerfileServiceImpl to return promise instead of callbacks
This commit is contained in:
commit
4e913f106d
34 changed files with 299 additions and 490 deletions
|
@ -5,21 +5,30 @@ import { Injectable } from 'angular-ts-decorators';
|
|||
@Injectable(DockerfileService.name)
|
||||
export class DockerfileServiceImpl implements DockerfileService {
|
||||
|
||||
constructor(private DataFileService: any, private Config: any, private FileReaderFactory: () => FileReader) {
|
||||
constructor(private DataFileService: any,
|
||||
private Config: any,
|
||||
private fileReaderFactory: () => FileReader) {
|
||||
|
||||
}
|
||||
|
||||
public extractDockerfile(file: any): Promise<DockerfileInfoImpl | string> {
|
||||
public getDockerfile(file: any): Promise<DockerfileInfoImpl | string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
var reader: FileReader = this.FileReaderFactory();
|
||||
var reader: FileReader = this.fileReaderFactory();
|
||||
reader.onload = (event: any) => {
|
||||
// FIXME: Debugging
|
||||
console.log(event.target.result);
|
||||
|
||||
this.DataFileService.readDataArrayAsPossibleArchive(event.target.result,
|
||||
(files: any[]) => {
|
||||
this.processFiles1(files);
|
||||
this.processFiles(files)
|
||||
.then((dockerfileInfo: DockerfileInfoImpl) => resolve(dockerfileInfo))
|
||||
.catch((error: string) => reject(error));
|
||||
},
|
||||
() => {
|
||||
// Not an archive. Read directly as a single file.
|
||||
this.processFile1(event.target.result);
|
||||
this.processFile(event.target.result)
|
||||
.then((dockerfileInfo: DockerfileInfoImpl) => resolve(dockerfileInfo))
|
||||
.catch((error: string) => reject(error));
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -28,40 +37,7 @@ export class DockerfileServiceImpl implements DockerfileService {
|
|||
});
|
||||
}
|
||||
|
||||
public getDockerfile(file: any,
|
||||
success: (dockerfile: DockerfileInfoImpl) => void,
|
||||
failure: (error: Event | string) => void): void {
|
||||
var reader: FileReader = this.FileReaderFactory();
|
||||
reader.onload = (event: any) => {
|
||||
this.DataFileService.readDataArrayAsPossibleArchive(event.target.result,
|
||||
(files: any[]) => {
|
||||
this.processFiles(files, success, failure);
|
||||
},
|
||||
() => {
|
||||
// Not an archive. Read directly as a single file.
|
||||
this.processFile(event.target.result, success, failure);
|
||||
});
|
||||
};
|
||||
|
||||
reader.onerror = failure;
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
|
||||
private processFile(dataArray: any,
|
||||
success: (dockerfile: DockerfileInfoImpl) => void,
|
||||
failure: (error: ErrorEvent | string) => void): void {
|
||||
this.DataFileService.arrayToString(dataArray, (contents: string) => {
|
||||
var result: DockerfileInfoImpl | null = DockerfileInfoImpl.forData(contents, this.Config);
|
||||
if (result == null) {
|
||||
failure('File chosen is not a valid Dockerfile');
|
||||
}
|
||||
else {
|
||||
success(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private processFile1(dataArray: any): Promise<DockerfileInfoImpl | string> {
|
||||
private processFile(dataArray: any): Promise<DockerfileInfoImpl | string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.DataFileService.arrayToString(dataArray, (contents: string) => {
|
||||
var result: DockerfileInfoImpl | null = DockerfileInfoImpl.forData(contents, this.Config);
|
||||
|
@ -75,31 +51,7 @@ export class DockerfileServiceImpl implements DockerfileService {
|
|||
});
|
||||
}
|
||||
|
||||
private processFiles(files: any[],
|
||||
success: (dockerfile: DockerfileInfoImpl) => void,
|
||||
failure: (error: ErrorEvent | string) => void): void {
|
||||
var found: boolean = false;
|
||||
files.forEach((file) => {
|
||||
if (file['name'] == 'Dockerfile') {
|
||||
this.DataFileService.blobToString(file.toBlob(), (contents: string) => {
|
||||
var result: DockerfileInfoImpl | null = DockerfileInfoImpl.forData(contents, this.Config);
|
||||
if (result == null) {
|
||||
failure('Dockerfile inside archive is not a valid Dockerfile');
|
||||
}
|
||||
else {
|
||||
success(result);
|
||||
}
|
||||
});
|
||||
found = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (!found) {
|
||||
failure('No Dockerfile found in root of archive');
|
||||
}
|
||||
}
|
||||
|
||||
private processFiles1(files: any[]): Promise<DockerfileInfoImpl | string> {
|
||||
private processFiles(files: any[]): Promise<DockerfileInfoImpl | string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
var found: boolean = false;
|
||||
files.forEach((file) => {
|
||||
|
|
Reference in a new issue